Skip to content

Commit

Permalink
refix: allow custom extracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujanadh committed Oct 14, 2024
1 parent 7aa5ebb commit 11e3fd8
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions fmtm_splitter/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def geojson_to_shapely_polygon(
def splitBySquare( # noqa: N802
self,
meters: int,
extract_geojson: Optional[Union[dict, FeatureCollection]] = None,
) -> FeatureCollection:
"""Split the polygon into squares.
Expand All @@ -177,13 +178,28 @@ def splitBySquare( # noqa: N802
cols = list(np.arange(xmin, xmax + width, width))
rows = list(np.arange(ymin, ymax + length, length))
polygons = []
extract_geoms = []
if extract_geojson:
features = (
extract_geojson.get("features", extract_geojson)
if isinstance(extract_geojson, dict)
else extract_geojson.features
)
extract_geoms = [shape(feature["geometry"]) for feature in features]

for x in cols[:-1]:
for y in rows[:-1]:
grid_polygon = Polygon(
[(x, y), (x + width, y), (x + width, y + length), (x, y + length)]
)
clipped_polygon = grid_polygon.intersection(self.aoi)
polygons.append(clipped_polygon)
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:
polygons.append(clipped_polygon)

self.split_features = FeatureCollection(
[Feature(geometry=mapping(poly)) for poly in polygons]
)
Expand Down Expand Up @@ -382,6 +398,7 @@ def outputGeojson( # noqa: N802
def split_by_square(
aoi: Union[str, FeatureCollection],
meters: int = 100,
osm_extract: Union[str, FeatureCollection] = None,
outfile: Optional[str] = None,
) -> FeatureCollection:
"""Split an AOI by square, dividing into an even grid.
Expand All @@ -404,7 +421,11 @@ def split_by_square(
# Parse AOI
parsed_aoi = FMTMSplitter.input_to_geojson(aoi)
aoi_featcol = FMTMSplitter.geojson_to_featcol(parsed_aoi)
extract_geojson = None

if osm_extract:
extract_geojson = FMTMSplitter.input_to_geojson(osm_extract)

# Handle multiple geometries passed
if len(feat_array := aoi_featcol.get("features", [])) > 1:
features = []
Expand All @@ -421,7 +442,7 @@ def split_by_square(
split_features = FeatureCollection(features)
else:
splitter = FMTMSplitter(aoi_featcol)
split_features = splitter.splitBySquare(meters)
split_features = splitter.splitBySquare(meters, extract_geojson)
if not split_features:
msg = "Failed to generate split features."
log.error(msg)
Expand Down

0 comments on commit 11e3fd8

Please sign in to comment.