def make_on_the_go_farm_image(study_uid, field_name,satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID, max_num): 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 = 500 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: try: width = 2500 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 = 2500 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) im = make_rgb(im,w,h) im = PIL.Image.fromarray(im) im.save((study_uid + "/area_estimate_ndvi_" + field_name + ".png")) time.sleep(1) break 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