try: from osgeo import gdal except: import gdal from osgeo import osr import numpy as np import os,sys import cv2 import csv import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import time import threading import os from numba import jit, cuda cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/','storageBucket': 'farmbase-b2f7e.appspot.com'}) except: print('fire running') from firebase_admin import storage bucket = storage.bucket() uid = 'vPM6EKWHfHRxmRfc0YJ7PZ8J9tR2' uid = 'zkBZGiByagbnwFvjOmtLnSBVLeu1' uid = 'KzkCnSmHLpOkhnseAhhRWrI35BD2' uid = 'JgbkYJkMVsRXpSndLsDlabDomOp2' uid = 'wEJQVaxiZZaaId2aJe6lRJ8Zd0S2' isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) all_farms_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).get() for (fieldid, fieldobj) in all_farms_obj.items(): try: source_blob_name = 'PaidMonitoredFields/' + uid + '/' + fieldid + '/10101010/hybrid' try: fieldname = fieldobj["FieldDescription"] except: fieldname = fieldobj["FieldAddress"] blob = bucket.blob(source_blob_name) print(source_blob_name) file_name = fieldname + '.png' try: blob.download_to_filename(file_name) except: aw = 1 f_name= file_name f_tiff_name = uid + '/'+ fieldname + '.tif' new_string_json = fieldobj print(f_name) print(f_tiff_name) n_file = cv2.imread(f_name) orig_h,orig_w,orig_rgb = n_file.shape w_new = orig_w h_new = orig_h factor = 25000 w = round(factor*w_new/h_new) w_new = w h_new = factor 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 except: aw = 1