import fiona import requests import utm from geopy.geocoders import Nominatim import time import json import pandas as pd from fastkml import kml import xlrd from statistics import median import math import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import threading import xlsxwriter import numpy as np import datetime import os from numba import jit, cuda from firebase_admin import firestore from make_on_the_go_farm_image import make_on_the_go_farm_image from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection from get_polygon_mask import get_polygon_mask import traceback from google.cloud import storage import os from google.oauth2 import service_account import datetime from datetime import date storage_client = storage.Client.from_service_account_json("servicekey.json"); bucket_name = 'farmbase-b2f7e.appspot.com' cred = service_account.Credentials.from_service_account_file('servicekey.json') #cred = credentials.Certificate('servicekey.json') bucket = storage_client.bucket(bucket_name) try: firebase_admin.initialize_app(credentials.Certificate('servicekey.json'), {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') db_firestore = firestore.client() sentinelSettings = db.reference('SentinelSettings').get() clientID = sentinelSettings["ClientID"] clientSecret = sentinelSettings["ClientSecret"] wmsID = sentinelSettings["WMSID"] rviID = sentinelSettings["RVIID"] demID = sentinelSettings["DEMID"] def get_bounds(points_obj): lats, lngs = [], [] for (key, value) in points_obj.items(): lats.append(value["Latitude"]) lngs.append(value["Longitude"]) bounds = [min(lngs), min(lats), max(lngs), max(lats)] return bounds def get_from_to_dates(imageDate, day_gap): current_month = int(imageDate[4:6]) current_year = int(imageDate[0:4]) current_date = int(imageDate[6:]) latestsensedday = 0 start_year = current_year if current_date < day_gap and current_month>1: if latestsensedday == 0: start_date = abs(31-current_date - day_gap) else: start_date = abs(31-current_date -(day_gap - 1)) start_month = current_month-1 start_year = current_year elif current_date 1672511400000: # for i in range(0,42): # timestamp = int(timestamp) + 1500 # new_obj[timestamp] = main_obj # num = num + 1 # except: # aaq = 1 # print(num) # # Serializing json # json_object = json.dumps(new_obj, indent=4) # # Writing to sample.json # with open("sample_cor.json", "w") as outfile: # outfile.write(json_object) ii = 0 while ii == 0: try: data_requests_obj = db_firestore.collection(u'PolygonDataRequests').where('URL','==','').get() for temp_d in data_requests_obj: try: timestamp = temp_d.id main_obj = temp_d.to_dict() #new_obj[timestamp] = main_obj # for (timestamp, main_obj) in data_requests_obj: uid, fieldid, polygonid, imageType = main_obj["UID"], main_obj["FieldID"], main_obj["PolygonID"], main_obj["ImageType"] points_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Polygons').child(polygonid).child('Coordinates').get() day_gap = 5 try: imageDate = main_obj["ImageDate"] except Exception as e: today = date.today() imageDate = today.strftime('%Y%m%d') if imageType == 'EVAPO': imageType = 'EVAPOTRANSPIRATION' if imageType == 'RVI': satellite, INSTANCE_ID = DataCollection.SENTINEL1_IW, rviID day_gap = 20 elif imageType == 'DEM': satellite, INSTANCE_ID = DataCollection.DEM_MAPZEN, demID # elif imageType == 'NDVI': # satellite, INSTANCE_ID = DataCollection.HARMONIZED_LANDSAT_SENTINEL, wmsID else: satellite, INSTANCE_ID = DataCollection.SENTINEL2_L1C, wmsID try: c_map = int(main_obj["Colormap"]) except: c_map = 1 from_date, to_date = get_from_to_dates(imageDate, day_gap) bounds = get_bounds(points_obj) # if imageType == 'NDVI': # imageType = 'H-NDVI' tempdate, val = make_on_the_go_farm_image(c_map, uid, fieldid, polygonid,satellite, imageType,get_bounds(points_obj), from_date, to_date,clientID,clientSecret,INSTANCE_ID) print([timestamp, imageDate, from_date, to_date, tempdate]) imageDate = tempdate.replace("-","") get_polygon_mask(uid,polygonid,imageType,points_obj,bounds[3],bounds[1],bounds[2],bounds[0]) bucket = storage_client.get_bucket(bucket_name) destination = 'PaidMonitoredFieldsPolygons/' + uid + '/' + fieldid + '/' + polygonid + '/' + imageDate + '/' + imageType + str(c_map) blob = bucket.blob(destination) if c_map ==1: file_name = uid + "/" +polygonid + "_" + imageType + ".png" else: file_name = uid + "/" +polygonid + "_" + imageType + "_CMAP2.png" blob.upload_from_filename(file_name) url = make_url(blob) db_firestore.collection(u'PolygonDataRequests').document(timestamp).update({u'URL': url}) db_firestore.collection(u'PolygonDataRequests').document(timestamp).update({u'ImageDate': imageDate}) print(url) except: print(traceback.format_exc()) db_firestore.collection(u'PolygonDataRequests').document(temp_d.id).delete() except Exception as e: print(traceback.format_exc()) time.sleep(10)