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 akhiyanafarms = 'Downloads/dhirajgadh.xlsx' akhiyana_geojson = 'Downloads/dhirajgadh.geojson' workbook_kmlfarms = xlrd.open_workbook(akhiyanafarms) sheet_kmlfarms = workbook_kmlfarms.sheet_by_index(0) uid = "XSzwRsmzu4OeCm8UvllGn7xsnOk1" with open(akhiyana_geojson) as f: akhiyana_json = json.load(f) akhiyana_farms = akhiyana_json["features"] survey_obj = {} for i in akhiyana_farms: single_farm = i single_farm_points = single_farm["geometry"]["coordinates"] single_farm_survey_no = single_farm["properties"]["sno"] try: single_farm_survey_no = int(single_farm_survey_no) except: aaq = 1 survey_obj[single_farm_survey_no] = i total_area =0 num = 0 for i in range(1,sheet_kmlfarms.nrows): farmer_name = sheet_kmlfarms.cell_value(i,1) farm_area = sheet_kmlfarms.cell_value(i,4) farm_area = float(farm_area)*10000 try: survey_no = int(sheet_kmlfarms.cell_value(i,3)) except: survey_no = "NA" try: main_obj = survey_obj[survey_no] total_area = total_area + farm_area num = num + 1 #print(main_obj) polygonPoints = main_obj["geometry"]["coordinates"] lats, lngs = [],[] allPointsObj = {} tempObj = {} pointNum = 0 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 else: allPointsObj["a"] = tempPoint temp_field_address = "Ramagri - " + farmer_name + " - Survey No: " + str(survey_no) tempObj["FullAddress"] = temp_field_address tempObj["FieldAddress"] = temp_field_address pointNum = pointNum+1 tempObj["Points"] = allPointsObj tempObj["UID"] = uid tempObj["PaymentType"] = 6 tempObj["CropCode"] = 999 tempObj["Village"] = temp_field_address tempObj["City"] = temp_field_address tempObj["Area"] = farm_area print(tempObj) r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/submitFieldTemp2', json = tempObj) time.sleep(1) except Exception as e: print(e) print((total_area/10000)) print(num)