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 add_average_between_elements(input_array): result_array = [] for i in range(len(input_array) - 1): current_element = input_array[i] next_element = input_array[i + 1] # Calculate the average of current and next elements average_element = [(current_element[0] + next_element[0]) / 2, (current_element[1] + next_element[1]) / 2] # Add current element and average element to the result array result_array.append(current_element) result_array.append(average_element) # Add the last element from the input array to the result array result_array.append(input_array[-1]) return result_array 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[0]) lng = float(new_coords[1]) 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) found_coordinates = add_average_between_elements(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)