from osgeo import gdal from osgeo import osr import numpy as np import os,sys import cv2 import csv from io import BytesIO import base64 from google.cloud import storage from oauth2client.service_account import ServiceAccountCredentials import os import firebase_admin from firebase_admin import credentials from firebase_admin import db cred = credentials.Certificate('servicekey.json') firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) uid = 'aSrbxuElAmhNVHFVEaxe5lJc0Rm2' temp_list = db.reference('PaidMonitoredFields').child('PMF').child('aSrbxuElAmhNVHFVEaxe5lJc0Rm2').get() for (p,q) in temp_list.items(): new_string_json = temp_list[p] print(new_string_json) sensedDaysObj = new_string_json["SensedDays"] fieldID = new_string_json["FieldID"] for (x,y) in sensedDaysObj.items(): tempDay = x storage_client = storage.Client.from_service_account_json('servicekey.json') bucket_name = 'farmbase-b2f7e.appspot.com' destination_blob_name = 'PaidMonitoredFields/'+uid+'/'+fieldID+'/'+tempDay+'/ndvi' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) tempFileName = 'aSrbxuElAmhNVHFVEaxe5lJc0Rm2/ndvi_'+fieldID + "_" +tempDay + '.png' blob.download_to_filename(tempFileName) f_name= tempFileName f_tiff_name = 'aSrbxuElAmhNVHFVEaxe5lJc0Rm2/ndvi_' + fieldID + "_"+tempDay + '.tif' n_file = cv2.imread(f_name) orig_h,orig_w,orig_rgb = n_file.shape w_new = orig_w h_new = orig_h w = round(400*w_new/h_new) w_new = w h_new = 400 print('new_wh') print(w_new) print(h_new) n_file = cv2.resize(n_file,(w_new,h_new)) image_size = (w_new,h_new) newfieldminlat =new_string_json["FieldMinLat"] newfieldmaxlat =new_string_json["FieldMaxLat"] newfieldminlong =new_string_json["FieldMinLong"] newfieldmaxlong =new_string_json["FieldMaxLong"] lat = [newfieldminlat, newfieldmaxlat] lon = [newfieldminlong, newfieldmaxlong] b_pixels,g_pixels,r_pixels = cv2.split(n_file) nx = image_size[1] ny = image_size[0] xmin,ymin,xmax,ymax = [newfieldminlong, newfieldminlat, newfieldmaxlong, newfieldmaxlat] xres = (float(xmax)-float(xmin))/float(ny) yres = (float(ymax)-float(ymin))/float(nx) xmin = float(xmin) ymin = float(ymin) xmax = float(xmax) ymax = float(ymax) xres = float(xres) yres = float(yres) geotransform = (xmin,xres,0,ymax,0,-yres) dst_ds = gdal.GetDriverByName('GTiff').Create(f_tiff_name,ny,nx,3,gdal.GDT_Byte) dst_ds.SetGeoTransform(geotransform) srs = osr.SpatialReference() srs.ImportFromEPSG(4326) dst_ds.SetProjection(srs.ExportToWkt()) dst_ds.GetRasterBand(1).WriteArray(r_pixels) dst_ds.GetRasterBand(2).WriteArray(g_pixels) dst_ds.GetRasterBand(3).WriteArray(b_pixels) dst_ds.FlushCache() dst_ds=None