def find_sar(uid,fieldid,map_fields,latestsensedday,paused,expired, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID): from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection from sentinelhub import get_area_info #from sentinelhub import AwsTile from sentinelhub import SHConfig from oct2py import octave from io import BytesIO import base64 from google.cloud import storage from oauth2client.service_account import ServiceAccountCredentials import os import firebase_admin from firebase_admin import credentials from firebase_admin import db from PIL import Image, ImageFilter from send_notification import send_notification from sendemail import sendemail from sen_start_noti import sen_start_noti import os.path import json import scipy import PIL #from scipy import ndimag #from google.cloud import bigquery import datetime import time #from sentinelhub import AwsTileRequest import numpy as np #import cv2 wes1 = 1#print('sasr1') aa = 1 cred = credentials.Certificate('servicekey2.json') #client = bigquery.Client().from_service_account_json('servicekey2.json') wes1 = 1#print('sasr') #map_fields = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).get() #wes1 = 1#print(map_fields) #time.sleep(4) field_latitude = map_fields["Coordinates"]["a"]["Latitude"] field_longitude = map_fields["Coordinates"]["a"]["Longitude"] field_max_lat = map_fields["FieldMaxLat"] field_min_lat = map_fields["FieldMinLat"] field_max_long = map_fields["FieldMaxLong"] field_min_long = map_fields["FieldMinLong"] field_area = map_fields["FieldArea"] if field_max_lat < field_min_lat: temp = field_max_lat field_max_lat = field_min_lat field_min_lat = temp if field_max_long < field_min_long: temp = field_max_long field_max_long = field_min_long field_min_long = temp # Initialize the app with a service account, granting admin privileges #firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) min_lat = field_min_lat max_lat = field_max_lat min_long = field_min_long max_long = field_max_long access_key = 'AKIAIPCM5ZR7FRHMY3MA' secret_key = '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' # access_key = 'AKIAQZCF7Q5CIN2VIHVL' # secret_key = '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' #INSTANCE_ID = '954b8f5e-d44e-4f2e-ab87-2e317246fa02' #INSTANCE_ID = 'a7153cec-25c9-43bc-8d78-0b8600647ee5' config = SHConfig() #CLIENT_ID = 'c1a2fa32-6ca2-414a-9cd0-77556891bf88' #CLIENT_SECRET = 'bR,4M[f8y?C^dR_n]1i1>:g~P@*JXwl]^tciiIN/' # CLIENT_ID = 'ebd1003f-b189-4f4e-9e29-5e56fe0fc343' # CLIENT_SECRET = 'A[W3>BVZk-RNcYko1N9%[da$F*(ahDv)0(|irX8&' if CLIENT_ID and CLIENT_SECRET: config.sh_client_id = CLIENT_ID config.sh_client_secret = CLIENT_SECRET config.instance_id = INSTANCE_ID # if INSTANCE_ID: # config = SHConfig() # config.instance_id = INSTANCE_ID # else: # config = None # INSTANCE_ID = 'e16a1fed-63e3-4de9-8365-117635a744ca' # In case you put instance ID into cofniguration file you can leave this unchanged #img_width = abs(round((max_lat-min_lat)*111*1000/10)) img_width = abs(round((max_lat-min_lat)*111*1000/10)) if img_width >= 1100: img_width = 1100 elif img_width < 100: img_width = 100 wes1 = 1#print('img_width') wes1 = 1#print(img_width) search_bbox = BBox(bbox=[min_long,min_lat,max_long,max_lat], crs=CRS.WGS84) #search_bbox = BBox(bbox=[32.64358939614938,35.23309160023928,32.65433485229119,35.2399842068553], crs=CRS.WGS84) x = datetime.datetime.now() current_month = x.month current_year = x.year current_date = x.day current_date = current_date current_date = current_date start_year = current_year if current_date < 15 and current_month>1: if latestsensedday == 0: start_date = 31-current_date - 15 else: start_date = 31-current_date -14 start_month = current_month-1 start_year = current_year elif current_date <15 and current_month ==1: if latestsensedday == 0: start_date = 31-current_date - 15 else: start_date = 31-current_date - 14 start_month = 12 start_year = start_year-1 else: if latestsensedday == 0: start_date = current_date-15 else: start_date = current_date-14 start_month = current_month start_year = current_year if start_date == 0: start_date = 1 if current_date == 0: current_date = 1 if current_month < 10: current_month = "0"+str(current_month) else: current_month = str(current_month) if start_month < 10: start_month = "0"+str(start_month) else: start_month = str(start_month) if current_date < 10: current_date = "0"+str(current_date) else: current_date = str(current_date) if start_date < 10: start_date = "0"+str(start_date) else: start_date = str(start_date) start_year = str(start_year) if from_date == None: from_date = start_year+'-'+start_month+'-'+str(start_date) to_date = str(current_year)+'-'+current_month+'-'+str(current_date) l_date = str(current_year)+current_month+str(current_date) from_date = from_date+'T00:00:00' to_date = to_date+'T23:59:59' else: l_date = str(current_year)+current_month+str(current_date) # from_date = '2020-11-25T00:00:00' # to_date = '2020-11-30T23:59:59' # from_date = '2020-10-05T00:00:00' # to_date = '2020-07-07T23:59:59' wes1 = 1#print(from_date) wes1 = 1#print(to_date) search_time_interval = (from_date, to_date) wes1 = 1#print(search_time_interval) tile_num = 0 wfs_iterator = WebFeatureService(search_bbox,search_time_interval,data_collection=DataCollection.SENTINEL1_IW,maxcc=100.0,config=config) for tile_info in wfs_iterator: wes1 = 1#print(tile_info) temp_tile = tile_info geom = temp_tile["geometry"] coords = geom["coordinates"] new_coords = coords[0][0] coord_num = 0 wes1 = 1#print('coo') wes1 = 1#print(new_coords) coord_len = len(new_coords) wes1 = 1#print(coord_len) coord_iter = 0 west_lons = [] east_lons = [] north_lats = [] south_lats = [] all_lons = [] for kk in new_coords: latlng = new_coords[coord_iter] longitude = latlng[0] all_lons.append(longitude) if coord_iter<2: longitude = latlng[0] west_lons.append(longitude) elif coord_iter>1 and coord_iter<4: longitude = latlng[0] east_lons.append(longitude) if coord_iter ==0 or coord_iter==3: latitude = latlng[1] north_lats.append(latitude) elif coord_iter ==1 or coord_iter==2: latitude = latlng[1] south_lats.append(latitude) coord_iter = coord_iter+1 all_lons.sort() all_lons = np.unique(all_lons) wes1 = 1#print('all_lons') wes1 = 1#print(all_lons) west_lons = [] east_lons = [] west_lons.append(all_lons[0]) west_lons.append(all_lons[1]) east_lons.append(all_lons[(len(all_lons)-2)]) east_lons.append(all_lons[(len(all_lons)-1)]) wes1 = 1#print(west_lons) wes1 = 1#print(east_lons) wes1 = 1#print(north_lats) wes1 = 1#print(south_lats) west_lon = min(west_lons) east_lon = max(east_lons) min_east_lon = min(east_lons) north_lat = max(north_lats) south_lat = min(south_lats) if min_east_lon>max_long: tile_id = tile_info["properties"]["id"] temp_map_obj = {} A = new_coords[0] A_lat = A[1] A_long = A[0] B = new_coords[1] B_lat = B[1] B_long = B[0] C = new_coords[2] C_lat = C[1] C_long = C[0] D = new_coords[3] D_lat = D[1] D_long = D[0] temp_map_obj["A_lat"] = A_lat temp_map_obj["A_long"] = A_long temp_map_obj["B_lat"] = B_lat temp_map_obj["B_long"] = B_long temp_map_obj["C_lat"] =C_lat temp_map_obj["C_long"]=C_long temp_map_obj["D_lat"] = D_lat temp_map_obj["D_long"]=D_long temp_map_obj["TileMaxLat"]=north_lat temp_map_obj["TileMinLat"]=south_lat temp_map_obj["TileMaxLong"]=west_lon temp_map_obj["TileMinLong"]=east_lon temp_map_obj["FieldMaxLat"]=field_max_lat temp_map_obj["FieldMinLat"]=field_min_lat temp_map_obj["FieldMaxLong"]=field_max_long temp_map_obj["FieldMinLong"]=field_min_long temp_map_obj["FieldArea"]=field_area temp_map_obj["UID"]=uid temp_map_obj["cloud_cover"]=0 #coord_iter = coord_iter+1 wes1 = 1#print('tileID') wes1 = 1#print(tile_id) tile_id = tile_info["properties"]["id"] temp_map_obj["MGRS"]="sample" latest_date = tile_info["properties"]["date"] latest_date = latest_date[0:10] latest_date = latest_date.replace("-","") temp_map_obj["LatestDay"]=latest_date s1_request = WmsRequest(data_collection=DataCollection.SENTINEL1_IW,layer='RVI-NEW',bbox=search_bbox,time=tile_info["properties"]["date"],width=img_width,config=config) s1_data = s1_request.get_data() s1_request_sm = WmsRequest(data_collection=DataCollection.SENTINEL1_IW,layer='RSM',bbox=search_bbox,time=tile_info["properties"]["date"],width=img_width,config=config) s1_data_sm = s1_request_sm.get_data() if int(latest_date)>int(latestsensedday) and paused == 0 and expired ==0: file_name= uid+ '/' + fieldid +'/rvi.png' file_sm = uid +'/' + fieldid + '/rsm.png' #file_name2= uid+'/rvi2.png' #cv2.imwrite(file_name,s1_data[0]) im = PIL.Image.fromarray(s1_data[-1],"L") im_sm = PIL.Image.fromarray(s1_data_sm[-1],"L") #im = s1_data[-1] isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) im_sm.save(file_sm) im.save(file_name) #im.save(file_name2) #cv2.imwrite(file_name,im) return temp_map_obj temp_map_obj = {} temp_map_obj["TileMaxLat"]="NA" temp_map_obj["TileMinLat"]="NA" temp_map_obj["TileMaxLong"]="NA" temp_map_obj["TileMinLong"]="NA" temp_map_obj["FieldMaxLat"]=field_max_lat temp_map_obj["FieldMinLat"]=field_min_lat temp_map_obj["FieldMaxLong"]=field_max_long temp_map_obj["FieldMinLong"]=field_min_long temp_map_obj["FieldArea"]=field_area temp_map_obj["UID"]=uid try: temp_map_obj["LatestDay"]=latest_date except: temp_map_obj["LatestDay"]=l_date temp_map_obj["MGRS"]="NotA" temp_map_obj["cloud_cover"]="NA" return temp_map_obj