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 ins_report_filler import populateData, companyLogos from gen_report_new2 import gen_report_new import pdfkit from make_dir import make_dir 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'] 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 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(field_obj["Coordinates"]) bounds, polygon_min_lat, polygon_max_lat, polygon_min_long, polygon_max_long, polygon_center_lat, polygon_center_long = get_bounds(field_obj["Coordinates"]) points_obj = field_obj["Coordinates"] if imageType in ['NDVI', 'NDWI', 'RECL', 'NDRE', 'RVI', 'RSM']: polygon_date, index_val = make_on_the_go_farm_image(c_map, uid, None, fieldid,satellite, imageType,bounds, from_date, to_date,clientID,clientSecret,INSTANCE_ID) get_polygon_mask(uid,fieldid,imageType,points_obj,bounds[3],bounds[1],bounds[2],bounds[0]) print(('index val: ' + str(index_val))) 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, None, fieldid,satellite, imageType,bounds, from_date, to_date,clientID,clientSecret,INSTANCE_ID) # get_polygon_mask(uid,fieldid,(imageType+"_CMAP2"),points_obj,bounds[3],bounds[1],bounds[2],bounds[0]) else: make_on_the_go_farm_image(c_map, uid, None, fieldid,satellite, imageType,bounds, from_date, to_date,clientID,clientSecret,INSTANCE_ID) get_polygon_mask(uid,fieldid,imageType,points_obj,bounds[3],bounds[1],bounds[2],bounds[0]) if imageType == 'TCI': etci_file_name = uid + "/" +fieldid + "/TCI.png" octave.make_polygon_etci(etci_file_name, uid) get_polygon_mask(uid,fieldid,imageType,points_obj,bounds[3],bounds[1],bounds[2],bounds[0]) try: if uid == "TCXcp5VIsfhHZrh0nm2VsgBtcGy2": f_address = "Farmer Code: " +field_obj["Name"] + ", Land ID: " + field_obj["PhoneNumber"] else: f_address = "Farmer Name: " +field_obj["Name"] + ", Phone Number: " + field_obj["PhoneNumber"] except: f_address = "Farm" fieldarea = field_obj["FieldArea"] 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) is_land_hist_cult = False if ndvi_val/100 > 0.4: is_land_hist_cult = True #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], field_obj["Coordinates"], polygonid, ndvi_val,ndwi_val, field_obj) strImgDate = str(imageDate) yyyy,mm,dd = strImgDate[:4],strImgDate[4:6], strImgDate[6:] print([yyyy,mm,dd]) ndvi_file_name = uid + "/" +fieldid + "/NDVI.png" allReportValues = { "field_coords":field_obj["Coordinates"], "ndvi_avg_value": int(ndvi_val)/100, "ndvi_path": ndvi_file_name, "generation_date": (dd + '-' + mm + '-' + yyyy), "sowing_date": None, "harvest_date": None, "etci_path": etci_file_name, "irri_value": ndwi_val, "is_land_hist_cult": False, "crop_inten_value": ndvi_val, "soil_health_value": None, "grd_water_value": None, "land_score_value": None, "crop_score_value": None, "poten_earn_value": None, "mand_dist_km": None, "has_hazard_hist": None, "companyLogoImg":companyLogos.get(whitelabel) } html_str = populateData(translations_obj, "en", allReportValues, False) htmlfilename = 'temp1.html' pdf_name = uid + '/'+fieldid + '/temp.pdf' saveToTmpHtml(html_str, htmlfilename) options = { "enable-local-file-access": "", "page-height": "192mm", "page-width": "136mm", "margin-top": "6mm", "margin-bottom": "6mm", "margin-left": "8mm", "margin-right": "8mm", } pdfkit.from_file(htmlfilename, pdf_name, options=options) reportPathDays[polygon_date] = "yes" ##db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('SensedDays').child(polygon_date).set('yes') db_firestore.collection(u'FinancePolygons').document(timestamp).update({u'LatestDay': polygon_date}) db_firestore.collection(u'FinancePolygons').document(timestamp).update({u'Processed': 'yes'}) db_firestore.collection(u'FinancePolygons').document(timestamp).update({u'ReportPathDates': reportPathDays}) destination_blob_name_dem = 'FinancePolygons/'+uid+'/'+fieldid + '/'+ str(polygon_date)+'/report.pdf' dem_file_name = uid+ '/' + fieldid + '/temp.pdf' bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_dem) blob.upload_from_filename(dem_file_name) except: print(traceback.format_exc()) time.sleep(60)