import fiona import requests import utm from geopy.geocoders import Nominatim import time import firebase_admin from firebase_admin import credentials from firebase_admin import firestore import threading import os cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') db = firestore.client() 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 = 'unavailable' try: villageName = villageName.lower() except: villageName = 'unavailable' villageGeometry = villageObj["geometry"] polygonPoints = villageGeometry["coordinates"] fieldArea = villagePropertiesObj["AREA"] 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') 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" amroRef = db.collection("coromandel").where("village","==",villageName).where("district","==",villageTown) for (p,q) in amroRef.items(): polygonNum = polygonNum + 1 if allowed == 1: print(tempObj) r = requests.post('https://us-central1-farmbase-b2f7e.cloudfunctions.net/submitFieldTemp2', json = tempObj) time.sleep(1) break break except: qwer = 1 break