from tempfile import tempdir import firebase_admin from firebase_admin import credentials from firebase_admin import db from find_s1_image import find_img_value import pandas as pd from sklearn.metrics import classification_report, confusion_matrix, accuracy_score import statsmodels.api as sm import seaborn as sns sns.set() from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split import csv import time import json from find_modis_ndvi import find_modis_ndvi import numpy as np from sklearn.cluster import KMeans from find_study_area_values import find_study_area_values from find_study_area_values3 import find_study_area_values3 from make_area_estimate_image import make_area_estimate_image from make_egypt_estimate_image import make_egypt_estimate_image from sentinelhub import WebFeatureService, BBox, CRS, DataSource, MimeType, CRS, BBox, WmsRequest,DataCollection import traceback cred = credentials.Certificate('servicekey.json') x = 0.0003 y = 0.0003 fromdate = '20220701' todate = '20220715' s1_images = ['IW-VH-DB'] s2_images = [] # s1_images = ["B02", "B03", "B04", "B05"] #s2_images = ["RVI-NEW"] try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') #study_uid = "MBFshQgimkS6AwSDtqAyOpRXZW12" study_uid = "vPM6EKWHfHRxmRfc0YJ7PZ8J9tR2" study_uid = "vYw5pBtwLhcSlKIz5YikEr27GMu2" gt_uid = 'hxA3VYEFpaOluurIbHxdzd4gPlw1' different_gt_uid = 'joCrKyhfobbzgCxCQPHJfOOKKPF3' study_field_id = '1661604460461' #study_field_id = '1599307314955' gt_fields_dir = 'Downloads/PaddyGTpointsMP.geojson' study_fields = db.reference('PaidMonitoredFields').child('PMF').child(study_uid).get() sentinelSettings = db.reference('SentinelSettings').get() clientID = sentinelSettings["ClientID"] clientSecret = sentinelSettings["ClientSecret"] wmsID = sentinelSettings["WMSID"] rviID = sentinelSettings["RVIID"] demID = sentinelSettings["DEMID"] modisID = sentinelSettings["MODISID"] field_num = 0 from_date = '2022-05-15T00:00:00' to_date = '2022-09-14T23:59:59' max_num = 18 main_from_date = '20220105' main_to_date = '20220502' def append_img_vals(temp_arr, img_values): if temp_arr is not None: for k in temp_arr: img_values.append(k) return img_values all_fields_img_values = [] gt_fields_img_values = [] with open("all_field_img_values.csv", mode = "r") as file: csvFile = csv.reader(file) for line in csvFile: # print(line) all_fields_img_values.append(line) #gt_fields_dir = 'crop_wise_img_values.json' gt_fields_dir = 'temp1.json' with open(gt_fields_dir) as f: crop_wise_img_values = json.load(f) # print(all_fields_img_values) # print(crop_wise_img_values) k_groups = 7 X = [] y = [] all_fields_img_values = [] main_arr = [] for (crop, crop_values) in crop_wise_img_values.items(): for single_arr in crop_values: X.append(single_arr) all_fields_img_values.append(single_arr) if crop == "Corn": y.append(-1) elif crop == "cotton": y.append(4) elif crop == "rice": y.append(-1) main_arr.append(single_arr[0:max_num]) elif crop == "Yellow corn": y.append(-1) else: y.append(-1) kmeans = KMeans(k_groups) X = np.array(X) y = np.array(y) x_num = 0 for k in X: k = k[0:max_num] X[x_num] = k # print(X) print(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0) ii = 0 regressor = RandomForestRegressor(n_estimators=200,random_state=0) regressor.fit(X_train, y_train) y_pred = regressor.predict(X) print(y_pred) #print(pred_vals) main_y = [[4,-1,-1,-1,-1],[-1,4,-1,-1,-1],[-1,-1,4,-1,-1],[-1,-1,-1,4,-1]] veg_arr = ["Corn","cotton","rice","Yellow corn"] field_num = 0 for (field_id, study_field) in study_fields.items(): y = [] for (crop, crop_values) in crop_wise_img_values.items(): for single_arr in crop_values: if crop == veg_arr[field_num]: y.append(4) else: y.append(-1) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0) regressor = RandomForestRegressor(n_estimators=200,random_state=0) regressor.fit(X_train, y_train) y_pred = regressor.predict(X) print(y_pred) field_num = field_num + 1 study_pixel_values_arr = [] #get values for study area try: field_name = study_field["FieldDescription"] except: field_name = study_field["FieldAddress"] midlat = (float(study_field["FieldMinLat"]) + float(study_field["FieldMaxLat"]))/2 midlng = (float(study_field["FieldMinLong"]) + float(study_field["FieldMaxLong"]))/2 bounds = [float(study_field["FieldMinLong"]),float(study_field["FieldMinLat"]),float(study_field["FieldMaxLong"]),float(study_field["FieldMaxLat"])] print(bounds) image_num = 0 find_modis_ndvi(field_name, DataCollection.MODIS, "NDVI", bounds, from_date, to_date, clientID, clientSecret, modisID, max_num) # time.sleep(100000) for image in s2_images: temp_arr,w,h = find_study_area_values3(field_name, DataCollection.SENTINEL2_L1C,image, bounds, from_date, to_date, clientID, clientSecret, wmsID, max_num) for image in s1_images: temp_arr,w,h = find_study_area_values3(field_name, DataCollection.SENTINEL1_IW, image, bounds, from_date, to_date, clientID, clientSecret, rviID, max_num) np.savetxt("temp_arr.csv", temp_arr, delimiter = ",") y_pred = regressor.predict(temp_arr) # print(y_pred) pixel_num = 0 try: #Identified_clusters = kmeans.fit_predict(temp_arr) # np.savetxt('identified_arr.csv',Identified_clusters, delimiter=",") pred_vals = [] make_egypt_estimate_image(y_pred, w, h, study_uid, field_id, pred_vals) except Exception as e: print(traceback.format_exc())