Skip to content

Commit

Permalink
fix(helper): add if-branch for GeoSeries
Browse files Browse the repository at this point in the history
convert all formats to GeoDataFrame which is harnessed for GeoJSON creation
  • Loading branch information
SlowMo24 committed Jun 27, 2024
1 parent 1fb110e commit 2b2049e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions ohsome/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def format_bboxes(
)
elif isinstance(bboxes, str):
return bboxes
elif isinstance(bboxes, gpd.GeoDataFrame):
elif isinstance(bboxes, gpd.GeoDataFrame) or isinstance(bboxes, gpd.GeoSeries):
raise OhsomeException(
message="Use the 'bpolys' parameter to specify the boundaries using a geopandas.GeoDataFrame."
message="Use the 'bpolys' parameter to specify the boundaries using a geopandas object."
)
elif isinstance(bboxes, pd.DataFrame):
try:
Expand Down Expand Up @@ -214,21 +214,24 @@ def format_bpolys(
bpolys: Polygons given as geopandas.GeoDataFrame or string formatted as GeoJSON FeatureCollection.
:return:
"""
if isinstance(bpolys, gpd.GeoDataFrame) or isinstance(bpolys, gpd.GeoSeries):
return bpolys.to_json(na="drop")
if isinstance(bpolys, gpd.GeoDataFrame):
return bpolys.to_json(na="drop", show_bbox=False, drop_id=False, to_wgs84=True)
elif isinstance(bpolys, gpd.GeoSeries):
return format_bpolys(bpolys.to_frame("geometry"))
elif isinstance(bpolys, shapely.Polygon) or isinstance(
bpolys, shapely.MultiPolygon
):
return format_bpolys(gpd.GeoDataFrame(geometry=[bpolys]))
return format_bpolys(gpd.GeoDataFrame(geometry=[bpolys], crs="EPSG:4326"))
elif isinstance(bpolys, str):
try:
return format_bpolys(gpd.GeoDataFrame.from_features(json.loads(bpolys)))
return format_bpolys(
gpd.GeoDataFrame.from_features(json.loads(bpolys), crs="EPSG:4326")
)
except Exception as e:
raise OhsomeException(message="Invalid geojson.") from e
else:
raise OhsomeException(
message="bpolys must be a geojson string, a shapely polygonal object or a geopandas "
"object"
message="bpolys must be a geojson string, a shapely polygonal object or a geopandas object"
)


Expand Down

0 comments on commit 2b2049e

Please sign in to comment.