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 from make_uprooting_farm_image import make_uprooting_farm_image from get_uprooting_json import get_uprooting_json 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_time(): import time obj = time.gmtime(0) epoch = time.asctime(obj) #print("The epoch is:",epoch) curr_time = round(time.time()*1000) return curr_time 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 4: # break start_time = get_time() uid, imageType = 'TCXcp5VIsfhHZrh0nm2VsgBtcGy2', 'TCI' points_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Polygons').child(polygonid).child('Coordinates').get() day_gap = 5 satellite, INSTANCE_ID = DataCollection.SENTINEL2_L1C, wmsID c_map = 1 #from_date, to_date = get_from_to_dates(imageDate, day_gap) try: age = float(uprooting_json[matched_num]["Age"]) except: age = 3 if float(age) < 1.5: from_date, to_date = '2022-01-01T00:00:00', '2023-04-02T23:59:59' else: from_date, to_date = '2021-01-01T00:00:00', '2023-04-02T23:59:59' bounds = get_bounds(points_obj) # if imageType == 'NDVI': # imageType = 'H-NDVI' main_arr = make_uprooting_farm_image(c_map, uid, fieldid, polygonid,satellite, imageType,get_bounds(points_obj), from_date, to_date,clientID,clientSecret,INSTANCE_ID, points_obj) #single_farm uprooting_json[matched_num]["UprootingDates"] = main_arr uprooting_json[matched_num]["FieldID"] = fieldid uprooting_json[matched_num]["PolygonID"] = polygonid end_time = get_time() time_elapsed = (end_time-start_time)/1000 print(('Time Taken: ' + str(time_elapsed))) except: print(traceback.format_exc()) # Serializing json json_object = json.dumps(uprooting_json, indent=4) # Writing to sample.json with open("new_uprooting.json", "w") as outfile: outfile.write(json_object) except: print(traceback.format_exc()) json_object = json.dumps(uprooting_json, indent=4) # Writing to sample.json with open("new_uprooting.json", "w") as outfile: outfile.write(json_object) # db_firestore.collection(u'PolygonDataRequests').document(temp_d.id).delete() # except Exception as e: # print(traceback.format_exc()) #time.sleep(10)