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 workbook_kmlfarms = xlrd.open_workbook(kmlfarms) sheet_kmlfarms = workbook_kmlfarms.sheet_by_index(0) workbook_amrofarms = xlrd.open_workbook(amrofilepath) sheet_amrofarms = workbook_amrofarms.sheet_by_index(0) amro_obj = {} plot_num =0 err_num = 0 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 return newObj for i in range(1,sheet_amrofarms.nrows): farmer_code = sheet_amrofarms.cell_value(i,4) farmer_declared_area = sheet_amrofarms.cell_value(i,23) farmer_variety = sheet_amrofarms.cell_value(i,17) farmer_variety = try_lower(farmer_variety) farmer_crop = sheet_amrofarms.cell_value(i,16) farmer_crop = try_lower(farmer_crop) farmer_code = try_lower(farmer_code) tempObj = {} tempObj["Area"] = farmer_declared_area tempObj["Variety"] = farmer_variety tempObj["Crop"] = farmer_crop amro_obj[farmer_code] = tempObj 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/findHNIVillage', json = getRequestObj(pointsObj, uid)) time.sleep(1) mgrs = getMGRS(pointsObj) try: farmerInfoDataObj = polygonobj["FarmerInfoData"] farmer_code = farmerInfoDataObj["FarmerCode"] farmerInfoDataObj["variety"] = amro_obj[farmer_code]["Variety"] farmerInfoDataObj["declaredArea"] = amro_obj[farmer_code]["Area"] farmerInfoDataObj["crop"] = amro_obj[farmer_code]["Crop"] db.reference('PaidPolygonsByPolygonID').child('PMF').child(uid).child(polygonid).child("FarmerInfoData").set(farmerInfoDataObj) time.sleep(0.01) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child("Polygons").child(polygonid).child("FarmerInfoData").set(farmerInfoDataObj) time.sleep(0.01) db.reference('PaidMGRSPolygons').child(mgrs).child(polygonid).child("FarmerInfoData").set(farmerInfoDataObj) time.sleep(0.01) #print(farmerInfoDataObj) #num = num+1 except Exception as e: try: farmer_code = polygonobj["farmerCode"] farmerInfoDataObj["variety"] = amro_obj[farmer_code]["Variety"] farmerInfoDataObj["declaredArea"] = amro_obj[farmer_code]["Area"] farmerInfoDataObj["crop"] = amro_obj[farmer_code]["Crop"] #print(farmerInfoDataObj) #num = num + 1 except Exception as e: try: farmer_code = polygonobj["FarmerCode"] farmerInfoDataObj["variety"] = amro_obj[farmer_code]["Variety"] farmerInfoDataObj["declaredArea"] = amro_obj[farmer_code]["Area"] farmerInfoDataObj["crop"] = amro_obj[farmer_code]["Crop"] #print(farmerInfoDataObj) #num = num + 1 except Exception as e: #print(e) awa = 1 print(num)