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 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 = {} for i in range(1,sheet_amrofarms.nrows): farmer_code = sheet_amrofarms.cell_value(i,4) farmer_code = try_lower(farmer_code) #print(amro_obj) for i in range(1, sheet_kmlfarms.nrows): single_farmer_obj = {} farmer_code = sheet_kmlfarms.cell_value(i,4) farmer_code = try_lower(farmer_code) farmer_coordinates = sheet_kmlfarms.cell_value(i,28) #print(farmer_coordinates) farmer_name = sheet_kmlfarms.cell_value(i,5) farm_area = sheet_kmlfarms.cell_value(i,20) #print(farm_area) try: farm_area = float(farm_area)*10000/4.047 farm_area = int(farm_area) except: farm_area = farm_area try: farmer_amro_obj = amro_obj[farmer_code] except: farmer_amro_obj = {} farmer_amro_obj["zone"] = "na" farmer_amro_obj["amro"] = "na" farmer_amro_obj["store"] = "na" farmer_amro_obj["mobile"] = "na" farmer_amro_obj["village"] = "na" farmer_amro_obj["taluka"] = "na" farmer_amro_obj["district"] = "na" farmer_amro_obj["state"] = "na" farmer_info_obj = {} single_farmer_obj["zone"] = farmer_amro_obj["zone"] single_farmer_obj["amro"] = farmer_amro_obj["amro"] single_farmer_obj["store"] = farmer_amro_obj["store"] single_farmer_obj["mobile"] = farmer_amro_obj["mobile"] single_farmer_obj["village"] = farmer_amro_obj["village"] single_farmer_obj["taluka"] = farmer_amro_obj["taluka"] single_farmer_obj["district"] = farmer_amro_obj["district"] single_farmer_obj["state"] = farmer_amro_obj["state"] farmer_info_obj["zone"] = farmer_amro_obj["zone"] farmer_info_obj["amro"] = farmer_amro_obj["amro"] farmer_info_obj["store"] = farmer_amro_obj["store"] farmer_info_obj["PhoneNumber"] = farmer_amro_obj["mobile"] farmer_info_obj["village"] = farmer_amro_obj["village"] farmer_info_obj["taluka"] = farmer_amro_obj["taluka"] farmer_info_obj["district"] = farmer_amro_obj["district"] farmer_info_obj["state"] = farmer_amro_obj["state"] farmer_info_obj["FarmerCode"] = farmer_code try: polygonPoints = json.loads(farmer_coordinates) except: polygonPoints = {} #print(polygonPoints) allPointsObj = {} pointNum = 0 tempObj = {} lats = [] lngs = [] #try: for aa in polygonPoints: for k in aa: singlePointobj = k #print(singlePointobj) singlePointLat = singlePointobj[1] singlePointLong = singlePointobj[0] if (singlePointLat in lats) and (singlePointLong in lngs): trwr = 1 else: lats.append(singlePointLat) lngs.append(singlePointLong) #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 temp_field_address = str(single_farmer_obj["village"]) + ', ' + str(single_farmer_obj["taluka"]) + ', ' + str(single_farmer_obj["district"]) + ', ' + str(single_farmer_obj["state"]) tempObj["FullAddress"] = temp_field_address tempObj["City"] = single_farmer_obj["village"] pointNum = pointNum+1 try: min_lat, max_lat, min_lng, max_lng = min(lats), max(lats), min(lngs), max(lngs) except: min_lat, max_lat, min_lng, max_lng = 0,0,0,0 try: median_lng = median(lngs) diagonal = 2*math.sqrt(farm_area) diagonal_degree = diagonal*0.0002 new_lats = [] new_lngs = [] for rrt in range(0,len(lngs)): temp_lng = lngs[rrt] if abs(temp_lng-median_lng) < diagonal_degree: new_lats.append(lats[rrt]) new_lngs.append(lngs[rrt]) lats = new_lats lngs = new_lngs allPointsObj = {} for wqq in range(0,len(lats)): tempPoint = {} tempPoint["Latitude"] = lats[wqq] tempPoint["Longitude"] = lngs[wqq] pointKey = "P_" + str(wqq) allPointsObj[pointKey] = tempPoint tempObj["Coordinates"] = allPointsObj tempObj["Name"] = farmer_name tempObj["FarmerInfoData"] = farmer_info_obj tempObj["Area"] = farm_area tempObj["PhoneNumber"] = farmer_amro_obj["mobile"] tempObj["UID"] = "snQYQZqQx3SmVbRztmEqYn5Mkcz2" if farm_area > 1020 and farm_area< 4046: r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/findHNIVillage', json = tempObj) time.sleep(1) except: gshshs = 1 # # # if farmer_amro_obj["zone"] != "na": # print(tempObj) # break # except: # qq = 1 # break # mainObj = {} # mainArr = [] # polygonNum = 0 # rr = 1 # with open(filepath) as f: # data = json.load(f) # allVillagesObj = data["features"] # for villageObj in allVillagesObj: # villagePropertiesObj = villageObj["properties"] # villageGeometry = villageObj["geometry"] # polygonPoints = villageGeometry["coordinates"] # villagePropertiesObj["coordinates"] = polygonPoints # mainArr.append(villagePropertiesObj) # with open('dataa.json','w') as f2: # json.dump(mainArr,f2) # df_json = pd.read_json('dataa.json') # df_json.to_excel('farms.xlsx') # for villageObj in allVillagesObj: # villagePropertiesObj = villageObj["properties"] # farmerName = villagePropertiesObj["FarmerName"] # farmerCode = villagePropertiesObj["FarmerCode"] # farmerCrop = villagePropertiesObj["Crop"] # villageGeometry = villageObj["geometry"] # polygonPoints = villageGeometry["coordinates"] # fieldArea = villagePropertiesObj["Area"] # acreArea = float(fieldArea) # if acreArea >= 1 and acreArea <=50: # polygonNum = polygonNum+1 # print(polygonNum) # # fieldArea = int(fieldArea)*4047 # # villageTown = villagePropertiesObj["PlotOwner"] # # farmerInfoObj = {} # # farmerInfoObj["FarmerCode"] = farmerCode # # tempObj = {} # # tempObj["Area"] = fieldArea # # pointNum = 0 # # allPointsObj = {} # # allowed = 1 # # lats = [] # # lngs = [] # # try: # # for i in polygonPoints: # # for k in i: # # singlePointobj = k # # #print(singlePointobj) # # singlePointLat = singlePointobj[1] # # singlePointLong = singlePointobj[0] # # lats.append(singlePointLat) # # lngs.append(singlePointLong) # # #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"] = temp_field_address # # tempObj["City"] = villageTown # # pointNum = pointNum+1 # # tempObj["Points"] = allPointsObj # # tempObj["Nama"] = farmerName # # tempObj["FarmerInfoData"] = farmerInfoObj # # tempObj["UID"] = "snQYQZqQx3SmVbRztmEqYn5Mkcz2" # # if (max(lats) - min(lats) < 0.01) and (max(lngs)-min(lngs)<0.01): # # r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/findHNIVillage', json = tempObj) # # time.sleep(2) # # except: # # wqwq = 1