Skip to content

Commit

Permalink
Merge pull request #55 from hotosm/fix/split-by-sq
Browse files Browse the repository at this point in the history
fix: remove mandatory osm_extracts from split by square
  • Loading branch information
spwoodcock authored Oct 14, 2024
2 parents a1e00af + f86740c commit 2df47cb
Showing 1 changed file with 8 additions and 39 deletions.
47 changes: 8 additions & 39 deletions fmtm_splitter/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,27 @@ 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]
else:
extract_geoms = []

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)
if not clipped_polygon.is_empty:
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 @@ -419,44 +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 not osm_extract:
config_data = dedent(
"""
query:
select:
from:
- nodes
- ways_poly
- ways_line
where:
tags:
highway: not null
building: not null
waterway: not null
railway: not null
aeroway: not null
"""
)
# Must be a BytesIO JSON object
config_bytes = BytesIO(config_data.encode())

pg = PostgresClient(
"underpass",
config_bytes,
)
# The total FeatureCollection area merged by osm-rawdata automatically
extract_geojson = pg.execQuery(
aoi_featcol,
extra_params={"fileName": "fmtm_splitter", "useStWithin": False},
)

else:
if osm_extract:
extract_geojson = FMTMSplitter.input_to_geojson(osm_extract)
if not extract_geojson:
err = "A valid data extract must be provided."
log.error(err)
raise ValueError(err)

# Handle multiple geometries passed
if len(feat_array := aoi_featcol.get("features", [])) > 1:
features = []
Expand Down

0 comments on commit 2df47cb

Please sign in to comment.