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 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 def get_land_use(uid,fieldid, map_fields, latestsensedday,latestfailedday,expired,paused, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID): 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 = '28b9b1e8-f791-47cc-adc5-cd56b3004002' print('land here') 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 = 'e9ff6a59-0e56-4a3b-9033-dba23e74bd99' # 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 start_year = current_year 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) 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 = '2021-12-25T00:00:00' # to_date = '2022-01-02T23:59:59' # from_date = '2021-10-25T00:00:00' # to_date = '2021-11-05T23:59:59' #print(l_date) # img_width = abs(round((max_lat-min_lat)*111*1000/10)) # if img_width >= 700: # img_width = 700 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 # 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 print(search_bbox) try: wfs_iterator = WebFeatureService(search_bbox,search_time_interval,data_collection=DataCollection.SENTINEL2_L2A,maxcc=100.0,config=config) except Exception as e: print(e) #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','B11','B12'] # bands=['R20m/B02','R20m/B03','R20m/B04','R20m/B05','R20m/B08','R60m/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) and int(latest_date) > int(latestfailedday) and paused == 0 and expired ==0: #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() print('getting') #s1_request = WmsRequest(data_collection=DataCollection.SENTINEL2_L1C,layer='LULC',bbox=search_bbox,time='latest',width=img_width,config=config) s1_request = WmsRequest(data_collection=DataCollection.SENTINEL2_L2A,layer='LULC',bbox=search_bbox,time=search_time_interval,width=img_width,config=config) s1_data = s1_request.get_data() file_name= uid+ '/' + fieldid+'/lulc.png' im = PIL.Image.fromarray(s1_data[-1],"RGB") isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) 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 def getLayer(layerName, search_bbox,img_width, config, uid, dataSet): print('layer') s1_request = WmsRequest(data_collection= dataSet,layer=layerName,bbox=search_bbox,time='latest',width=img_width,config=config) s1_data = s1_request.get_data() file_name= uid+'/' + layerName + '.png' im = PIL.Image.fromarray(s1_data[-1],"L") isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) im.save(file_name)