def get_polygon_mask(uid,polygon_id,imageType,coordinates,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong): import math import matplotlib.path from matplotlib.path import Path import numpy as np import cv2 from PIL import Image from merge_contour_images import merge_contour_images from oct2py import octave import collections import os import firebase_admin from firebase_admin import credentials from firebase_admin import db from create_index_piechart import create_index_piechart from create_soc_piechart import create_soc_piechart from merge_mask_img import merge_mask_img from estimate_yield import estimate_yield from merge_mask_img import merge_mask_img # from firebase_admin import messaging coordinates = collections.OrderedDict(sorted(coordinates.items())) # print(coordinates) tci_file_name = uid + "/" +polygon_id + "/" + imageType + ".png" tci = cv2.imread(tci_file_name,0) w_tci,h_tci = tci.shape centerx = round(w_tci/2) centery = round(h_tci/2) zero_lat = fieldmaxlat zero_long = fieldminlong #print('coords') X = [] Y = [] pixelsperlat = h_tci/abs(float(fieldmaxlong)-float(fieldminlong)) pixelsperlong = w_tci/abs(float(fieldmaxlat)-float(fieldminlat)) pformatflag =0 for k,v in coordinates.items(): if k.find("P_")>=0: pformatflag = 1 break temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) x = round((temp_long-zero_long)*pixelsperlat) y = round((zero_lat-temp_lat)*pixelsperlong) # print(x) # print(y) X.append(x) Y.append(y) if pformatflag == 1: X = [] Y = [] ploc = 0 for k,v in coordinates.items(): pointKey = "P_" + str(ploc) ploc = ploc + 1 try: v = coordinates[pointKey] temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) x = round((temp_long-zero_long)*pixelsperlat) y = round((zero_lat-temp_lat)*pixelsperlong) # print(x) # print(y) X.append(x) Y.append(y) except: v = coordinates["a"] temp_lat = v['Latitude'] temp_long = v['Longitude'] temp_lat = float(temp_lat) temp_long = float(temp_long) x = round((temp_long-zero_long)*pixelsperlat) y = round((zero_lat-temp_lat)*pixelsperlong) # print(x) # print(y) X.append(x) Y.append(y) isdir = os.path.isdir(uid) if isdir != True: os.mkdir(uid) poly_m = octave.create_polymask(uid,polygon_id,w_tci,h_tci,X,Y) #polygon_id + "/"+ img_date + "_" + imageType + ".png" #merge_mask_img(uid,(polygon_id + "/"+ img_date + "_" + imageType)) merge_mask_img(uid, imageType, polygon_id)