from itertools import zip_longest from tempfile import tempdir import firebase_admin from firebase_admin import credentials from firebase_admin import db from find_s1_image import find_img_value import pandas as pd from sklearn.metrics import classification_report, confusion_matrix, accuracy_score import statsmodels.api as sm import seaborn as sns sns.set() from zipfile import ZipFile from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split import csv import time import json from find_modis_ndvi import find_modis_ndvi import numpy as np from sklearn.cluster import KMeans from find_study_area_values import find_study_area_values from find_study_area_values3 import find_study_area_values3 from make_area_estimate_image import make_area_estimate_image from make_egypt_estimate_image import make_egypt_estimate_image from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection import traceback from firebase_admin import firestore from PIL import Image import numpy as np 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 from datetime import date from datetime import timedelta from datetime import datetime import requests import traceback from ftplib import FTP import paramiko import pysftp import math 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() from sentinelhub import SHConfig, MimeType, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, geo_utils def detect_barren(bbox, time_interval, output_dir, config=None): if config is None: # Configure SentinelHub with your credentials # config = SHConfig() # config.sh_client_id = '3e9277b3-aa11-4f98-a160-3bf006130cc7' # config.sh_client_secret = 'CY>imU7J,97EniGj5jyRtz:l13@XL2*gfY+pvqRw' config = SHConfig() # config.sh_client_id = '3e9277b3-aa11-4f98-a160-3bf006130cc7' # config.sh_client_secret = 'CY>imU7J,97EniGj5jyRtz:l13@XL2*gfY+pvqRw' new_client_id = '32a8119b-ff7b-4028-b6ce-85c3152cc49b' new_client_secret = 'oK23G4DolHSVxHYrAgFtx2VCd3cIUOYi' config.sh_client_id = new_client_id config.sh_client_secret = new_client_secret # Define the SentinelHub request parameters evalscript = """ let val = 2.5 * ((B11 + B04)-(B08 + B02))/((B11 + B04)+(B08 + B02)); return [2.5* val, B08, B11]; """ request = SentinelHubRequest( evalscript=evalscript, input_data=[ SentinelHubRequest.input_data( data_collection=DataCollection.SENTINEL2_L1C, time_interval=time_interval, maxcc=0.2 ) ], responses=[ SentinelHubRequest.output_response('default', MimeType.TIFF), ], bbox=bbox, config=config, ) # Fetch Sentinel imagery sentinel_data = request.get_data() sentinel_data = np.array(sentinel_data[0]) png_path = f"{output_dir}/barren.png" cv2.imwrite(png_path, sentinel_data) return png_path#, geojson_path # Example usage: def get_dates(): today = datetime.now() date_10_days_ago = today - timedelta(days=10) today_formatted = today.strftime("%Y%m%d") date_10_days_ago_formatted = date_10_days_ago.strftime("%Y%m%d") return str(today_formatted), str(date_10_days_ago_formatted) def upload_file(source_path, destination_path, expire_minutes=15): storage_client = storage.Client.from_service_account_json('servicekey.json') bucket_name = 'farmbase-b2f7e.appspot.com' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_path) storage_url = 'https://farmonaut.com/Images/data_not_found.jpg' try: blob.upload_from_filename(source_path) storage_url = blob.generate_signed_url(version="v4",expiration = timedelta(minutes=expire_minutes), method="GET") return storage_url except: print(traceback.format_exc()) def format_date(temp_date): new_date = temp_date[:4] + '-' + temp_date[4:6] + '-' + temp_date[6:] return new_date def main(): #storage_client = storage.Client() # req_obj = {} # req_obj["IsReportRequested"] = 1 # db_firestore.collection('ReportZipRequests').document('XSzwRsmzu4OeCm8UvllGn7xsnOk1').set(req_obj) zip_requests_obj = db_firestore.collection(u'ErosionRequests').where('Processed','==',0).get() for temp_d in zip_requests_obj: time_stamp = temp_d.id time_stamp = str(time_stamp) print(temp_d.id) print(temp_d.to_dict()) temp_main_obj = temp_d.to_dict() try: uid = temp_main_obj["UID"] fieldid = temp_main_obj["FieldID"] fieldid = str(fieldid) # from_date = temp_main_obj["FromDate"] # to_date = temp_main_obj["ToDate"] to_date, from_date = get_dates() area_type = temp_main_obj.get("LocationType", 0) field_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(fieldid).get() longitude_min, latitude_min, longitude_max, latitude_max = float(field_obj["FieldMinLong"]), float(field_obj["FieldMinLat"]), float(field_obj["FieldMaxLong"]), float(field_obj["FieldMaxLat"]) bbox = BBox([longitude_min, latitude_min, longitude_max, latitude_max], crs="EPSG:4326") time_interval = (format_date(str(from_date)), format_date(str(to_date))) output_directory = 'SFTP' png_path = detect_barren(bbox, time_interval, output_directory) url = upload_file((output_directory + '/barren.png'), ('PaidMonitoredFields/'+uid + '/' + fieldid + '/barren.png')) temp_main_obj["Processed"] = 1 temp_main_obj["URL"] = url db_firestore.collection(u'ErosionRequests').document(time_stamp).set(temp_main_obj) except: print(traceback.format_exc()) db_firestore.collection(u'ErosionRequests').document(time_stamp).delete() #upload_file_ftp(hostname, username, password, local_file_path, remote_directory) #upload_file_ftp(hostname, username, password, local_file_path, remote_directory) i = 0 while i == 0: main() time.sleep(30) # longitude_min, latitude_min, longitude_max, latitude_max =-50.768464422847735,-7.073172070815588,-50.33313117089461,-6.653230308928273 # print(result)