import fiona import requests import utm from geopy.geocoders import Nominatim import time shape = fiona.open(('Downloads/india-village-census-2001-AP.shp')) mainObj = {} polygonNum = 0 rr = 1 while rr == 1: villageObj = shape.next() #print(villageObj) villagePropertiesObj = villageObj["properties"] villageName = villagePropertiesObj["NAME"] villageTown = villagePropertiesObj["NEAR_TOWN"] try: villageTown = villageTown.lower() except: villageTown = 'random' villageGeometry = villageObj["geometry"] polygonPoints = villageGeometry["coordinates"] fieldArea = villagePropertiesObj["AREA"] fieldArea = int(fieldArea)*10000 tempObj = {} tempObj["Area"] = fieldArea pointNum = 0 allPointsObj = {} allowed = 0 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') 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"] = address_obj.get('city','') allowed = 1 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 allowed == 1: # print(tempObj) # r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/submitFieldTemp', json = tempObj) # time.sleep(5) # break # break except: qwer = 1