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(helper): GeoSeries not working as bpolys input #158

Merged
merged 6 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 15 additions & 10 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 All @@ -204,29 +204,34 @@ def format_bboxes(


def format_bpolys(
bpolys: Union[gpd.GeoDataFrame, shapely.Polygon, shapely.MultiPolygon, str]
bpolys: Union[
gpd.GeoDataFrame, gpd.GeoSeries, shapely.Polygon, shapely.MultiPolygon, str
]
) -> str:
"""
Formats bpolys parameter to comply with ohsome API
:param
bpolys: Polygons given as geopandas.GeoDataFrame or string formatted as GeoJSON FeatureCollection.
bpolys: Polygons given as geopandas.GeoDataFrame, geopandas.GeoSeries, Shapely.Polygon or GeoJSON FeatureCollection as string.
: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"))
SlowMo24 marked this conversation as resolved.
Show resolved Hide resolved
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"))
SlowMo24 marked this conversation as resolved.
Show resolved Hide resolved
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
59 changes: 0 additions & 59 deletions ohsome/test/cassettes/test_client/test_format_bpolys.yaml

This file was deleted.

16 changes: 1 addition & 15 deletions ohsome/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,6 @@ def test_format_bcircles_geodataframe_geometry_error(base_client):
del client


@pytest.mark.vcr
def test_format_bpolys(base_client):
"""
Test whether a GeoDataFrame obejct is formatted correctly for ohsome api.
:return:
"""
bpolys = gpd.read_file(f"{script_path}/data/polygons.geojson")
time = "2018-01-01"
fltr = "amenity=restaurant and type:node"

client = base_client
client.elements.count.post(bpolys=bpolys, time=time, filter=fltr)


@pytest.mark.vcr
def test_format_bboxes_dataframe(base_client):
"""
Expand Down Expand Up @@ -254,7 +240,7 @@ def test_format_bboxes_geodataframe(base_client):

assert (
"Use the 'bpolys' parameter to specify the boundaries using a "
"geopandas.GeoDataFrame." in e_info.value.message
"geopandas object." in e_info.value.message
)


Expand Down
40 changes: 33 additions & 7 deletions ohsome/test/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import os

import geopandas as gpd
import numpy as np
import pandas as pd
import pytest
Expand Down Expand Up @@ -225,13 +226,38 @@ def test_format_bpolys():
== "bolys must be a geojson string, a shapely polygonal object or a geopandas object"
)

with open(f"{script_path}/data/polygons.geojson") as geojson:
json_str = geojson.read()
assert json.loads(format_bpolys(json_str)) == json.loads(json_str)
geojson = json.dumps(
{
"type": "FeatureCollection",
"features": [
{
"id": "0",
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0]]
],
},
}
],
}
)

assert format_bpolys(geojson) == geojson

polygon = Polygon(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)))
assert format_bpolys(polygon) == (
'{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", '
'"properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[0.0, '
"0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0]]]}}]}"
assert format_bpolys(polygon) == geojson

series = gpd.GeoSeries(
data=[polygon],
crs="EPSG:4326",
)
assert format_bpolys(series) == geojson

df = gpd.GeoDataFrame(
geometry=[polygon],
crs="EPSG:4326",
)
assert format_bpolys(df) == geojson
Loading