def find_study_area_values4(field_name,satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID, max_num, uid, max_dim): from sentinelhub import WebFeatureService, BBox, CRS, DataSource, 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 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) #get_image_edges(get_all_dates_arr(iterator_wms), uid) dates = get_all_dates_arr(iterator_wms) for single_tuple in date_tuples: for single_date in single_tuple: print(['single_date',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 + '/' + str(single_date) +'.tiff' i_num = i_num + 1 #im = np.array(im) im = s1_data[-1] z_num = 0 w,h = len(im[0]), len(im) if imageType != "RVI-NEW": images_arr.append(im[:,:,0]) if imageType == "RVI-NEW": im = make_rgb(im,w,h) #print(im[0]) im = PIL.Image.fromarray(im) im.save((uid + "/area_estimate_rvi.tiff")) else: im = PIL.Image.fromarray(s1_data[-1]) im.save(file_name) #images_arr.append(merge_images(single_tuple, uid)) #get_image_edges(get_all_dates_arr(iterator_wms), uid) new_arr = merge_images2(dates, uid, max_num) print('new_arr', len(new_arr)) return new_arr,w,h new_arr = [] print(['images_arr', len(images_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]) val = round(100*images_arr[k][i][j])/100 #print(val) temp_arr.append(val) 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 merge_images2(dates, uid, max_num): print('merging') from PIL import Image from numpy import asarray import time img_arr = [] all_imgs_arr = [] date_num = 0 merged_name = '' for single_date in dates: file_name = uid + '/' + single_date + '.tiff' merged_name = merged_name + single_date img = Image.open(file_name) img_arr.append(img) width,height = img.size date_num = date_num + 1 x = width y = height #result = [] # Iterate over the pixels in the images for x in range(width): for y in range(height): temp_pixel_arr = [] for i in range(date_num): pixel_value = img_arr[i].getpixel((x, y)) pixel_value = pixel_value[0] #print(['pixel', pixel_value]) if pixel_value != 0 and len(temp_pixel_arr)< max_num: temp_pixel_arr.append(pixel_value) if len(temp_pixel_arr) == 0: for d in range(max_num): temp_pixel_arr.append(0) while len(temp_pixel_arr) 0: new_tuple = Union(prev_tuple, next_tuple) next_tuple = new_tuple # print(new_tuple) main_list.append(new_tuple) else: kv = 1 main_list.append(next_tuple) tuple_num = tuple_num + 1 # print(main_list) # 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