Skip to content

Commit

Permalink
Merge pull request #51 from chrieke/add_get_osm_geometries_from_xml
Browse files Browse the repository at this point in the history
Add get osm geometries from xml
  • Loading branch information
chrieke committed Apr 23, 2024
2 parents fee1fb3 + 371a7d9 commit 5c7f9b2
Show file tree
Hide file tree
Showing 4 changed files with 1,122 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ fig = Plot(
fig.savefig("map.jpg")
```

You can also plot exported OSM XML files e.g. from openstreetmap.org:

```python
from prettymapp.osm import get_osm_geometries_from_xml

df = get_osm_geometries_from_xml(filepath="Berlin.osm")
aoi_bounds = df.total_bounds
...
```

To customize the map appearance, use the additional arguments of the [`Plot`](plotting.py#L36) class (e.g. `shape`,
`contour_width` etc.). Check the preconfigured [styles](prettymapp/settings.py#L35) and
webapp [examples](streamlit-prettymapp/examples.json) for inspiration.
39 changes: 35 additions & 4 deletions 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 @@ -21,11 +27,14 @@ def get_osm_tags():
return tags


def cleanup_osm_df(df: GeoDataFrame, aoi: Polygon) -> GeoDataFrame:
def cleanup_osm_df(df: GeoDataFrame, aoi: Union[Polygon, None] = None) -> 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"])]

df = clip(df, aoi)
if aoi is not None:
df = clip(df, aoi)
df = explode_multigeometries(df)

df["landcover_class"] = None
Expand All @@ -52,7 +61,29 @@ 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
Loading

0 comments on commit 5c7f9b2

Please sign in to comment.