def search_new_sentinel(uid,fieldid,latestsensedday,latestfailedday): from sentinelhub import WebFeatureService, BBox, CRS, DataSource, 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 import PIL from send_notification import send_notification from sendemail import sendemail from sen_start_noti import sen_start_noti import json import scipy from scipy import ndimage from google.cloud import bigquery import datetime import time from sentinelhub import AwsTileRequest import numpy as np aa = 1 cred = credentials.Certificate('servicekey2.json') client = bigquery.Client().from_service_account_json('servicekey2.json') map_fields = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).get() #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 = 'f8482e6b-a91f-47df-ac17-fda8a7c0a9c0' config = SHConfig() CLIENT_ID = 'f2d6f727-50da-4639-b8f4-b0982d1c4d13' CLIENT_SECRET = '_<_BU3>(f>-|7Se2puu0QH^;oJa]kD.>&vP**yFW' # 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' INSTANCE_ID = 'e2e8adb2-a193-4774-af3a-062f53fdba15' # In case you put instance ID into cofniguration file you can leave this unchanged 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 if current_date < 5 and current_month>1: if latestsensedday == 0: start_date = 31-current_date - 5 else: start_date = 31-current_date - 4 start_month = current_month-1 start_year = current_year elif current_date <5 and current_month ==1: if latestsensedday == 0: start_date = 31-current_date - 5 else: start_date = 31-current_date - 4 start_month = 12 start_year = start_year-1 else: if latestsensedday == 0: start_date = current_date-5 else: start_date = current_date-4 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) 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' # from_date = '2020-10-21T00:00:00' # to_date = '2020-10-25T23:59:59' # from_date = '2020-10-05T00:00:00' # to_date = '2020-07-07T23:59:59' img_width = abs(round((max_lat-min_lat)*111*1000/10)) print('im_w') print(img_width) print(from_date) print(to_date) search_time_interval = (from_date, to_date) print(search_time_interval) tile_num = 0 wfs_iterator = WebFeatureService(search_bbox,search_time_interval,maxcc=1.0) #for tile_info in get_area_info(search_bbox, search_time_interval,maxcc=0.5): sat_tiles = wfs_iterator.get_tiles() print('tiles') print(sat_tiles) for tile_info in wfs_iterator: print(tile_info) temp_tile = tile_info geom = temp_tile["geometry"] coords = geom["coordinates"] new_coords = coords[0][0] coord_num = 0 print('coo') print(new_coords) coord_len = len(new_coords) 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) print('all_lons') 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)]) print(west_lons) print(east_lons) print(north_lats) 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"]=tile_info["properties"]["cloudCoverPercentage"] #coord_iter = coord_iter+1 print('tileID') print(tile_id) #tile_id = tile_info["title"] #tile_name, time, aws_index = AwsTile.tile_id_to_tile(tile_id) tile_name, time, aws_index = sat_tiles[0] temp_map_obj["MGRS"]=tile_name new_time = time time = time.replace("-","") latest_date = tile_info["properties"]["date"] latest_date = latest_date[0:10] latest_date = latest_date.replace("-","") temp_map_obj["LatestDay"]=latest_date bands=['B02','B03','B04','B05','B08','B12'] metafiles = ['tileInfo', 'preview', 'qi/MSK_CLOUDS_B00','TCI'] data_folder = './AwsData' ar =0 br = 0 if ar == 0: if br ==0: #if int(latest_date)>int(latestsensedday): # if int(latest_date) > int(latestfailedday): request = AwsTileRequest(tile=tile_name, time=new_time, aws_index=aws_index, bands=bands, metafiles=metafiles, data_folder=data_folder,data_source=DataSource.SENTINEL2_L1C) request.save_data() s1_request = WmsRequest(data_collection=DataCollection.SENTINEL2_L1C,layer='EVAPOTRANSPIRATION',bbox=search_bbox,time='latest',width=img_width,config=config) s1_data = s1_request.get_data() file_name= uid+'/evapo.png'; #cv2.imwrite(file_name,s1_data[0]) im = PIL.Image.fromarray(s1_data[-1],"RGB") im.save(file_name) 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 temp_map_obj["LatestDay"]=l_date temp_map_obj["MGRS"]="NotA" temp_map_obj["cloud_cover"]="NA" return temp_map_obj