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 from gen_geotiff2 import gen_geotiff2 uid = 'KZQ7TZIYXnXN0b07OtrL1hlyYij1' fieldid = '1653834416376' aa = 1 cred = credentials.Certificate('servicekey2.json') client = bigquery.Client().from_service_account_json('servicekey2.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) map_fields = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).get() sentinel_settings = db.reference('SentinelSettings').get() print(sentinel_settings) CLIENT_ID = sentinel_settings["ClientID"] CLIENT_SECRET = sentinel_settings["ClientSecret"] INSTANCE_ID = sentinel_settings["WMSID"] #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 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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' config = SHConfig() if CLIENT_ID and CLIENT_SECRET: config.sh_client_id = CLIENT_ID config.sh_client_secret = CLIENT_SECRET config.instance_id = INSTANCE_ID search_bbox = BBox(bbox=[min_long,min_lat,max_long,max_lat], crs=CRS.WGS84) from_dates = ['2018-01-01','2018-01-15','2018-02-01','2018-02-15','2018-03-01','2018-03-15','2018-04-01','2018-04-15','2018-05-01','2018-05-15','2018-06-01','2018-06-15' ,'2018-07-01','2018-07-15','2018-08-01','2018-08-15','2018-09-01','2018-09-15','2018-10-01','2018-10-15','2018-11-01','2018-11-15','2018-12-01','2018-12-15', '2019-01-01','2019-01-15','2019-02-01','2019-02-15','2019-03-01','2019-03-15','2019-04-01','2019-04-15','2019-05-01','2019-05-15','2019-06-01','2019-06-15' ,'2019-07-01','2019-07-15','2019-08-01','2019-08-15','2019-09-01','2019-09-15','2019-10-01','2019-10-15','2019-11-01','2019-11-15','2019-12-01','2019-12-15', '2020-01-01','2020-01-15','2020-02-01','2020-02-15','2020-03-01','2020-03-15','2020-04-01','2020-04-15','2020-05-01','2020-05-15','2020-06-01','2020-06-15' ,'2020-07-01','2020-07-15','2020-08-01','2020-08-15','2020-09-01','2020-09-15','2020-10-01','2020-10-15','2020-11-01','2020-11-15','2020-12-01','2020-12-15', '2021-01-01','2021-01-15','2021-02-01','2021-02-15','2021-03-01','2021-03-15','2021-04-01','2021-04-15','2021-05-01','2021-05-15','2021-06-01','2021-06-15' ,'2021-07-01','2021-07-15','2021-08-01','2021-08-15','2021-09-01','2021-09-15','2021-10-01','2021-10-15','2021-11-01','2021-11-15','2021-12-01','2021-12-15', '2022-01-01','2022-01-15','2022-02-01','2022-02-15','2022-03-01','2022-03-15','2022-04-01','2022-04-15','2022-05-01','2022-05-15'] date_num = 0 for from_date in from_dates: date_num = date_num + 1 if date_num < len(from_dates): to_date = from_dates[date_num] else: to_date = '2022-05-25' # from_date = from_date + 'T00:00:00' # to_date = to_date+'T23:59:59' img_width = abs(round((max_lat-min_lat)*111*1000/10)) # 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,config=config) #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"] #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] new_time = time time = time.replace("-","") latest_date = tile_info["properties"]["date"] latest_date = latest_date[0:10] latest_date = latest_date.replace("-","") # s1_request = WmsRequest(data_collection=DataCollection.SENTINEL2_L1C,layer='FALSE',bbox=search_bbox,time=(from_date,to_date),width=img_width,config=config) # s1_data = s1_request.get_data() file_name= uid+'/' +latest_date + 'false.png' file_tiff = uid+'/' +latest_date + 'false.tiff' gen_geotiff2(map_fields,uid,file_name,file_tiff) # im = PIL.Image.fromarray(s1_data[-1],"RGB") # isdir = os.path.isdir(uid) # if isdir != True: # os.mkdir(uid) # im.save(file_name)