from xxlimited import new import fiona import requests import utm from geopy.geocoders import Nominatim import time import json import pandas as pd from fastkml import kml import xlrd from statistics import median import math import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import threading import xlsxwriter import numpy as np 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/'}) except: print('fire running') kmlfarms = 'farms.xlsx' amrofilepath = 'amro.xlsx' def try_lower(temp_str): try: temp_str = temp_str.lower() except: wew = 1 return temp_str def getMGRS(pointsObj): lats = [] lngs = [] for (p,q) in pointsObj.items(): lats.append(q["Latitude"]) lngs.append(q["Longitude"]) mean_lat = np.mean(lats) mean_lng = np.mean(lngs) mgrs = str(round(10*mean_lat)) + "_" + str(round(10*mean_lng)) #print(mgrs) return mgrs uid = 'snQYQZqQx3SmVbRztmEqYn5Mkcz2' mur_polygons = db.reference('PaidPolygonsByPolygonID').child('PMF').child(uid).get() def getRequestObj(pointsObj, uid): newObj = {} newPointsObj = {} for (key, singlePoint) in pointsObj.items(): new_key = key if key == "P_0": new_key = "a" newPointsObj[new_key] = singlePoint newObj["Points"] = newPointsObj newObj["UID"] = uid #print(newObj) return newObj num = 0 for (polygonid, polygonobj) in mur_polygons.items(): fieldid = polygonobj["fieldID"] pointsObj = polygonobj["Coordinates"] r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/getFieldAreaByBoundaryPoints', json = getRequestObj(pointsObj, uid)) dataObj = r.json() try: new_area = dataObj["FieldArea"] polygonobj["Area"] = new_area print(polygonid) print(new_area) time.sleep(0.25) break # mgrs = getMGRS(pointsObj) # print(new_area) # #try: # db.reference('PaidPolygonsByPolygonID').child('PMF').child(uid).child(polygonid).set(polygonobj) # time.sleep(0.01) # db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child("Polygons").child(polygonid).set(polygonobj) # time.sleep(0.01) # db.reference('PaidMGRSPolygons').child(mgrs).child(polygonid).set(polygonobj) # time.sleep(0.01) #print(farmerInfoDataObj) #num = num+1 except Exception as e: print(e) print(num)