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 segment_sample import get_main_segment 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 haversine(lat1, lon1, lat2, lon2): # Radius of the Earth in kilometers R = 6371.0 # Convert latitude and longitude from degrees to radians lat1 = math.radians(lat1) lon1 = math.radians(lon1) lat2 = math.radians(lat2) lon2 = math.radians(lon2) # Haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) distance = R * c return distance def area_from_bbox(bbox): # Check if the bbox is a list of 4 coordinates [longitude_min, latitude_min, longitude_max, latitude_max] if len(bbox) != 4: raise ValueError("Bounding box should contain 4 coordinates [longitude_min, latitude_min, longitude_max, latitude_max]") lon_min, lat_min, lon_max, lat_max = bbox # Calculate the distances between the four corners of the bbox side1 = haversine(lat_min, lon_min, lat_max, lon_min) side2 = haversine(lat_min, lon_min, lat_min, lon_max) # Calculate the area area = side1 * side2 return area def fraction_of_color(image, target_color=(0, 0, 255, 255)): # Open the image using Pillow with Image.open(image) as img: # Convert the image to a NumPy array img_array = np.array(img) # Count the number of pixels with the target color target_color_count = np.sum(np.all(img_array == np.array(target_color), axis=2)) # Calculate the fraction of pixels with the target color total_pixels = img_array.shape[0] * img_array.shape[1] fraction = target_color_count / total_pixels return fraction def detect_deforestation(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' # Define the SentinelHub request parameters evalscript = """ // Import Sentinel-2 bands return [B02, B03, B04, B08]; """ 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]) # Calculate the NDVI (Normalized Difference Vegetation Index) nir = sentinel_data[:, :, 3] red = sentinel_data[:, :, 2] ndvi = (nir - red) / (nir + red) # Apply a threshold to identify deforested areas threshold = 0.2 deforestation_mask = (ndvi < threshold) # Create a color-coded PNG image deforestation_image = np.zeros_like(sentinel_data) deforestation_image[deforestation_mask] = [0, 0, 255, 255] # Red color for deforestation # Save the PNG image png_path = f"{output_dir}/deforestation.png" cv2.imwrite(png_path, deforestation_image) fraction = fraction_of_color(png_path, target_color=(0, 0, 255, 255)) return png_path, fraction#, geojson_path # Example usage: 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 = datetime.timedelta(minutes=expire_minutes), method="GET") return storage_url except: print('not there') def format_date(temp_date): new_date = temp_date[:4] + '-' + temp_date[4:6] + '-' + temp_date[6:] return new_date def get_def_thres(area_type): if area_type ==0: return 0.05 elif area_type == 1: return 0.01 else: return 0.05 def main(): #storage_client = storage.Client() # req_obj = {} # req_obj["IsReportRequested"] = 1 # db_firestore.collection('ReportZipRequests').document('XSzwRsmzu4OeCm8UvllGn7xsnOk1').set(req_obj) deforestation_thres =0.05 zip_requests_obj = db_firestore.collection(u'DeforestationRequests').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"] 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") bbox_area = area_from_bbox(bbox) time_interval = (format_date(str(from_date)), format_date(str(to_date))) output_directory = 'SFTP' png_path, fraction = detect_deforestation(bbox, time_interval, output_directory) if fraction>deforestation_thres: temp_main_obj["Deforestation"] = "yes" else: temp_main_obj["Deforestation"] = "no" url = upload_file((output_directory + '/deforestation.png'), ('PaidMonitoredFieldsPolygons/'+uid + '/' + fieldid + '/deforestation.png')) temp_main_obj["Processed"] = 1 temp_main_obj["URL"] = url db_firestore.collection(u'DeforestationRequests').document(time_stamp).set(temp_main_obj) except: print(traceback.format_exc()) db_firestore.collection(u'DeforestationRequests').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)