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 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() def simplify_coordinates(coordinates, tolerance): coordinates = np.array(coordinates) # Convert to NumPy array if len(coordinates) <= 2: return coordinates.tolist() # Convert back to list # Find the point with the maximum distance max_distance = 0 index = 0 end = len(coordinates) - 1 for i in range(1, end): distance = perpendicular_distance(coordinates[i], coordinates[0], coordinates[end]) if distance > max_distance: max_distance = distance index = i # If the maximum distance is greater than the tolerance, recursively simplify if max_distance > tolerance: simplified1 = simplify_coordinates(coordinates[:index+1], tolerance) simplified2 = simplify_coordinates(coordinates[index:], tolerance) return simplified1[:-1] + simplified2 # No need to convert back to list else: return [coordinates[0], coordinates[end]] # No need to convert back to list def perpendicular_distance(point, start, end): if np.all(start == end): return np.linalg.norm(np.array(point) - np.array(start)) line_length = np.linalg.norm(np.array(end) - np.array(start)) t = np.dot(np.array(point) - np.array(start), np.array(end) - np.array(start)) / (line_length * line_length) t = np.clip(t, 0, 1) closest_point = np.array(start) + t * (np.array(end) - np.array(start)) return np.linalg.norm(np.array(point) - closest_point) def convert_to_list_of_lists(data): result = [list(np.array(item)) for item in data] return result def process_segments(): zip_requests_obj = db_firestore.collection(u'SegmentRequests').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"] coords = temp_main_obj["Point"] new_coords = coords.split(",") lat = float(new_coords[1]) lng = float(new_coords[0]) found_coordinates = get_main_segment(lat,lng,uid, "sample") # Example usage: ##coordinates = [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] tolerance = 0.00005 # Adjust this value to control the level of simplification found_coordinates = simplify_coordinates(found_coordinates, tolerance) found_coordinates = convert_to_list_of_lists(found_coordinates) temp_main_obj["Processed"] = 1 temp_main_obj["Points"] = str(found_coordinates) db_firestore.collection(u'SegmentRequests').document(time_stamp).set(temp_main_obj) except: print(traceback.format_exc()) db_firestore.collection(u'SegmentRequests').document(time_stamp).delete() i = 0 while i == 0: process_segments() time.sleep(5)