Skip to content

Commit

Permalink
add get_osm_geometries_from_xml
Browse files Browse the repository at this point in the history
  • Loading branch information
chrieke committed Apr 22, 2024
1 parent fee1fb3 commit ceb574a
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion prettymapp/osm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from osmnx.features import features_from_polygon
from typing import Union
from pathlib import Path

from osmnx.features import features_from_polygon, features_from_xml
from osmnx import settings
from geopandas import clip, GeoDataFrame
from shapely.geometry import Polygon
Expand All @@ -11,6 +14,9 @@


def get_osm_tags():
"""
Get relevant OSM tags for use with prettymapp
"""
tags: dict = {}
for d in LC_SETTINGS.values(): # type: ignore
for k, v in d.items(): # type: ignore
Expand All @@ -22,6 +28,9 @@ def get_osm_tags():


def cleanup_osm_df(df: GeoDataFrame, aoi: Polygon) -> GeoDataFrame:
"""
Cleanup of queried osm geometries to relevant level for use with prettymapp
"""
df = df.droplevel(level=0)
df = df[~df.geometry.geom_type.isin(["Point", "MultiPoint"])]

Expand Down Expand Up @@ -52,7 +61,27 @@ def cleanup_osm_df(df: GeoDataFrame, aoi: Polygon) -> GeoDataFrame:


def get_osm_geometries(aoi: Polygon) -> GeoDataFrame:
"""
Query OSM features within a polygon geometry.
Args:
aoi: Polygon geometry query boundary.
"""
tags = get_osm_tags()
df = features_from_polygon(polygon=aoi, tags=tags)
df = cleanup_osm_df(df, aoi)
return df


def get_osm_geometries_from_xml(filepath: Union[str, Path], aoi: Union[Polygon, None]=None) -> GeoDataFrame:
"""
Query OSM features in an OSM-formatted XML file.
Args:
filepath: path to file containing OSM XML data
aoi: Optional geographic boundary to filter elements
"""
tags = get_osm_tags()
df = features_from_xml(filepath, polygon=aoi, tags=tags)
df = cleanup_osm_df(df, aoi)
return df

0 comments on commit ceb574a

Please sign in to comment.