From c1f43de26fa09d900c40e588da935b7061673ad9 Mon Sep 17 00:00:00 2001 From: bacetiner Date: Wed, 13 Mar 2024 18:55:47 -0700 Subject: [PATCH] Added a plotting function to display created geo meshes --- brails/utils/geoTools.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/brails/utils/geoTools.py b/brails/utils/geoTools.py index ef2fc07..337600a 100644 --- a/brails/utils/geoTools.py +++ b/brails/utils/geoTools.py @@ -37,10 +37,11 @@ # Barbaros Cetiner # # Last updated: -# 03-12-2024 +# 03-13-2024 from math import radians, sin, cos, atan2, sqrt from shapely.geometry import Polygon +import matplotlib.pyplot as plt def haversine_dist(p1,p2): """ @@ -108,4 +109,27 @@ def mesh_polygon(polygon, rows:int, cols:int): # a valid cell: if poly.area > 0: rectangles.append(poly.envelope) - return rectangles \ No newline at end of file + return rectangles + +def plot_polygon_cells(bpoly, rectangles, fout=False): + """ + Function that plots the mesh for a polygon and saves the plot into a PNG image + + Inputs: A Shapely polygon and a list of rectangular mesh cells saved as + Shapely polygons. Optional input is a string containing the name of the + output file + """ + + if bpoly.geom_type == 'MultiPolygon': + for poly in bpoly.geoms: + plt.plot(*poly.exterior.xy,'k') + else: + plt.plot(*bpoly.exterior.xy,'k') + for rect in rectangles: + try: + plt.plot(*rect.exterior.xy) + except: + pass + if fout: + plt.savefig(fout, dpi=600, bbox_inches="tight") + plt.show() \ No newline at end of file