def find_dem(uid,fieldid,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 ndimage #from google.cloud import bigquery import datetime import time #from sentinelhub import AwsTileRequest import numpy as np #import cv2 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 = '3ca9f166-597b-40c8-a4af-0387972bb00a' 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 print('img_width') 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' print(from_date) print(to_date) search_time_interval = (from_date, to_date) print(search_time_interval) tile_num = 0 temp_map_obj ={} temp_map_obj["LatestDay"] = l_date if paused == 0 and expired ==0: s1_request = WmsRequest(data_collection=DataCollection.DEM,layer='DEM-NEW',bbox=search_bbox,time='latest',width=img_width,config=config) s1_data = s1_request.get_data() file_name= uid+'/' + fieldid +'/dem.png' #cv2.imwrite(file_name,s1_data[0]) im = PIL.Image.fromarray(s1_data[0],"RGB") isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) im.save(file_name) #cv2.imwrite(file_name,im) return temp_map_obj