def create_index_piechart(uid,fieldid,img_type, image_date): import matplotlib.pyplot as plt import numpy as np import os,sys import cv2 import csv import firebase_admin from firebase_admin import credentials from firebase_admin import db import seaborn as sns sns.set() import csv import time import traceback import xlsxwriter cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: wes1 = 1#print('fire running') import matplotlib.patches as mpatches plt.rcParams.update({'font.size':12}) if img_type=='ndvi': lb = 'NDVI: ' lb2 = 'NDVI' elif img_type=='ndwi': lb = 'NDWI: ' lb2 = 'NDWI' elif img_type=='vari': lb = 'VARI: ' lb2 = 'VARI' elif img_type=='evi': lb = 'EVI: ' lb2 = 'EVI' elif img_type=='savi': lb = 'SAVI: ' lb2 = 'SAVI' elif img_type=='avi': lb = 'AVI: ' lb2 = 'AVI' elif img_type=='bsi': lb = 'BSI: ' lb2 = 'BSI' elif img_type=='si': lb = 'SI: ' lb2 = 'SI' elif img_type=='soc': lb = 'SOC: ' lb2 = 'SOC' elif img_type=='ndmi': lb = 'NDMI: ' lb2 = 'NDMI' elif img_type=='lai': lb = 'LAI: ' lb2 = 'LAI' elif img_type=='rvi': lb = 'RVI: ' lb2 = 'RVI' elif img_type=='lai': lb = 'RSM: ' lb2 = 'RSM' else: lb='NDRE: ' lb2 = 'NDRE' labels = (lb+'-1 to 0.1'),(lb+'0.1 to 0.2'),(lb+'0.2 to 0.3'),(lb+'0.3 to 0.4'),(lb+'0.4 to 0.5'),(lb+'0.5 to 0.6'),(lb+'0.6 to 0.7'),(lb+'0.7 to 0.8'),(lb+'0.8 to 0.9'),(lb+'0.9 to 1.0') labels_vari = (lb+'-1 to -0.8'),(lb+'-0.8 to -0.6'),(lb+'-0.6 to -0.4'),(lb+'-0.4 to -0.2'),(lb+'-0.2 to 0'),(lb+'0 to 0.2'),(lb+'0.2 to 0.4'),(lb+'0.4 to 0.6'),(lb+'0.6 to 0.8'),(lb+'0.8 to 1.0') if img_type =='vari': labels = labels_vari cmap = ["#ab0535","#ea4f3b","#f7885a","#fbc07e","#fff0b5","#e6f3a4","#bae383","#81bf6c","#11a75f","#06653d"] file_name = uid +'/' + fieldid+'/IndexValues_'+img_type+'.csv' abc = 0 sqm = [] ind_values = [] explode = (0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2) wes1 = 1#print(['file_name', file_name]) with open(file_name,'rt') as f: data = csv.reader(f) for row in data: db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('IndexBreakdown').child(image_date).child(img_type).set(row[1:11]) for values in row: if (abc>0) and (abc<11): sq1 = (float(values)/10000) sq1 = round(sq1,1) ind_values.append(sq1) ss = str(values) + ' sq. m.' sqm.append(ss) if abc==0: avg_ind_value = values if abc==11: field_area = values if abc==12: good_area = values if abc==13: avg_area = values if abc==14: bad_area = values abc = abc+1 fig1,ax1 = plt.subplots() wes1 = 1#print(labels) wes1 = 1#print(ind_values) sizes = ind_values #ax1.set_prop_cycle("color",cmap) #ax1.pie(ind_values,labeldistance = 2, explode=explode, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. ax1.set_title(("Field Area (in square meters) in Different "+lb2+" Ranges")) patch1 = mpatches.Patch(color="#ab0535",label=(labels[0]+' -> '+sqm[0])) patch2 = mpatches.Patch(color="#ea4f3b",label=(labels[1]+' -> '+sqm[1])) patch3 = mpatches.Patch(color="#f7885a",label=(labels[2]+' -> '+sqm[2])) patch4 = mpatches.Patch(color="#fbc07e",label=(labels[3]+' -> '+sqm[3])) patch5 = mpatches.Patch(color="#fff0b5",label=(labels[4]+' -> '+sqm[4])) patch6 = mpatches.Patch(color="#e6f3a4",label=(labels[5]+' -> '+sqm[5])) patch7 = mpatches.Patch(color="#bae383",label=(labels[6]+' -> '+sqm[6])) patch8 = mpatches.Patch(color="#81bf6c",label=(labels[7]+' -> '+sqm[7])) patch9 = mpatches.Patch(color="#11a75f",label=(labels[8]+' -> '+sqm[8])) patch10 = mpatches.Patch(color="#06653d",label=(labels[9]+' -> '+sqm[9])) ax1.legend(loc = 'center',handles=[patch10,patch9,patch8,patch7,patch6,patch5,patch4,patch3,patch2,patch1],title='Field Area in Different Health Zones') plt.show() file_name = uid+'/' + fieldid+'/'+img_type+'_piechart.png' plt.savefig(file_name) plt.close() pie_c = cv2.imread(file_name) pie_c = pie_c[100:400,120:500] cv2.imwrite(file_name,pie_c) return avg_ind_value,field_area