from xxlimited import new import fiona import requests import utm from geopy.geocoders import Nominatim import time import json import pandas as pd import xlrd import matplotlib.path as mplPath import numpy as np 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 from oct2py import octave from io import BytesIO import base64 import os from shapely.geometry import Point from shapely.geometry.polygon import Polygon 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') new_ttg_json = 'Downloads/ttg_new.geojson' uid = 'mFFHQdEtiSbn2hbYQAwwoIdYVi02' ttg_fields = db.reference('PaidMonitoredFields').child('PMF').child(uid).get() for (fieldid, fieldobj) in ttg_fields.items(): # try: # ttg_data = fieldobj["TTGData"] # db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('TTGData').delete() # time.sleep(0.25) # except: # awa = 1 # farm_num = 0 # num = 0 # def try_lower(temp_str): # try: # temp_str = temp_str.lower() # except: # wew = 1 # return temp_str # with open(new_ttg_json) as f: # data = json.load(f) # all_farms_obj = data["features"] # for farm_obj in all_farms_obj: # farmGeometry = farm_obj["geometry"] # farmPropertiesObj = farm_obj["properties"] # farmName = farmPropertiesObj["name"] # farmName = try_lower(farmName) # farmPoints = farmGeometry["coordinates"] # mainFarmPoints = farmPoints # farmPoints = farmPoints[0] # farmPointsPath = Polygon(farmPoints) # intersection_num = 0 # tempObj = {} # tempObj["FieldAddress"] = farmName # villageName = farmName # allPointsObj = {} # allowed = 1 # pointNum = 0 # for i in mainFarmPoints: # for k in i: # singlePointobj = k # #print(singlePointobj) # singlePointLat = singlePointobj[1] # singlePointLong = singlePointobj[0] # #utmToLatLon = utm.to_latlon(singlePointLong, singlePointLat, 44, 'N') # utmToLatLon = [singlePointLat, singlePointLong] # tempPoint = {} # tempPoint["Latitude"] = utmToLatLon[0] # tempPoint["Longitude"] = utmToLatLon[1] # if pointNum > 0: # allPointsObj[("P_"+str(pointNum))] = tempPoint # else: # allPointsObj["a"] = tempPoint # geolocator = Nominatim(user_agent ="geoapiExercises") # location = geolocator.reverse(str(utmToLatLon[0])+"," + str(utmToLatLon[1])) # address_obj = location.raw['address'] # state = address_obj.get('state','') # temp_field_address = address_obj.get('suburb','') + ", " + address_obj.get('city','') + ", " + address_obj.get('state','') + ", " + address_obj.get('country','') # tempObj["FullAddress"] = villageName # tempObj["City"] = villageName # pointNum = pointNum + 1 # tempObj["Points"] = allPointsObj # tempObj["PaymentType"] = 12 # tempObj["CropCode"] = 999 # tempObj["Village"] = villageName # tempObj["UID"] = "mFFHQdEtiSbn2hbYQAwwoIdYVi02" # for (p,q) in ttg_fields.items(): # field_points = [] # singleFieldPointsObj = q["Coordinates"] # pointNum = 0 # multiplier = 1 # for (x,y) in singleFieldPointsObj.items(): # if pointNum == 0: # pointObj = singleFieldPointsObj["a"] # else: # pointObj = singleFieldPointsObj[("P_"+str(pointNum))] # pointNum = pointNum + 1 # field_points.append((multiplier*float(pointObj["Longitude"]),multiplier*float(pointObj["Latitude"]))) # #poly_path = Polygon(field_points) # poly_path = Polygon(field_points) # #print(field_points) # #print(poly_path) # p3 = poly_path.intersection(farmPointsPath) # if (p3.area/min(poly_path.area, farmPointsPath.area)) > 0.2: # #if poly_path.intersects(farmPointsPath): # intersection_num = 1 # break # if intersection_num == 0: # farm_num = farm_num + 1 # print('new_farm') # #print(tempObj) # r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/submitFieldTemp', json = tempObj) # time.sleep(0.5) # print(farm_num)