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 kmlfarms = 'farms.xlsx' amrofilepath = 'amro.xlsx' def get_area_ratio(new_area, farm_area): try: area_ratio = int(new_area)/int(farm_area) except: area_ratio = 0 return area_ratio def reproject(latitude, longitude): """Returns the x & y coordinates in meters using a sinusoidal projection""" from math import pi, cos, radians earth_radius = 6371009 # in meters lat_dist = pi * earth_radius / 180.0 X = [] Y = [] for i in range(0,len(latitude)): lat = latitude[i] lng = longitude[i] y = lat*lat_dist x = lng*lat_dist*cos(radians(lat)) X.append(x) Y.append(y) # y = [lat * lat_dist for lat in latitude] # x = [long * lat_dist * cos(radians(lat)) # for lat, long in zip(latitude, longitude)] # print(X) # print(Y) return X, Y def new_points(lat, lng, tempArea): new_points_arr = [] side = sqrt(tempArea) side_in_deg = side*0.00001/(2*1.11) new_points_arr.push((lat - side_in_deg), (lng + side_in_deg)) new_points_arr.push((lat + side_in_deg), (lng + side_in_deg)) new_points_arr.push((lat + side_in_deg), (lng - side_in_deg)) new_points_arr.push((lat - side_in_deg), (lng - side_in_deg)) return new_points_arr def area_of_polygon(x, y): """Calculates the area of an arbitrary polygon given its verticies""" area = 0.0 for i in range(-1, len(x)-1): area += x[i] * (y[i+1] - y[i-1]) return round(abs(area) / (2.0)) 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_zone = sheet_amrofarms.cell_value(i,1) farmer_amro = sheet_amrofarms.cell_value(i,2) farmer_store = sheet_amrofarms.cell_value(i,3) farmer_code = sheet_amrofarms.cell_value(i,4) farmer_mobile = sheet_amrofarms.cell_value(i,7) farmer_village = sheet_amrofarms.cell_value(i,8) farmer_taluka = sheet_amrofarms.cell_value(i,9) farmer_district = sheet_amrofarms.cell_value(i,10) farmer_state = sheet_amrofarms.cell_value(i,11) single_farmer_obj = {} farmer_code = try_lower(farmer_code) single_farmer_obj["zone"] = try_lower(farmer_zone) single_farmer_obj["amro"] = try_lower(farmer_amro) single_farmer_obj["store"] = try_lower(farmer_store) single_farmer_obj["mobile"] = try_lower(farmer_mobile) single_farmer_obj["village"] = try_lower(farmer_village) single_farmer_obj["taluka"] = try_lower(farmer_taluka) single_farmer_obj["district"] = try_lower(farmer_district) single_farmer_obj["state"] = try_lower(farmer_state) amro_obj[farmer_code] = single_farmer_obj #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