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, 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 make_geotiff import make_geotiff import traceback from get_mask import get_mask #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 iterator_wms = WebFeatureService(search_bbox,search_time_interval,data_collection=satellite,maxcc=1.0,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] 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 = make_rgb2(im,w,h) else: im = make_rgb(im,w,h) im = PIL.Image.fromarray(im) isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) im.save((uid + "/" +polygon_id + "_" + imageType + ".png")) time.sleep(1) return tile_info["properties"]["date"] #break def make_rgb2(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.5: new_im[p_num][q_num] =[13,13,13] elif q/255 > -0.5 and q/255 <= -0.2: new_im[p_num][q_num] =[191,191,191] elif q/255 > -0.2 and q/255 <= -0.1: new_im[p_num][q_num] =[219,219,219] elif q/255 > -0.1 and q/255 <=0: new_im[p_num][q_num] =[235,235,235] elif q/255 > 0 and q/255 <= 0.025: new_im[p_num][q_num] =[254,250,202] elif q/255 > 0.025 and q/255 <=0.05: new_im[p_num][q_num] =[238,232,182] elif q/255 > 0.05 and q/255 <=0.075: new_im[p_num][q_num] =[222,218,157] elif q/255 > 0.075 and q/255 <=0.1: new_im[p_num][q_num] =[205,200,132] elif q/255 > 0.1 and q/255 <=0.125: new_im[p_num][q_num] =[191,183,108] elif q/255 > 0.125 and q/255 <=0.15: new_im[p_num][q_num] =[175,195,98] elif q/255 > 0.15 and q/255 <=0.175: new_im[p_num][q_num] =[164,205,87] elif q/255 > 0.175 and q/255 <=0.2: new_im[p_num][q_num] =[127,179,71] elif q/255 > 0.2 and q/255 <=0.25: new_im[p_num][q_num] =[111,161,64] elif q/255 > 0.25 and q/255 <=0.3: new_im[p_num][q_num] =[96,150,54] elif q/255 > 0.3 and q/255 <=0.35: new_im[p_num][q_num] =[79,138,46] elif q/255 > 0.35 and q/255 <=0.4: new_im[p_num][q_num] =[65,125,37] elif q/255 > 0.4 and q/255 <=0.45: new_im[p_num][q_num] =[48,110,27] elif q/255 > 0.45 and q/255 <=0.5: new_im[p_num][q_num] =[34,96,19] elif q/255 > 0.5 and q/255 <=0.55: new_im[p_num][q_num] =[15,83,105] elif q/255 > 0.55: new_im[p_num][q_num] =[2,69,0] else: new_im[p_num][q_num] =[254,250,202] q_num = q_num + 1 p_num = p_num + 1 return new_im 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