def find_study_area_values3(field_id,satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID, max_num, uid, max_dim, day_gap): from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection from sentinelhub import SHConfig from firebase_admin import credentials from firebase_admin import db from statistics import mean from PIL import Image, ImageFilter import os.path import PIL import numpy as np from oct2py import octave from io import BytesIO import base64 import time from sentinelhub import BBoxSplitter from pathlib import Path from osgeo import gdal import os from osgeo import gdal from osgeo import osr import numpy as np import os,sys import cv2 import csv import json from datetime import date from make_geotiff import make_geotiff import traceback #from scipy import ndimag config = SHConfig() #print(imageType) img_values_arr = [] cred = credentials.Certificate('servicekey2.json') access_key = 'AKIAIPCM5ZR7FRHMY3MA' secret_key = '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' if CLIENT_ID and CLIENT_SECRET: config.sh_client_id = CLIENT_ID config.sh_client_secret = CLIENT_SECRET config.instance_id = INSTANCE_ID lat_len = (bounds[3]-bounds[1]) lng_len = bounds[2] - bounds[0] #if width > height img_width = 500 search_bbox = BBox(bbox=bounds, crs=CRS.WGS84) search_time_interval = (from_date, to_date) isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) avg_im = 0 i = 0 i_num = 0 x_num =0 arr_init = 0 iterator_wms = WebFeatureService(search_bbox,search_time_interval,data_collection=satellite,maxcc=1.0,config=config) images_arr = [] prev_date = '90101010' # date_tuples = group_dates(iterator_wms) # for single_date in date_tuples: n_dates = 0 for tile_info in iterator_wms: n_dates = n_dates + 1 print(('n_dates: ' + str(n_dates) + ', max_dim: ' + str(max_num))) # if n_dates > max_dim # we need to adjust day_gap # day_gap = round(day_gap*(n_dates/max_dim)) # if int(n_dates) > int(max_num): # day_gap = round(n_dates/max_num) # print(('new day gap: ' + str(day_gap))) for tile_info in iterator_wms: if x_num %day_gap == 0: single_date = tile_info["properties"]["date"] print(single_date) try: #width = 500 s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=single_date,width = max_dim, image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() except: #height = 500 s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=single_date,height = max_dim,image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() file_name= uid + '/' + field_id + '/' + str(single_date) + str(i_num)+'.tiff' i_num = i_num + 1 #im = np.array(im) im = s1_data[-1] z_num = 0 if imageType == "RVI-NEW": for p in im: for q in p: if q.any() == 0: z_num = z_num + 1 else: for p in im: for q in p: try: if q[1] == 0: z_num = z_num + 1 except: z_num = z_num #im = PIL.Image.open((field_name + '/' +file_name)) #im = np.array(im) w,h = len(im[0]), len(im) print(['z_num: ', z_num, w, h]) new_date = tile_info["properties"]["date"] prev_day = prev_date.replace("-","") new_day = new_date.replace("-","") file_date = new_date if z_num < int(0.1*w*h): if int(prev_day) != int(new_day): print((prev_day + ', ' + new_day)) if imageType != "RVI-NEW": prev_date = new_date #im = np.array(im) images_arr.append(im[:,:]) if imageType == "RVI-NEW": im = make_rgb(im,w,h) #print(im[0]) im = PIL.Image.fromarray(im) im.save((uid + "/" + field_id+ "/area_estimate_rvi.tiff")) break else: im = PIL.Image.fromarray(s1_data[-1]) im.save(file_name) x_num = x_num + 1 new_arr = [] if imageType != "RVI-NEW": try: for i in range(0, len(images_arr[0])): for j in range(0, len(images_arr[0][0])): temp_arr = [] for k in range(0,max_num): #print([k,i,j]) #print(images_arr[k][i][j][0]) #temp_arr.append(round((100*images_arr[k][i][j][0])/100)) try: temp_arr.append(np.round((100*images_arr[k][i][j][0])/100)) except: temp_arr.append(np.round((100*images_arr[k][i][j])/100)) new_arr.append(temp_arr) except Exception as e: print(traceback.format_exc()) aaa = 1 time.sleep(0.25) return new_arr,w,h def group_dates(iterator_wms): date_tuple = [] all_dates = [] from datetime import date iter_num = 0 for tile_info in iterator_wms: temp_date = tile_info["properties"]["date"] if iter_num == 0: prev_date, new_date = temp_date, temp_date else: if temp_date != new_date: prev_date = new_date new_date = temp_date all_dates.append(temp_date) #print([new_date, prev_date, temp_date]) d0 = date(int(prev_date[0:4]), int(prev_date[5:7]), int(prev_date[8:])) d1 = date(int(new_date[0:4]), int(new_date[5:7]), int(new_date[8:])) delta = d1 - d0 # print('delta days') # print(delta.days) if (abs(delta.days) < 4): date_tuple.append([prev_date, new_date]) iter_num = + iter_num + 1 tuple_num = 0 print([all_dates,date_tuple]) main_list = [] for single_tuple in date_tuple: if tuple_num == 0: prev_tuple = single_tuple next_tuple = single_tuple else: prev_tuple = next_tuple next_tuple = single_tuple # print([replace_dash(prev_tuple), replace_dash(next_tuple)]) # print(intersection(replace_dash(prev_tuple), replace_dash(next_tuple))) if len(intersection(prev_tuple,next_tuple))> 0: new_tuple = Union(prev_tuple, next_tuple) print(new_tuple) main_list.append(new_tuple) else: main_list.append(next_tuple) tuple_num = tuple_num + 1 print('main_list') res = [] [res.append(x) for x in main_list if x not in res] #my_list = [*set(main_list)] print(res) return res def replace_dash(arr): return arr new_arr = [] for i in arr: i = i.replace("-","") new_arr.append(int(i)) return new_arr def intersection(lst1, lst2): lst3 = [value for value in lst1 if value in lst2] return lst3 def Union(lst1, lst2): newList = lst1 for element in lst2: if element not in newList: newList.append(element) return newList def make_rgb(im,w,h): import numpy as np new_im = np.zeros((h,w,3), dtype=np.uint8) p_num = 0 q_num = 0 for p in im: q_num = 0 for q in p: if q/255 <=0.1: new_im[p_num][q_num] =[171,5,53] elif q/255 > 0.1 and q/255 <= 0.2: new_im[p_num][q_num] =[234,79,59] elif q/255 > 0.2 and q/255 <= 0.3: new_im[p_num][q_num] =[247,136,90] elif q/255 > 0.3 and q/255 <=0.4: new_im[p_num][q_num] =[251,192,126] elif q/255 > 0.4 and q/255 <= 0.5: new_im[p_num][q_num] =[255,240,181] elif q/255 > 0.5 and q/255 <=0.6: new_im[p_num][q_num] =[230,243,164] elif q/255 > 0.6 and q/255 <=0.7: new_im[p_num][q_num] =[186,227,131] elif q/255 > 0.7 and q/255 <=0.8: new_im[p_num][q_num] =[129,191,108] elif q/255 > 0.8 and q/255 <=0.9: new_im[p_num][q_num] =[17,167,95] else: new_im[p_num][q_num] =[6,101,61] q_num = q_num + 1 p_num = p_num + 1 return new_im