import rasterio import numpy from xml.dom import minidom from socket import * from oct2py import octave from io import BytesIO import base64 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 from PIL import Image, ImageFilter from send_notification import send_notification from sendemail import sendemail from sen_start_noti import sen_start_noti from send_sar_email import send_sar_email import json import scipy import random import time import datetime from merge_sar import merge_sar from find_sar import find_sar from merge_dem import merge_dem from find_dem import find_dem from scipy import ndimage from make_bigquery import make_bigquery from send_moni_noti import send_moni_noti from send_error_noti import send_error_noti from gmap_image import gmap_image from gmap_image_large import gmap_image_large from datetime import date from find_img import find_img from find_img_large import find_img_large from merge_img import merge_img from all_proc import all_proc from contour_images import contour_images from send_expiring_noti import send_expiring_noti from send_expired_noti import send_expired_noti from make_trial_bigquery import make_trial_bigquery from gen_geotiff import gen_geotiff from sendgeotifs import sendgeotifs from gen_report import gen_report from get_weather_data import get_weather_data from sendonlyreport import sendonlyreport from gen_failed_report import gen_failed_report from sendfailedreport import sendfailedreport #from search_sentinel import search_sentinel from map_coords import map_coords from search_new_sentinel import search_new_sentinel from convert_to_pdf import convert_to_pdf from latlon_jp2_to_pixel import latlon_jp2_to_pixel from gen_geotiff2 import gen_geotiff2 from search_sentinel_again import search_sentinel_again from get_prev_date import get_prev_date from make_bigquery_again import make_bigquery_again import requests import pdftotree from convert_to_html import convert_to_html from map_planet_coords import map_planet_coords from latlon_tif_to_pixel import latlon_tif_to_pixel import cv2 from merge_planet_img import merge_planet_img import csv import math today = date.today() d1 = today.strftime('%Y%m%d') new_field = 0 print(d1) aqw2 = 1 cred = credentials.Certificate('servicekey.json') os.system("rm -rf AwsData") uid = "TCXcp5VIsfhHZrh0nm2VsgBtcGy2" # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) uid_list = db.reference('PaidMonitoredFields').child('PMF').child('TCXcp5VIsfhHZrh0nm2VsgBtcGy2').child('1611730411677').child('Polygons').get() image_file = "planet_data_1.tif" with rasterio.open(image_file) as src: band_blue = src.read(1) with rasterio.open(image_file) as src: band_green = src.read(2) # Load red and NIR bands - note all PlanetScope 4-band images have band order BGRN with rasterio.open(image_file) as src: band_red = src.read(3) with rasterio.open(image_file) as src: band_nir = src.read(4) xmldoc = minidom.parse("20210427_051127_53_240a_3B_AnalyticMS_metadata_clip.xml") nodes = xmldoc.getElementsByTagName("ps:bandSpecificMetadata") # XML parser refers to bands by numbers 1-4 coeffs = {} for node in nodes: bn = node.getElementsByTagName("ps:bandNumber")[0].firstChild.data if bn in ['1', '2', '3', '4']: i = int(bn) value = node.getElementsByTagName("ps:reflectanceCoefficient")[0].firstChild.data coeffs[i] = float(value) # Multiply by corresponding coefficients band_blue = band_blue * coeffs[1] #2 band_green = band_green * coeffs[2] #3 band_red = band_red * coeffs[3] #4 band_nir = band_nir * coeffs[4] #8 # Allow division by zero numpy.seterr(divide='ignore', invalid='ignore') max_si = band_blue.astype(float) # Calculate NDVI ndvi = (band_nir.astype(float) - band_red.astype(float)) / (band_nir + band_red) bsi = (band_red.astype(float) + band_blue.astype(float) - band_green.astype(float))/(band_red.astype(float) + band_blue.astype(float) + band_green.astype(float)) vari = (band_green.astype(float)-band_red.astype(float)/(band_green.astype(float) + band_red.astype(float) - band_blue.astype(float))) # Set spatial characteristics of the output object to mirror the input kwargs = src.meta kwargs.update( dtype=rasterio.float32, count = 1) # Create the file with rasterio.open('ndvi.tif', 'w', **kwargs) as dst: dst.write_band(1, ndvi.astype(rasterio.float32)) with rasterio.open('ndvi.png', 'w', **kwargs) as dst: dst.write_band(1, ndvi.astype(rasterio.float32)) with rasterio.open('vari.png', 'w', **kwargs) as dst: dst.write_band(1, vari.astype(rasterio.float32)) with rasterio.open('bsi.png', 'w', **kwargs) as dst: dst.write_band(1, bsi.astype(rasterio.float32)) with rasterio.open('b2.png', 'w', **kwargs) as dst: dst.write_band(1, band_blue.astype(rasterio.float32)) with rasterio.open('b3.png', 'w', **kwargs) as dst: dst.write_band(1, band_green.astype(rasterio.float32)) with rasterio.open('b4.png', 'w', **kwargs) as dst: dst.write_band(1, band_red.astype(rasterio.float32)) with rasterio.open('nir.png', 'w', **kwargs) as dst: dst.write_band(1, band_nir.astype(rasterio.float32)) ##si = octave.make_planet_si("sample") ndviWidth = 3835 ndviHeight = 5988 with open('planet_table.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerow(["Polygon ID", "Farmer Name", "Farmer Phone Number", "Field Area", "NDVI", "BSI", "SI"]) for (k,v) in uid_list.items(): polygon_id = k polygon_data = v try: farmer_name = polygon_data["Name"] except: farmer_name = "Not Available" try: phone_number = polygon_data["PhoneNumber"] except: phone_number = "Not Available" try: field_area = polygon_data["Area"] except: field_area = "Not Available" polygon_points = polygon_data["Coordinates"] coordinates = polygon_points polygon_max_lat = -180 polygon_min_lat = 180 polygon_max_long = -180 polygon_min_long = 180 for(pointKey, pointData) in polygon_points.items(): print('Point data') print(pointKey) print(pointData) pointLat = pointData["Latitude"] pointLong = pointData["Longitude"] if pointLat > polygon_max_lat: polygon_max_lat = pointLat if pointLat < polygon_min_lat: polygon_min_lat = pointLat if pointLong > polygon_max_long: polygon_max_long = pointLong if pointLong < polygon_min_long: polygon_min_long = pointLong new_string_json = {} print('polygon bounds') print(polygon_min_lat, polygon_max_lat, polygon_min_long, polygon_max_long) up_left_x,up_left_y = latlon_tif_to_pixel(polygon_max_lat,polygon_min_long) up_right_x,up_right_y = latlon_tif_to_pixel(polygon_max_lat,polygon_max_long) down_right_x,down_right_y = latlon_tif_to_pixel(polygon_min_lat,polygon_max_long) down_left_x,down_left_y = latlon_tif_to_pixel(polygon_min_lat,polygon_min_long) new_string_json["StartPixelLat"] = min([up_left_x,up_right_x,down_left_x,down_right_x]) new_string_json["EndPixelLat"] = max([up_left_x,up_right_x,down_left_x,down_right_x]) new_string_json["StartPixelLong"] = min([up_left_y,up_right_y,down_left_y,down_right_y]) new_string_json["EndPixelLong"] = max([up_left_y,up_right_y,down_left_y,down_right_y]) fieldminlat = polygon_min_lat fieldmaxlat = polygon_max_lat fieldminlong = polygon_min_long fieldmaxlong = polygon_max_long print('tif bounds') print(up_left_x,up_left_y) print(up_right_x,up_right_y) print(down_right_x,down_right_y) print(down_left_x,down_left_y) startx = min(up_left_x,up_right_x,down_left_x,down_right_x) endx = max(up_left_x,up_right_x,down_left_x,down_right_x) starty = min(up_left_y,up_right_y,down_left_y,down_right_y) endy = max(up_left_y,up_right_y,down_left_y,down_right_y) print(startx, endx, starty, endy) fieldid = polygon_id new_string_json["UID"] = uid new_string_json["FieldID"] = polygon_id if startx < ndviWidth and startx >0 and endx < ndviWidth and endx > 0 and starty < ndviHeight and starty > 0 and endy > 0 and endy < ndviHeight: print('doing...') result_status = octave.planet_ndvi(new_string_json,'ndvi') result_status = octave.planet_ndvi(new_string_json,'bsi') result_status = octave.planet_ndvi(new_string_json,'si') result_status = octave.planet_ndvi(new_string_json,'evi') result_status = octave.planet_ndvi(new_string_json,'avi') result_status = octave.planet_ndvi(new_string_json,'savi') result_status = octave.planet_ndvi(new_string_json,'vari') ndvi_value,bsi_value,si_value,vari_value,avi_value,savi_value,evi_value = map_planet_coords(uid,fieldid,coordinates,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong) print(ndvi_value,bsi_value,si_value,vari_value,avi_value,savi_value,evi_value) writer.writerow([polygon_id, farmer_name, phone_number, field_area, ndvi_value, bsi_value, si_value,vari_value,avi_value,savi_value,evi_value])