import requests import utm from geopy.geocoders import Nominatim import time import json filepath = 'Downloads/ka.geojson' mainObj = {} polygonNum = 0 rr = 1 with open(filepath) as f: data = json.load(f) allVillagesObj = data["features"] for villageObj in allVillagesObj: #villageObj = shape.next() #print(villageObj) villagePropertiesObj = villageObj["properties"] villageName = villagePropertiesObj["NAME"] villageTown = villagePropertiesObj["DISTRICT"] try: villageTown = villageTown.lower() except: villageTown = 'unavailable' try: villageName = villageName.lower() except: villageName = 'unavailable' villageGeometry = villageObj["geometry"] polygonPoints = villageGeometry["coordinates"] try: fieldArea = int(villagePropertiesObj["AREA"]) except: fieldArea = 0 fieldArea = int(fieldArea)/10000 tempObj = {} tempObj["Area"] = fieldArea pointNum = 0 allPointsObj = {} allowed = 1 try: for i in polygonPoints: for k in i: singlePointobj = k #print(singlePointobj) singlePointLat = singlePointobj[1] singlePointLong = singlePointobj[0] #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["PaymentType"] = 12 tempObj["CropCode"] = 999 tempObj["Village"] = villageName tempObj["UID"] = "snQYQZqQx3SmVbRztmEqYn5Mkcz2" polygonNum = polygonNum + 1 substring1 = "forest" substring2 = "reserve" substring3 = "water body" substring4 = "village point" if (substring1 in villageName) or (substring2 in villageName) or (villageName == "hill") or (substring4 in villageName): print("not a village") r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/submitFieldTemp2', json = tempObj) time.sleep(0.3) else: aq = 1 #if villageTown == "krishnagiri": #print(tempObj) except: print('village error')