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 kmlfarms = 'farms.xlsx' amrofilepath = 'amro.xlsx' def new_points2(main_lat,main_lng,farm_area): new_lats = [] new_lngs = [] if main_lat != None and main_lng != None: try: #median_lng = median(lngs) diagonal = 1.42*math.sqrt(farm_area) diagonal_degree = diagonal*0.000009 temp_min_lat = main_lat - diagonal_degree temp_max_lat = main_lat + diagonal_degree temp_min_lng = main_lng - diagonal_degree temp_max_lng = main_lng + diagonal_degree new_lats.append(temp_min_lat) new_lats.append(temp_max_lat) new_lats.append(temp_max_lat) new_lats.append(temp_min_lat) new_lngs.append(temp_min_lng) new_lngs.append(temp_min_lng) new_lngs.append(temp_max_lng) new_lngs.append(temp_max_lng) return new_lats,new_lngs except Exception as e: print(('a1' + str(e))) return new_lats, new_lngs else: return new_lats, new_lngs def remove_outliers(lats,lngs): 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]) return new_lats, new_lngs 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 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) farm_area2 = sheet_kmlfarms.cell_value(i,18) main_lat = sheet_kmlfarms.cell_value(i,12) main_lng = sheet_kmlfarms.cell_value(i,13) try: main_lat = float(main_lat) main_lng = float(main_lng) except: main_lat = None main_lng = None ##print(farm_area) try: farm_area = float(farm_area)*4047 farm_area = int(farm_area) farm_area2 = float(farm_area2)*4047 farm_area2 = int(farm_area2) except Exception as e: #print(('a3' +str(e))) #print(farm_area) farm_area = 0 try: farm_area2 = float(farm_area2)*4047 farm_area2 = int(farm_area2) except Exception as e: #print(('a7' +str(e))) #print(farm_area2) farm_area2 = 0 try: farmer_amro_obj = amro_obj[farmer_code] except Exception as e: #print(('a4' +str(e))) 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 Exception as e: #print(('a5' +str(e))) #print(farmer_coordinates) 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) 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 main_area = farm_area if main_area == 0: main_area = 8100 try: lats,lngs = remove_outliers(lats,lngs) except Exception as e: print(e) plot_num = plot_num +1 lats,lngs = new_points2(main_lat, main_lng, main_area) print(plot_num) # try: # if (int(farm_area) < 1000 or int(farm_area) > 202350) or (int(farm_area2) < 1000 or int(farm_area2) > 202350): # main_area = farm_area # if int(farm_area) > 1000 and int(farm_area) < 202350: # main_area = farm_area # if int(farm_area2) > 1000 and int(farm_area2) < 202350: # main_area = farm_area2 # if main_area == 0: # main_area = 8100 # lats,lngs = new_points2(main_lat, main_lng, main_area) # ###print(lats, lngs) # except Exception as e: # #print(('a6' +str(e))) # err_num = err_num +1 if len(lats)>3: 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"] = main_area tempObj["PhoneNumber"] = farmer_amro_obj["mobile"] tempObj["UID"] = "snQYQZqQx3SmVbRztmEqYn5Mkcz2" #print(tempObj) # try: # #if (int(farm_area) < 1100 or int(farm_area) > 202350) or (int(farm_area2) < 1100 or int(farm_area2) > 202350): # r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/findHNIVillage', json = tempObj) # plot_num = plot_num +1 # #print(plot_num) # time.sleep(0.4) # #break # except: # gshshs = 1 else: plot_num = plot_num+1 #print(plot_num) print(plot_num)