import fiona import requests import utm from geopy.geocoders import Nominatim import time import traceback 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 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") 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") def get_from_to_dates(imageDate, day_gap): current_month = int(imageDate[4:6]) current_year = int(imageDate[0:4]) current_date = int(imageDate[6:]) latestsensedday = 0 start_year = current_year if current_date < day_gap and current_month > 1: if latestsensedday == 0: start_date = abs(31 - current_date - day_gap) else: start_date = abs(31 - current_date - (day_gap - 1)) start_month = current_month - 1 start_year = current_year elif current_date < day_gap and current_month == 1: if latestsensedday == 0: start_date = abs(31 - current_date - day_gap) else: start_date = abs(31 - current_date - (day_gap - 1)) start_month = 12 start_year = start_year - 1 else: if latestsensedday == 0: start_date = current_date - day_gap else: start_date = current_date - (day_gap - 1) start_month = current_month start_year = current_year if start_date == 0: start_date = 1 if current_date == 0: current_date = 1 if current_month < 10: current_month = "0" + str(current_month) else: current_month = str(current_month) if start_month < 10: start_month = "0" + str(start_month) else: start_month = str(start_month) if current_date < 10: current_date = "0" + str(current_date) else: current_date = str(current_date) if start_date < 10: start_date = "0" + str(start_date) else: start_date = str(start_date) start_year = str(start_year) from_date = start_year + "-" + start_month + "-" + str(start_date) to_date = str(current_year) + "-" + current_month + "-" + str(current_date) l_date = str(current_year) + current_month + str(current_date) from_date = from_date + "T00:00:00" to_date = to_date + "T23:59:59" return from_date, to_date def get_bounds(points_obj): lats, lngs = [], [] for key, value in points_obj.items(): lats.append(value["Latitude"]) lngs.append(value["Longitude"]) bounds = [min(lngs), min(lats), max(lngs), max(lats)] return ( bounds, min(lats), max(lats), min(lngs), max(lngs), sum(lats) / len(lats), sum(lngs) / len(lngs), ) 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") except: print('not there') def process_tmux_polygons(uid, polygons_arr): for single_polygon in polygons_arr: fieldid, polygonid = single_polygon[0], single_polygon[1] polygon_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(fieldid).child('Polygons').child(polygonid).get() try: try: sensed_days = polygon_obj["SensedDays"] except: sensed_days = None prev_day = 0 if sensed_days is not None: for pkk, temp_obj in sensed_days.items(): prev_day = pkk print([fieldid, polygonid]) print([imageDate, prev_day]) str_polygon_date = '' if int(imageDate) - int(prev_day) > 6: for imageType in images_arr: if imageType == "RVI" or imageType == "RSM": satellite, INSTANCE_ID = DataCollection.SENTINEL1_IW, rviID day_gap = 20 else: satellite, INSTANCE_ID = DataCollection.SENTINEL2_L1C, wmsID day_gap = 5 c_map = 1 from_date, to_date = get_from_to_dates(imageDate, day_gap) # print(polygon_obj["Coordinates"]) ( bounds, polygon_min_lat, polygon_max_lat, polygon_min_long, polygon_max_long, polygon_center_lat, polygon_center_long, ) = get_bounds(polygon_obj["Coordinates"]) points_obj = polygon_obj["Coordinates"] if imageType in ["NDVI", "NDWI", "RECL", "NDRE", "RVI", "RSM"]: polygon_date, index_val = make_on_the_go_farm_image( c_map, uid, fieldid, polygonid, satellite, imageType, bounds, from_date, to_date, clientID, clientSecret, INSTANCE_ID, ) str_index_val = str(index_val) #str_polygon_date = polygon_date.replace("-","") db.reference("PaidMonitoredFields").child("PMF").child(uid).child(fieldid).child("Polygons").child(polygonid).child("Health").child(str_polygon_date).child(imageType).set(str_index_val) get_polygon_mask( uid, polygonid, imageType, points_obj, bounds[3], bounds[1], bounds[2], bounds[0], ) #upload_file((uid + '/' + polygonid + '/' + imageType + '.png'), ('PaidMonitoredFieldsPolygons/'+uid + '/' + fieldid + '/'+polygonid + '/'+str_polygon_date + '/'+imageType + 'png')) if imageType == "NDVI": ndvi_val = round(index_val) / 100 ndvi_val = index_val elif imageType == "NDWI": ndwi_val = round(index_val) / 100 ndwi_val = index_val #print([ndvi_val, ndwi_val]) else: temp_date, y = make_on_the_go_farm_image( c_map, uid, fieldid, polygonid, satellite, imageType, bounds, from_date, to_date, clientID, clientSecret, INSTANCE_ID, ) get_polygon_mask( uid, polygonid, imageType, points_obj, bounds[3], bounds[1], bounds[2], bounds[0], ) str_polygon_date = temp_date.replace("-","") #upload_file((uid + '/' + polygonid + '/' + imageType + '.png'), ('PaidMonitoredFieldsPolygons/'+uid + '/' + fieldid + '/'+polygonid + '/'+str_polygon_date + '/'+imageType + 'png')) if imageType == "TCI": etci_file_name = uid + "/" + polygonid + "/TCI.png" octave.make_polygon_etci(etci_file_name, uid) get_polygon_mask( uid, polygonid, imageType, points_obj, bounds[3], bounds[1], bounds[2], bounds[0], ) upload_file((uid + '/' + polygonid + '/' + imageType + '.png'), ('PaidMonitoredFieldsPolygons/'+uid + '/' + fieldid + '/'+polygonid + '/'+str_polygon_date + '/'+imageType + '.png')) try: if uid == "TCXcp5VIsfhHZrh0nm2VsgBtcGy2": f_address = ( "Farmer Code: " + polygon_obj["Name"] + ", Land ID: " + polygon_obj["PhoneNumber"] ) else: f_address = ( "Farmer Name: " + polygon_obj["Name"] + ", Phone Number: " + polygon_obj["PhoneNumber"] ) except: f_address = "Farm" fieldarea = polygon_obj["Area"] print(polygon_date) polygon_date = polygon_date.replace("-", "") # get_polygon_mask(uid,polygonid,(imageType+"_CMAP2"),points_obj,bounds[3],bounds[1],bounds[2],bounds[0]) # gen_report_new(uid, field_address, center_lat, center_long, imagedate, fieldid, field_area, lang, failed, whitelabel, rangeLatLng, points, polygonID=None) print( [ polygon_center_lat, polygon_center_long, polygon_min_lat, polygon_max_lat, polygon_min_long, polygon_max_long, ] ) db.reference("PaidMonitoredFields").child("PMF").child(uid).child( fieldid ).child("Polygons").child(polygonid).child("SensedDays").child( str_polygon_date ).set( "yes" ) #time.sleep(80060) except: print(traceback.format_exc()) # kkk = 0 # uid = 'TCXcp5VIsfhHZrh0nm2VsgBtcGy2' # if not os.path.exists(uid): # os.makedirs(uid) # all_polygons_arr = [] # n_tmux = 28 # while kkk == 0: # temp_list = db.reference("PaidMonitoredFields").child("PMF").child(uid).get(False, True) # for fieldid, tmp in temp_list.items(): # polygons_obj = db.reference("PaidMonitoredFields").child("PMF").child(uid).child(fieldid).child('Polygons').get(False, True) # try: # for polygonid, temp_p in polygons_obj.items(): # all_polygons_arr.append((fieldid, polygonid)) # except: # awa = 1 # interval = round(len(all_polygons_arr)/n_tmux) # print([len(all_polygons_arr), interval]) # for i in range(1, (n_tmux + 1)): # if i == n_tmux: # temp_polygons_arr = all_polygons_arr[((i-1)*interval): (len(all_polygons_arr)-1)] # else: # temp_polygons_arr = all_polygons_arr[((i-1)*interval): (i*interval)] # process_tmux_polygons(uid, temp_polygons_arr) # time.sleep(86400) # get the list of approved polygons # structure: Satellite Polygons -> polygonID -> (UID, fieldID, enabled, duration) # iterate through polygons # generate images of that polygon # generate report # send whatsapp