print('entered') import os import time import fiona import requests import utm from geopy.geocoders import Nominatim import time import json import pandas as pd from fastkml import kml from socket import * from oct2py import octave from io import BytesIO import base64 import xlrd import csv 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 import traceback from gen_report_new2 import gen_report_new from google.cloud import storage from oauth2client.service_account import ServiceAccountCredentials import os import firebase_admin from firebase_admin import credentials from firebase_admin import db import datetime 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") print('step 1') db_firestore = firestore.client() sentinelSettings = db.reference("SentinelSettings").get() clientID = sentinelSettings["ClientID"] clientSecret = sentinelSettings["ClientSecret"] wmsID = sentinelSettings["WMSID"] rviID = sentinelSettings["RVIID"] demID = sentinelSettings["DEMID"] images_arr = ["TCI", "NDVI", "NDWI", "NDRE", "RECL", "RVI", "RSM"] day_gap = 5 today = date.today() ndvi_val, ndwi_val = 0, 0 imageDate = today.strftime("%Y%m%d") print('step 2') kkk = 0 uid = 'TCXcp5VIsfhHZrh0nm2VsgBtcGy2' if not os.path.exists(uid): os.makedirs(uid) all_polygons_arr = [] n_tmux = 28 print('step 4') all_polygons_arr = [] missing_polygons_arr = [] main_final_obj = {} polygon_num = 1 iter = 0 field_count = 0 main_arr = [] # temp_list = db.reference("PaidMonitoredFields").child("PMF").get(False, True) # for uid, tmp in temp_list.items(): # fields_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).get(False, True) # for field_id, field_obj in fields_obj.items(): # single_point_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Coordinates").child("a").get() # if single_point_obj is not None: # field_area = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("FieldArea").get() # main_arr.append((single_point_obj.get("Latitude"), single_point_obj.get("Longitude"), field_area)) # field_count = field_count + 1 # print(field_count) # polygons_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Polygons").get(False, True) # if polygons_obj is not None: # for polygon_id, polygon_obj in polygons_obj.items(): # single_point_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Polygons").child(polygon_id).child("Coordinates").child("P_0").get() # if single_point_obj is not None: # field_area = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Polygons").child(polygon_id).child("Area").get() # main_arr.append((single_point_obj.get("Latitude"), single_point_obj.get("Longitude"), field_area)) # field_count = field_count + 1 # print(field_count) # temp_list = db.reference("DeletedFields").child("PMF").get(False, True) # for uid, tmp in temp_list.items(): # fields_obj = db.reference("DeletedFields").child("PMF").child(uid).get(False, True) # for field_id, field_obj in fields_obj.items(): # single_point_obj = db.reference("DeletedFields").child("PMF").child(uid).child(field_id).child("Coordinates").child("a").get() # if single_point_obj is not None: # field_area = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("FieldArea").get() # main_arr.append((single_point_obj.get("Latitude"), single_point_obj.get("Longitude"), field_area)) # field_count = field_count + 1 # print(field_count) # polygons_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Polygons").get(False, True) # if polygons_obj is not None: # for polygon_id, polygon_obj in polygons_obj.items(): # single_point_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Polygons").child(polygon_id).child("Coordinates").child("P_0").get() # if single_point_obj is not None: # field_area = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(field_id).child("Polygons").child(polygon_id).child("Area").get() # main_arr.append((single_point_obj.get("Latitude"), single_point_obj.get("Longitude"), field_area)) # field_count = field_count + 1 # print(field_count) import csv def save_to_csv(data, file_name): """ Save an array of three-value pairs to a CSV file. Args: data (list of tuple): A list containing tuples of three values each. file_name (str): The name of the CSV file to save the data. Example: data = [("Alice", 25, "Engineer"), ("Bob", 30, "Doctor"), ("Charlie", 22, "Artist")] save_to_csv(data, "output.csv") """ # Ensure data is a list of tuples with exactly three elements each if not all(isinstance(item, (tuple, list)) and len(item) == 3 for item in data): raise ValueError("Each item in the data must be a tuple or list with exactly three elements.") # Define the header row headers = ["Latitude", "Longitude", "Area"] # Write the data to the CSV file with open(file_name, mode="w", newline="", encoding="utf-8") as file: writer = csv.writer(file) # Write the header row writer.writerow(headers) # Write the data rows writer.writerows(data) print(f"Data successfully saved to {file_name}") # Example usage: # save_to_csv(main_arr, "lat_longs.csv") import csv import json def csv_to_geojson(csv_file_path, geojson_file_path): """ Convert a CSV file with Latitude, Longitude, and Area columns to a GeoJSON file with markers. Args: csv_file_path (str): Path to the input CSV file. geojson_file_path (str): Path to save the output GeoJSON file. """ features = [] # Read the CSV file with open(csv_file_path, 'r') as csv_file: csv_reader = csv.DictReader(csv_file) for row in csv_reader: try: # Extract latitude, longitude, and area latitude = float(row['Latitude']) longitude = float(row['Longitude']) area = row['Area'] # Create a GeoJSON feature feature = { "type": "Feature", "geometry": { "type": "Point", "coordinates": [longitude, latitude] }, "properties": { "Area": area } } features.append(feature) except ValueError as e: print(f"Skipping row due to error: {e}") # Create the GeoJSON structure geojson = { "type": "FeatureCollection", "features": features } # Write the GeoJSON to a file with open(geojson_file_path, 'w') as geojson_file: json.dump(geojson, geojson_file, indent=4) print(f"GeoJSON file created: {geojson_file_path}") # Example usage # csv_to_geojson('input.csv', 'output.geojson') csv_to_geojson('lat_longs.csv', 'farmonaut.geojson')