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 import statsmodels.api as sm import seaborn as sns sns.set() import time import json import numpy as np from sklearn.cluster import KMeans from find_study_area_values import find_study_area_values from make_area_estimate_image import make_area_estimate_image from sentinelhub import WebFeatureService, BBox, CRS, DataSource, MimeType, CRS, BBox, WmsRequest,DataCollection 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" gt_uid = 'hxA3VYEFpaOluurIbHxdzd4gPlw1' different_gt_uid = 'joCrKyhfobbzgCxCQPHJfOOKKPF3' study_field_id = '1599307314955' gt_fields_dir = 'Downloads/gt_egypt.json' #try: with open(gt_fields_dir) as f: gt_fields = json.load(f) new_gt_json = {} x = 0.0004 y = 0.0004 gt_fields = gt_fields["Sheet1"] for single_gt_field in gt_fields: gt_lat = single_gt_field["lat"] gt_lng = single_gt_field["lng"] gt_crop = single_gt_field["Crop"] try: new_gt_json[gt_crop].append([gt_lat, gt_lng]) except: new_gt_json[gt_crop] = [] new_gt_json[gt_crop].append([gt_lat, gt_lng]) print(new_gt_json) sentinelSettings = db.reference('SentinelSettings').get() clientID = sentinelSettings["ClientID"] clientSecret = sentinelSettings["ClientSecret"] wmsID = sentinelSettings["WMSID"] rviID = sentinelSettings["RVIID"] demID = sentinelSettings["DEMID"] field_num = 0 from_date = '2022-06-25T00:00:00' to_date = '2022-09-10T23:59:59' max_num = 16 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 crop_wise_img_values = {} all_fields_img_values = [] i_num = 0 for (crop, all_points) in new_gt_json.items(): for single_point in all_points: midlat = float(single_point[0]) midlng = float(single_point[1]) bounds = [(midlng-y),(midlat-x),(midlng+y),(midlat+x)] img_values = [] for image in s1_images: temp_arr = find_img_value(crop,DataCollection.SENTINEL1_IW, image, bounds, from_date, to_date, clientID, clientSecret, rviID, max_num) img_values = append_img_vals(temp_arr, img_values) #print(img_values) if len(img_values) > 0: i_num = i_num + 1 print('i_num: ' + str(i_num)) all_fields_img_values.append(img_values) try: crop_wise_img_values[crop].append(img_values) except: crop_wise_img_values[crop] = [] crop_wise_img_values[crop].append(img_values) print(i_num) print(all_fields_img_values) print(crop_wise_img_values) np.savetxt("all_fields_img_values.csv", all_fields_img_values, delimiter = ",") np.savetxt("crop_wise_img_values.csv", crop_wise_img_values, delimiter = ",") k_groups = 14 kmeans = KMeans(k_groups) #kmeans = KMeans(n_clusters =2, random_state = 0) kmeans.fit(all_fields_img_values) for (crop, crop_values) in crop_wise_img_values.items(): cluster_array = kmeans.predict(crop_values) unique, count_array = np.unique(cluster_array, return_counts = True) result = np.column_stack((unique, count_array)) print(result) new_count_array = sorted(result, key = lambda x:x[1], reverse=True) print(new_count_array) pred_vals = [] sum_count_array = np.sum(count_array) j = 0 for i in new_count_array: if j/sum_count_array < 0.7: pred_vals.append(i[0]) j = j + i[1] else: break print(pred_vals) study_pixel_values_arr = [] 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 for image in s2_images: temp_arr,w,h = find_study_area_values(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_values(DataCollection.SENTINEL1_IW, image, bounds, from_date, to_date, clientID, clientSecret, rviID, max_num) Identified_clusters = kmeans.fit_predict(temp_arr) # np.savetxt('identified_arr.csv',Identified_clusters, delimiter=",") make_area_estimate_image(Identified_clusters, w, h, study_uid, study_field_id, pred_vals)