import os dir_path = 'TTG/' import fiona import requests import utm from geopy.geocoders import Nominatim import time import json for path in os.listdir(dir_path): if os.path.isfile(os.path.join(dir_path,path)): filepath = 'TTG/'+ path 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"] villageID = villageObj["id"] try: villageName = villageName.lower() except: villageName = 'unavailable' #print(villageName) villageGeometry = villageObj["geometry"] polygonPoints = villageGeometry["coordinates"] villageName = villageName + str(villageID) # fieldArea = villagePropertiesObj["Shape_Area"] # fieldArea = int(fieldArea)/10000 tempObj = {} tempObj["FieldAddress"] = villageName #print(tempObj["FieldAddress"]) # 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"] = villageName tempObj["City"] = villageName pointNum = pointNum+1 tempObj["Points"] = allPointsObj tempObj["PaymentType"] = 12 tempObj["CropCode"] = 999 tempObj["Village"] = villageName tempObj["UID"] = "mFFHQdEtiSbn2hbYQAwwoIdYVi02" r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/submitFieldTemp', json = tempObj) time.sleep(1) except: print('village error')