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 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 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), ) kkk = 0 while kkk == 0: # data_requests_obj = db_firestore.collection(u'WhatsAppPolygons').where('Enabled','==','yes').where('LatestDay','<', (int(imageDate) - 4)).get() # print(data_requests_obj) data_requests_obj = ( db_firestore.collection("WhatsAppPolygons").where("Enabled", "==", "yes").get() ) for temp_d in data_requests_obj: try: timestamp = temp_d.id main_obj = temp_d.to_dict() uid, fieldid, polygonid = ( main_obj["UID"], main_obj["FieldID"], main_obj["PolygonID"], ) polygon_obj = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Polygons") .child(polygonid) .get() ) 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([imageDate, prev_day]) 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 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, ) get_polygon_mask( uid, polygonid, imageType, points_obj, bounds[3], bounds[1], bounds[2], bounds[0], ) 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]) polygon_date, x = make_on_the_go_farm_image( 2, uid, fieldid, polygonid, satellite, imageType, bounds, from_date, to_date, clientID, clientSecret, INSTANCE_ID, ) get_polygon_mask( uid, polygonid, (imageType + "_CMAP2"), points_obj, bounds[3], bounds[1], bounds[2], bounds[0], ) else: 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], ) 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], ) 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, ] ) # gen_report_new(uid,f_address,centerlat,centerlong,imagedate,fieldid,fieldarea,lang,1, whitelabel, [fieldminlat, fieldminlong, fieldmaxlat, fieldmaxlong], coordinates, None,0,0,fie) gen_report_new( uid, f_address, polygon_center_lat, polygon_center_long, polygon_date, fieldid, fieldarea, "en", 1, "farmonaut", [ polygon_min_lat, polygon_min_long, polygon_max_lat, polygon_max_long, ], polygon_obj["Coordinates"], polygonid, ndvi_val, ndwi_val, polygon_obj, ) db.reference("PaidMonitoredFields").child("PMF").child(uid).child( fieldid ).child("Polygons").child(polygonid).child("SensedDays").child( polygon_date ).set( "yes" ) db_firestore.collection("WhatsAppPolygons").document(timestamp).update( {"LatestDay": polygon_date} ) except: print(traceback.format_exc()) time.sleep(60) # 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