import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import time import threading import pandas as pd import os import datetime from zipfile import ZipFile import json from numba import jit, cuda from google.cloud import storage import os from firebase_admin import db from get_mask import get_mask import cv2 import scipy.ndimage from google.oauth2 import service_account import datetime import traceback from datetime import date storage_client = storage.Client.from_service_account_json("servicekey.json"); bucket_name = 'farmbase-b2f7e.appspot.com' cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') uid= 'TCXcp5VIsfhHZrh0nm2VsgBtcGy2' #server2022('snQYQZqQx3SmVbRztmEqYn5Mkcz2', None) gavl_list = db.reference('PaidMonitoredFields').child('PMF').child(uid).get(False,True) #gavl_list = db.reference('PaidMonitoredFields').child('PMF').order_by_child('FieldArea').limit_to_first(2).get() import psycopg2 from gavl_postgis import addPolygonToPostGIS #Establishing the connection conn = psycopg2.connect( database="gavl-postgis", user='postgres', password='P7^6fL9Q}h2v$UvA', host='34.68.167.51', port= '5432' ) #Setting auto commit false conn.autocommit = True #Creating a cursor object using the cursor() method cursor = conn.cursor() #Executing an MYSQL function using the execute() method cursor.execute("select version()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print("Connection established to: ",data) def getVal(key, obj): try: val = obj[key] except: val = 'None' return val mainObj =[] json_obj = {} json_obj["type"] = "FeatureCollection" json_obj["features"] = [] fieldids = ['1611730411677','1611730551186','1611730664187','1611730834639','1611731010687','1611731098888','1611731265986','1611731498286','1611731614762','1611731704188','1611731797145','1611745120936'] #cursor, conn = None, None #pending_list = db.reference('PaidMonitoredFields').child('PMF').child(uid).get(False, True) num = 0 polygon_id_arr = db.reference('TempGAVLIDs').get() # for (fieldid,temp_obj) in pending_list.items(): # # p = str(p) # # fieldObj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(p).get() # # for fieldid in fieldids: # #for (fieldid, temp) in gavl_list.items(): # #fieldobj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).get() # try: polygonsObj = db.reference('PaidPolygonsByPolygonID').child('PMF').child(uid).get() #polygonsObj = fieldobj["Polygons"] for(polygonid, polygonObj) in polygonsObj.items(): if int(polygonid) not in polygon_id_arr: try: tempPropertiesObj = {} # geotagging_date, uid, fieldid, polygonid, name, phone_number, crop, season, area, added_by_phone_number, added_by_uid, points_arr #geotagging_date = str(datetime.date.fromtimestamp(float(getVal("polygonID", polygonObj)) / 1000)) # Apply fromtimestamp function print(polygonid) geotagging_date = str(datetime.date.fromtimestamp(float(polygonid) / 1000)) # Apply fromtimestamp function name = str(polygonObj.get("Name","-")) phone_number = str(polygonObj.get("PhoneNumber","-")) crop = str(polygonObj.get("Crop","-")) season = str(polygonObj.get("Season","-")) area = str(polygonObj.get("Area","-")) added_by_phone_number = str(polygonObj.get("AddedByPhoneNumber","-")) added_by_uid = str(polygonObj.get("AddedByUID","-")) points_obj = polygonObj.get("Coordinates","-") fieldid = polygonObj.get("fieldID","-") points_arr = [] num = num + 1 points_num = 0 for (p,q) in points_obj.items(): single_point_obj = points_obj[("P_"+str(points_num))] points_arr.append([single_point_obj["Longitude"], single_point_obj["Latitude"]]) points_num = points_num +1 #points_arr = str(points_arr) addPolygonToPostGIS(cursor, conn, geotagging_date, uid, fieldid, polygonid, name, phone_number, crop, season, area, added_by_phone_number, added_by_uid, points_arr) time.sleep(0.1) #break except: print(traceback.format_exc()) #break print(num)