import fiona import requests import utm from geopy.geocoders import Nominatim import time import json import pandas as pd from fastkml import kml filepath = 'Downloads/json_files/style.json' 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