def map_coords(uid,fieldid,coordinates,imagedate,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong,crop_type, images_array, main_field_area): import math import matplotlib.path from matplotlib.path import Path import numpy as np import cv2 from PIL import Image from merge_contour_images import merge_contour_images from oct2py import octave import collections import traceback import firebase_admin from firebase_admin import credentials from firebase_admin import db from create_index_piechart import create_index_piechart from create_soc_piechart import create_soc_piechart from merge_mask_img import merge_mask_img from estimate_yield import estimate_yield # from firebase_admin import messaging cred = credentials.Certificate('servicekey.json') #firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) #fieldmaxlat = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldMaxLat').get() #fieldminlat = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldMinLat').get() #fieldmaxlong = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldMaxLong').get() #fieldminlong = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldMinLong').get() #coordinates = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Coordinates').get() coordinates = collections.OrderedDict(sorted(coordinates.items())) # wes1 = 1#print(coordinates) tci_file_name = uid+'/' + fieldid+'/TCI.png' tci = cv2.imread(tci_file_name,0) w_tci,h_tci = tci.shape centerx = round(w_tci/2) centery = round(h_tci/2) zero_lat = fieldmaxlat zero_long = fieldminlong wes1 = 1#print('coords') X = [] Y = [] pixelsperlat = h_tci/abs(float(fieldmaxlong)-float(fieldminlong)) pixelsperlong = w_tci/abs(float(fieldmaxlat)-float(fieldminlat)) pformatflag =0 for k,v in coordinates.items(): if k.find("P_")>=0: pformatflag = 1 break temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) x = round((temp_long-zero_long)*pixelsperlat) y = round((zero_lat-temp_lat)*pixelsperlong) # wes1 = 1#print(x) # wes1 = 1#print(y)lin X.append(x) Y.append(y) if pformatflag == 1: X = [] Y = [] ploc = 1 for k,v in coordinates.items(): pointKey = "P_" + str(ploc) ploc = ploc + 1 try: v = coordinates[pointKey] temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) x = round((temp_long-zero_long)*pixelsperlat) y = round((zero_lat-temp_lat)*pixelsperlong) # wes1 = 1#print(x) # wes1 = 1#print(y) X.append(x) Y.append(y) except: v = coordinates["a"] temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) x = round((temp_long-zero_long)*pixelsperlat) y = round((zero_lat-temp_lat)*pixelsperlong) # wes1 = 1#print(x) # wes1 = 1#print(y) X.append(x) Y.append(y) poly_m = octave.create_polymask(uid,fieldid,w_tci,h_tci,X,Y) indexValues = [] yieldIndex = [] for i in range(4): yieldIndex.append(i) wes1 = 1#print('images_array') wes1 = 1#print(images_array) for imageName in images_array: wes1 = 1#print(['imagename', imageName, imagedate, main_field_area]) try: if imageName == "soc": c2 = octave.calc_soc_indices(uid,fieldid,imageName, main_field_area) index_value,field_area =create_soc_piechart(uid,fieldid,imageName) else: c2 = octave.calc_indices2(uid,fieldid,imageName, main_field_area) index_value,field_area =create_index_piechart(uid,fieldid,imageName, imagedate) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Health').child(imageName).child(imagedate).set(str(index_value)) wes1 = 1#print((imageName + " is done")) indexValues.append(index_value) except: wes1 = 1#print(traceback.format_exc()) try: if imageName == 'ndvi': yieldIndex[0] = index_value if imageName == 'evi': yieldIndex[1] = index_value if imageName == 'savi': yieldIndex[2] = index_value if imageName == 'ndre': yieldIndex[3] = index_value except: wes1 = 1#print(traceback.format_exc()) aawq = 1 if imageName == 'tci': imageName == 'TCI' elif imageName == 'etci': imageName = 'ETCI' merge_mask_img(uid,imageName, fieldid) #wes1 = 1#print(yieldIndex) try: yield_est = estimate_yield(yieldIndex[0],yieldIndex[1],yieldIndex[2],yieldIndex[3],crop_type,field_area) except: yield_est = -1 #rvi_v,field_area =create_index_piechart(uid,fieldid,'rvi') try: field_area = int(field_area) except: field_area = 0 #db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('FieldArea').set(field_area) if yield_est > -1: db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Yield').child(imagedate).set(str(yield_est)) #db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Health').child('rvi').child(imagedate).set(str(rvi_v)) return field_area