Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : remove osm-extracts from split by square #55

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading