def make_on_the_go_farm_image(c_map, uid, field_id, polygon_id,satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID): from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection from sentinelhub import SHConfig, MosaickingOrder 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 make_geotiff import make_geotiff import traceback from get_mask import get_mask from make_dir import make_dir #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 = 100 search_bbox = BBox(bbox=bounds, crs=CRS.WGS84) search_time_interval = (from_date, to_date) avg_im = 0 i = 0 i_num = 0 arr_init = 0 make_dir(uid, polygon_id) print(search_time_interval) iterator_wms = WebFeatureService(search_bbox,search_time_interval,data_collection=satellite,maxcc=0.5,config=config) images_arr = [] prev_date = '90101010' for tile_info in iterator_wms: print(tile_info) try: width = 40 s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],width = width, image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() except: height = 40 s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],height = height,image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() file_name= str(tile_info["properties"]["date"]) + str(i_num)+'.tiff' i_num = i_num + 1 im = s1_data[-1] index_val = 0 w,h = len(im[0]), len(im) if imageType == 'EVAPOTRANSPIRATION': im = PIL.Image.fromarray(s1_data[-1],"RGB") elif imageType== 'TCI': im = PIL.Image.fromarray(s1_data[-1]) im = im.convert("RGB") else: if c_map == 2: im, index_val = make_rgb2(im,w,h) else: im, index_val = make_rgb(im,w,h) im = PIL.Image.fromarray(im) isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) if c_map == 1: file_name = uid + "/" +polygon_id + "/" + imageType + ".png" else: file_name = uid + "/" +polygon_id + "/" + imageType + "_CMAP2.png" im.save(file_name) time.sleep(1) return tile_info["properties"]["date"], index_val #break return None, None def make_rgb2(im,w,h): import numpy as np index_val = 0 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.5: new_im[p_num][q_num] =[13,13,13] index_val =index_val + 0 elif q/255 > -0.5 and q/255 <= -0.2: new_im[p_num][q_num] =[191,191,191] index_val = index_val + 0 elif q/255 > -0.2 and q/255 <= -0.1: new_im[p_num][q_num] =[219,219,219] index_val = index_val + 0 elif q/255 > -0.1 and q/255 <=0: new_im[p_num][q_num] =[235,235,235] index_val = index_val + 0 elif q/255 > 0 and q/255 <= 0.025: new_im[p_num][q_num] =[254,250,202] index_val = index_val + 0.025 elif q/255 > 0.025 and q/255 <=0.05: new_im[p_num][q_num] =[238,232,182] index_val = index_val + 0.05 elif q/255 > 0.05 and q/255 <=0.075: new_im[p_num][q_num] =[222,218,157] index_val = index_val + 0.075 elif q/255 > 0.075 and q/255 <=0.1: new_im[p_num][q_num] =[205,200,132] index_val = index_val + 0.1 elif q/255 > 0.1 and q/255 <=0.125: new_im[p_num][q_num] =[191,183,108] index_val = index_val + 0.125 elif q/255 > 0.125 and q/255 <=0.15: new_im[p_num][q_num] =[175,195,98] index_val = index_val + 0.15 elif q/255 > 0.15 and q/255 <=0.175: new_im[p_num][q_num] =[164,205,87] index_val = index_val + 0.175 elif q/255 > 0.175 and q/255 <=0.2: new_im[p_num][q_num] =[127,179,71] index_val = index_val + 0.2 elif q/255 > 0.2 and q/255 <=0.25: new_im[p_num][q_num] =[111,161,64] index_val = index_val + 0.25 elif q/255 > 0.25 and q/255 <=0.3: new_im[p_num][q_num] =[96,150,54] index_val = index_val + 0.3 elif q/255 > 0.3 and q/255 <=0.35: new_im[p_num][q_num] =[79,138,46] index_val = index_val + 0.35 elif q/255 > 0.35 and q/255 <=0.4: new_im[p_num][q_num] =[65,125,37] index_val = index_val + 0.4 elif q/255 > 0.4 and q/255 <=0.45: new_im[p_num][q_num] =[48,110,27] index_val = index_val + 0.45 elif q/255 > 0.45 and q/255 <=0.5: new_im[p_num][q_num] =[34,96,19] index_val = index_val + 0.5 elif q/255 > 0.5 and q/255 <=0.55: new_im[p_num][q_num] =[15,83,105] index_val = index_val + 0.55 elif q/255 > 0.55: new_im[p_num][q_num] =[2,69,0] index_val = index_val + 0.6 else: new_im[p_num][q_num] =[254,250,202] q_num = q_num + 1 p_num = p_num + 1 index_val = index_val/q_num return new_im, index_val def make_rgb(im,w,h): import numpy as np index_val =0 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] index_val = index_val + 0.1 elif q/255 > 0.1 and q/255 <= 0.2: new_im[p_num][q_num] =[234,79,59] index_val = index_val + 0.2 elif q/255 > 0.2 and q/255 <= 0.3: new_im[p_num][q_num] =[247,136,90] index_val = index_val + 0.3 elif q/255 > 0.3 and q/255 <=0.4: new_im[p_num][q_num] =[251,192,126] index_val = index_val + 0.4 elif q/255 > 0.4 and q/255 <= 0.5: new_im[p_num][q_num] =[255,240,181] index_val = index_val + 0.5 elif q/255 > 0.5 and q/255 <=0.6: new_im[p_num][q_num] =[230,243,164] index_val = index_val + 0.6 elif q/255 > 0.6 and q/255 <=0.7: new_im[p_num][q_num] =[186,227,131] index_val = index_val + 0.7 elif q/255 > 0.7 and q/255 <=0.8: new_im[p_num][q_num] =[129,191,108] index_val = index_val + 0.8 elif q/255 > 0.8 and q/255 <=0.9: new_im[p_num][q_num] =[17,167,95] index_val = index_val + 0.9 else: new_im[p_num][q_num] =[6,101,61] q_num = q_num + 1 p_num = p_num + 1 index_val =index_val/q_num return new_im, index_val