Skip to content

Commit

Permalink
fix: avoid appending empty clipped polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujanadh committed Oct 28, 2024
1 parent 6f0b252 commit 1bc6610
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions fmtm_splitter/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ def splitBySquare( # noqa: N802
xmin, ymin, xmax, ymax = self.aoi.bounds

reference_lat = (ymin + ymax) / 2
length_deg, width_deg = self.meters_to_degrees(reference_lat, meters)
length_deg, width_deg = self.meters_to_degrees(meters, reference_lat)

# Create grid columns and rows based on the AOI bounds
cols = np.arange(xmin, xmax + width_deg, width_deg)
rows = np.arange(ymin, ymax + length_deg, length_deg)
polygons = []

extract_geoms = []
if extract_geojson:
features = (
Expand All @@ -226,13 +226,18 @@ def splitBySquare( # noqa: N802
)
extract_geoms = [shape(feature["geometry"]) for feature in features]

# Generate grid polygons and clip them by AOI
polygons = []
for x in cols[:-1]:
for y in rows[:-1]:
grid_polygon = box(x, y, x + width_deg, y + length_deg)
clipped_polygon = grid_polygon.intersection(self.aoi)

if clipped_polygon.is_empty:
continue

# Check intersection with extract geometries if available
if extract_geoms:
# Check if any extract geometry is within the clipped grid
if any(geom.within(clipped_polygon) for geom in extract_geoms):
polygons.append(clipped_polygon)
else:
Expand Down

0 comments on commit 1bc6610

Please sign in to comment.