Skip to content

Commit 76b7b7a

Browse files
don't use shapefile library and specify where small_area_centroids.shp will be written
1 parent d26f54b commit 76b7b7a

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

schimpy/prepare_schism.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def create_hgrid(s, inputs, logger):
4444
)
4545
small_area_param = section.get("small_areas")
4646
if small_area_param is not None:
47+
small_area_param["prepro_output_dir"] = inputs["prepro_output_dir"]
4748
# This just emits warnings unless the fail threshold is met
4849
small_areas(s.mesh, logger=logger, **small_area_param)
4950

schimpy/small_areas.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ def create_arg_parser():
1919
return parser
2020

2121

22-
def small_areas(mesh, warn=10.0, fail=1.0, logger=None, write_out_smalls=False):
22+
def small_areas(
23+
mesh,
24+
warn=10.0,
25+
fail=1.0,
26+
logger=None,
27+
write_out_smalls=False,
28+
prepro_output_dir="./",
29+
):
2330
if logger is not None:
2431
logger.info(
2532
"Checking for elements with small areas. Thresholds: warn={}, fail={}".format(
@@ -52,21 +59,23 @@ def small_areas(mesh, warn=10.0, fail=1.0, logger=None, write_out_smalls=False):
5259

5360
if areas_sorted[0] < fail:
5461
if write_out_smalls:
55-
import shapefile as shp
62+
import geopandas as gpd
63+
from shapely.geometry import Point
5664

5765
sm_centroids = centroids[areas < fail]
5866
sm_areas = areas[areas < fail]
5967

60-
w = shp.Writer("small_area_centroids.shp", shp.POINT)
61-
w.autoBalance = 1 # ensures gemoetry and attributes match
62-
w.field("FID", "N")
63-
w.field("x", "F", 10, 8)
64-
w.field("y", "F", 10, 8)
65-
w.field("area", "F", 10, 8)
66-
for i, smc in enumerate(sm_centroids):
67-
w.point(smc[0], smc[1]) # write the geometry
68-
w.record(i, smc[0], smc[1], sm_areas[i])
69-
w.close()
68+
gdf = gpd.GeoDataFrame(
69+
{
70+
"FID": range(len(sm_centroids)),
71+
"x": sm_centroids[:, 0],
72+
"y": sm_centroids[:, 1],
73+
"area": sm_areas,
74+
},
75+
geometry=[Point(xy) for xy in sm_centroids],
76+
crs="EPSG:4326", # Change if you know the mesh CRS
77+
)
78+
gdf.to_file(f"{prepro_output_dir}/small_area_centroids.shp")
7079

7180
raise ValueError(
7281
"Mesh contains areas smaller than the failure threshold. Consult the log or printout above for areas and warnings"

0 commit comments

Comments
 (0)