diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f141b..8f218bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - support for python 3.12 - custom [retry](https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.Retry) configuration - start and end timestamp meta information of the client are now datetime objects + - accept shapely Polygon and MultiPolygon for `bpolys` input parameter + - if a request fails a bash script containing the respective `curl` command is logged (if possible). This allows for easier debugging and sharing of failed requests. ### Removed diff --git a/ohsome/clients.py b/ohsome/clients.py index 23bc061..6ebfcc9 100644 --- a/ohsome/clients.py +++ b/ohsome/clients.py @@ -5,11 +5,14 @@ import datetime as dt import json from pathlib import Path -from typing import Union, Optional +from typing import Union, Optional, List from urllib.parse import urljoin import datetime as dt +import geopandas as gpd +import pandas as pd import requests +import shapely from requests import Session from requests.adapters import HTTPAdapter from requests.exceptions import RetryError @@ -235,22 +238,53 @@ def __init__( def post( self, - bboxes=None, - bcircles=None, - bpolys=None, - time=None, - filter=None, - filter2=None, - format=None, - showMetadata=None, - timeout=None, - groupByKey=None, - groupByKeys=None, - groupByValues=None, - properties=None, - clipGeometry=None, - endpoint=None, - ): + bboxes: Optional[ + Union[ + str, + dict, + pd.DataFrame, + List[str], + List[float], + List[List[str]], + List[List[float]], + ] + ] = None, + bcircles: Optional[ + Union[ + str, + List[str], + List[float], + List[List[str]], + List[List[float]], + dict, + gpd.GeoDataFrame, + pd.DataFrame, + ] + ] = None, + bpolys: Optional[ + Union[ + gpd.GeoDataFrame, + gpd.GeoSeries, + shapely.Polygon, + shapely.MultiPolygon, + str, + ] + ] = None, + time: Optional[ + Union[str, dt.datetime, dt.date, list, pd.DatetimeIndex, pd.Series] + ] = None, + filter: Optional[str] = None, + filter2: Optional[str] = None, + format: Optional[str] = None, + showMetadata: Optional[bool] = None, + timeout: Optional[int] = None, + groupByKey: Optional[str] = None, + groupByKeys: Optional[Union[str, List[str]]] = None, + groupByValues: Optional[Union[str, List[str]]] = None, + properties: Optional[Union[str, List[str]]] = None, + clipGeometry: Optional[bool] = None, + endpoint: Optional[str] = None, + ) -> OhsomeResponse: """ Sends request to ohsome API @@ -267,7 +301,7 @@ def post( - pandas.DataFrame with columns 'lon', 'lat' and 'radius' - geopandas.GeoDataFrame with geometry column with Point geometries only and a column 'radius'. - :param bpolys: Polygons given as geopandas.GeoDataFrame, GeoJSON FeatureCollection or str + :param bpolys: Polygons given as geopandas.GeoDataFrame, geopandas.GeoSeries, shapely.Polygon, shapely.MultiPolygon, GeoJSON FeatureCollection or str e.g. "8.65821,49.41129,8.65821,49.41825,8.70053,8.65821|8.67817,49.42147,8.67817,49.4342,8.67817" :param time: One or more ISO-8601 conform timestring(s) given as str, list, pandas.Series, pandas.DateTimeIndex, diff --git a/ohsome/exceptions.py b/ohsome/exceptions.py index e5725fb..efc38d1 100644 --- a/ohsome/exceptions.py +++ b/ohsome/exceptions.py @@ -7,6 +7,8 @@ import json from pathlib import Path +from curlify2 import Curlify + class OhsomeException(Exception): """Exception to handle ohsome API errors""" @@ -33,8 +35,17 @@ def log(self, log_dir: Path): self.log_bpolys(log_dir, log_file_name) self.log_parameter(log_dir, log_file_name) if self.response is not None: + self.log_curl(log_dir, log_file_name) self.log_response(log_dir, log_file_name) + def log_curl(self, log_dir: Path, log_file_name: str) -> None: + """Log the respective curl command for the request for easy debugging and sharing.""" + log_file = log_dir / f"{log_file_name}_curl.sh" + curl = Curlify(self.response.request) + curl_command = curl.to_curl() + with log_file.open(mode="w") as dst: + dst.write(curl_command) + def log_response(self, log_dir: Path, log_file_name: str): """ Log raw response. This may duplicate much data but is helpful for debugging to know the exact raw answer by the diff --git a/ohsome/helper.py b/ohsome/helper.py index ae4e2c0..57dc257 100644 --- a/ohsome/helper.py +++ b/ohsome/helper.py @@ -4,12 +4,14 @@ """Ohsome utility functions""" import datetime +import json import re -from typing import Tuple +from typing import Tuple, Union, List import geopandas as gpd import numpy as np import pandas as pd +import shapely from ohsome import OhsomeException @@ -73,7 +75,18 @@ def format_boundary(params: dict) -> dict: return params -def format_bcircles(bcircles): +def format_bcircles( + bcircles: Union[ + str, + List[str], + List[float], + List[List[str]], + List[List[float]], + dict, + gpd.GeoDataFrame, + pd.DataFrame, + ] +) -> str: """ Formats bcircles parameter to comply with ohsome API :param @@ -103,9 +116,12 @@ def format_bcircles(bcircles): ] ) elif isinstance(bcircles, gpd.GeoDataFrame): - if bcircles.geometry.geom_type.unique() != ["Point"]: + if (bcircles.geometry.geom_type.unique() != ["Point"]) or ( + "radius" not in bcircles.columns + ): raise OhsomeException( - message="The geometry of the 'bcircles' GeoDataFrame may only include 'Point' geometry types." + message="The geometry of the 'bcircles' GeoDataFrame may only include 'Point' geometry types and " + "requires a 'radius' column." ) formatted = bcircles.apply( lambda r: f"{int(r.name)}:{r.geometry.x},{r.geometry.y},{r['radius']}", @@ -127,7 +143,17 @@ def format_bcircles(bcircles): raise OhsomeException(message="'bcircles' parameter has invalid format.") -def format_bboxes(bboxes): +def format_bboxes( + bboxes: Union[ + str, + dict, + pd.DataFrame, + List[str], + List[float], + List[List[str]], + List[List[float]], + ] +) -> str: """ Formats bboxes parameter to comply with ohsome API :param @@ -177,17 +203,31 @@ def format_bboxes(bboxes): ) -def format_bpolys(bpolys): +def format_bpolys( + bpolys: Union[gpd.GeoDataFrame, 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. :return: """ - if isinstance(bpolys, gpd.GeoDataFrame): + if isinstance(bpolys, gpd.GeoDataFrame) or isinstance(bpolys, gpd.GeoSeries): return bpolys.to_json(na="drop") + elif isinstance(bpolys, shapely.Polygon) or isinstance( + bpolys, shapely.MultiPolygon + ): + return format_bpolys(gpd.GeoDataFrame(geometry=[bpolys])) + elif isinstance(bpolys, str): + try: + return format_bpolys(gpd.GeoDataFrame.from_features(json.loads(bpolys))) + except Exception as e: + raise OhsomeException(message="Invalid geojson.") from e else: - return bpolys + raise OhsomeException( + message="bpolys must be a geojson string, a shapely polygonal object or a geopandas " + "object" + ) def format_list_parameters(parameters: dict) -> dict: diff --git a/ohsome/test/cassettes/test_client/test_check_time_parameter_datetime.yaml b/ohsome/test/cassettes/test_client/test_check_time_parameter_datetime.yaml index 4666000..86751af 100644 --- a/ohsome/test/cassettes/test_client/test_check_time_parameter_datetime.yaml +++ b/ohsome/test/cassettes/test_client/test_check_time_parameter_datetime.yaml @@ -43,7 +43,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:23 GMT + - Fri, 17 Nov 2023 15:37:04 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_check_time_parameter_list.yaml b/ohsome/test/cassettes/test_client/test_check_time_parameter_list.yaml index af18d88..bfb2f4c 100644 --- a/ohsome/test/cassettes/test_client/test_check_time_parameter_list.yaml +++ b/ohsome/test/cassettes/test_client/test_check_time_parameter_list.yaml @@ -44,7 +44,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:23 GMT + - Fri, 17 Nov 2023 15:37:04 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_end_timestamp_as_time_input.yaml b/ohsome/test/cassettes/test_client/test_end_timestamp_as_time_input.yaml index 099f942..ef39cb8 100644 --- a/ohsome/test/cassettes/test_client/test_end_timestamp_as_time_input.yaml +++ b/ohsome/test/cassettes/test_client/test_end_timestamp_as_time_input.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:26 GMT + - Fri, 17 Nov 2023 15:37:06 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_format_bboxes_dataframe.yaml b/ohsome/test/cassettes/test_client/test_format_bboxes_dataframe.yaml index ef04d0f..4702274 100644 --- a/ohsome/test/cassettes/test_client/test_format_bboxes_dataframe.yaml +++ b/ohsome/test/cassettes/test_client/test_format_bboxes_dataframe.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:32 GMT + - Fri, 17 Nov 2023 15:37:13 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_format_bboxes_list.yaml b/ohsome/test/cassettes/test_client/test_format_bboxes_list.yaml index d4be934..cda57d1 100644 --- a/ohsome/test/cassettes/test_client/test_format_bboxes_list.yaml +++ b/ohsome/test/cassettes/test_client/test_format_bboxes_list.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:32 GMT + - Fri, 17 Nov 2023 15:37:13 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -99,7 +99,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:34 GMT + - Fri, 17 Nov 2023 15:37:14 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -156,7 +156,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:34 GMT + - Fri, 17 Nov 2023 15:37:14 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -213,7 +213,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:36 GMT + - Fri, 17 Nov 2023 15:37:16 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -270,7 +270,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:36 GMT + - Fri, 17 Nov 2023 15:37:16 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_format_bcircles_dataframe.yaml b/ohsome/test/cassettes/test_client/test_format_bcircles_dataframe.yaml index e841da2..e44fe91 100644 --- a/ohsome/test/cassettes/test_client/test_format_bcircles_dataframe.yaml +++ b/ohsome/test/cassettes/test_client/test_format_bcircles_dataframe.yaml @@ -45,7 +45,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:26 GMT + - Fri, 17 Nov 2023 15:37:06 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_format_bcircles_list.yaml b/ohsome/test/cassettes/test_client/test_format_bcircles_list.yaml index 75564e6..f48fd95 100644 --- a/ohsome/test/cassettes/test_client/test_format_bcircles_list.yaml +++ b/ohsome/test/cassettes/test_client/test_format_bcircles_list.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:27 GMT + - Fri, 17 Nov 2023 15:37:08 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -102,7 +102,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:27 GMT + - Fri, 17 Nov 2023 15:37:08 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -159,7 +159,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:29 GMT + - Fri, 17 Nov 2023 15:37:09 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -216,7 +216,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:29 GMT + - Fri, 17 Nov 2023 15:37:09 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_format_bcircles_geodataframe.yaml b/ohsome/test/cassettes/test_client/test_format_bcircles_pandas.yaml similarity index 98% rename from ohsome/test/cassettes/test_client/test_format_bcircles_geodataframe.yaml rename to ohsome/test/cassettes/test_client/test_format_bcircles_pandas.yaml index 5d21cf5..ba88e8b 100644 --- a/ohsome/test/cassettes/test_client/test_format_bcircles_geodataframe.yaml +++ b/ohsome/test/cassettes/test_client/test_format_bcircles_pandas.yaml @@ -51,7 +51,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:31 GMT + - Fri, 17 Nov 2023 15:37:11 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_format_bpolys.yaml b/ohsome/test/cassettes/test_client/test_format_bpolys.yaml index 50027e8..a1a74bb 100644 --- a/ohsome/test/cassettes/test_client/test_format_bpolys.yaml +++ b/ohsome/test/cassettes/test_client/test_format_bpolys.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: bpolys=%7B%22type%22%3A+%22FeatureCollection%22%2C+%22features%22%3A+%5B%7B%22id%22%3A+%220%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.695077896118164%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.408711468953854%5D%5D%5D%7D%7D%2C+%7B%22id%22%3A+%221%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.677010536193848%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.41370947536709%5D%5D%5D%7D%7D%5D%7D&time=2018-01-01&filter=amenity%3Drestaurant+and+type%3Anode + body: bpolys=%7B%22type%22%3A+%22FeatureCollection%22%2C+%22features%22%3A+%5B%7B%22id%22%3A+%220%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%22id%22%3A+%220%22%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.695077896118164%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.408711468953854%5D%5D%5D%7D%7D%2C+%7B%22id%22%3A+%221%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%22id%22%3A+%221%22%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.677010536193848%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.41370947536709%5D%5D%5D%7D%7D%5D%7D&time=2018-01-01&filter=amenity%3Drestaurant+and+type%3Anode headers: Accept: - '*/*' @@ -9,7 +9,7 @@ interactions: Connection: - keep-alive Content-Length: - - '975' + - '1013' Content-Type: - application/x-www-form-urlencoded user-agent: @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:31 GMT + - Fri, 17 Nov 2023 15:37:11 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_post_with_endpoint_string.yaml b/ohsome/test/cassettes/test_client/test_post_with_endpoint_string.yaml index 35e399e..ba1c230 100644 --- a/ohsome/test/cassettes/test_client/test_post_with_endpoint_string.yaml +++ b/ohsome/test/cassettes/test_client/test_post_with_endpoint_string.yaml @@ -50,7 +50,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:28:37 GMT + - Fri, 17 Nov 2023 15:37:17 GMT Keep-Alive: - timeout=5, max=100 Server: @@ -115,7 +115,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:28:37 GMT + - Fri, 17 Nov 2023 15:37:17 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_client/test_user_agent.yaml b/ohsome/test/cassettes/test_client/test_user_agent.yaml index 6b0bef1..639317f 100644 --- a/ohsome/test/cassettes/test_client/test_user_agent.yaml +++ b/ohsome/test/cassettes/test_client/test_user_agent.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:22 GMT + - Fri, 17 Nov 2023 15:37:03 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_exceptions/test_disable_logging.yaml b/ohsome/test/cassettes/test_exceptions/test_disable_logging.yaml index 21d9dc8..2c98cf0 100644 --- a/ohsome/test/cassettes/test_exceptions/test_disable_logging.yaml +++ b/ohsome/test/cassettes/test_exceptions/test_disable_logging.yaml @@ -18,7 +18,7 @@ interactions: uri: https://api.ohsome.org/v1/elements/geometry response: body: - string: "{\n \"timestamp\" : \"2023-11-17T11:28:39.812288772\",\n \"status\" + string: "{\n \"timestamp\" : \"2023-11-17T15:37:19.579114653\",\n \"status\" : 413,\n \"message\" : \"The given query is too large in respect to the given timeout. Please use a smaller region and/or coarser time period.\",\n \"requestUrl\" : \"https://api.ohsome.org/v1/elements/geometry\"\n}" @@ -44,7 +44,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:28:39 GMT + - Fri, 17 Nov 2023 15:37:19 GMT Server: - Apache Strict-Transport-Security: diff --git a/ohsome/test/cassettes/test_exceptions/test_exception_connection_reset.yaml b/ohsome/test/cassettes/test_exceptions/test_exception_connection_reset.yaml index 941d34c..5d557c9 100644 --- a/ohsome/test/cassettes/test_exceptions/test_exception_connection_reset.yaml +++ b/ohsome/test/cassettes/test_exceptions/test_exception_connection_reset.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: bpolys=%7B%22type%22%3A+%22FeatureCollection%22%2C+%22features%22%3A+%5B%7B%22id%22%3A+%220%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.695077896118164%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.408711468953854%5D%5D%5D%7D%7D%2C+%7B%22id%22%3A+%221%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.677010536193848%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.41370947536709%5D%5D%5D%7D%7D%5D%7D&time=2018-01-01&filter=name%3DKrautturm+and+type%3Away + body: bpolys=%7B%22type%22%3A+%22FeatureCollection%22%2C+%22features%22%3A+%5B%7B%22id%22%3A+%220%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%22id%22%3A+%220%22%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.695077896118164%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.408711468953854%5D%5D%5D%7D%7D%2C+%7B%22id%22%3A+%221%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%22id%22%3A+%221%22%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.677010536193848%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.41370947536709%5D%5D%5D%7D%7D%5D%7D&time=2018-01-01&filter=name%3DKrautturm+and+type%3Away headers: Accept: - '*/*' @@ -9,7 +9,7 @@ interactions: Connection: - keep-alive Content-Length: - - '970' + - '1008' Content-Type: - application/x-www-form-urlencoded user-agent: @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:47 GMT + - Fri, 17 Nov 2023 15:37:26 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_exceptions/test_exception_invalid_parameters.yaml b/ohsome/test/cassettes/test_exceptions/test_exception_invalid_parameters.yaml index 627109b..e993adc 100644 --- a/ohsome/test/cassettes/test_exceptions/test_exception_invalid_parameters.yaml +++ b/ohsome/test/cassettes/test_exceptions/test_exception_invalid_parameters.yaml @@ -18,7 +18,7 @@ interactions: uri: https://api.ohsome.org/v1/elements/count/groupBy/tag response: body: - string: "{\n \"timestamp\" : \"2023-11-17T11:28:47.569104003\",\n \"status\" + string: "{\n \"timestamp\" : \"2023-11-17T15:37:26.890336955\",\n \"status\" : 400,\n \"message\" : \"You need to give one groupByKey parameter, if you want to use groupBy/tag.\",\n \"requestUrl\" : \"https://api.ohsome.org/v1/elements/count/groupBy/tag\"\n}" headers: @@ -41,7 +41,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:47 GMT + - Fri, 17 Nov 2023 15:37:26 GMT Server: - Apache Strict-Transport-Security: diff --git a/ohsome/test/cassettes/test_exceptions/test_invalid_url.yaml b/ohsome/test/cassettes/test_exceptions/test_invalid_url.yaml index d54ba88..7881737 100644 --- a/ohsome/test/cassettes/test_exceptions/test_invalid_url.yaml +++ b/ohsome/test/cassettes/test_exceptions/test_invalid_url.yaml @@ -38,7 +38,7 @@ interactions: Content-Type: - text/html Date: - - Fri, 17 Nov 2023 11:28:39 GMT + - Fri, 17 Nov 2023 15:37:19 GMT ETag: - '"25c-5c7180820e5fc"' Keep-Alive: diff --git a/ohsome/test/cassettes/test_exceptions/test_log_bpolys.yaml b/ohsome/test/cassettes/test_exceptions/test_log_bpolys.yaml index 5201dff..36cbd89 100644 --- a/ohsome/test/cassettes/test_exceptions/test_log_bpolys.yaml +++ b/ohsome/test/cassettes/test_exceptions/test_log_bpolys.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: bpolys=%7B%22type%22%3A+%22FeatureCollection%22%2C+%22features%22%3A+%5B%7B%22id%22%3A+%220%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.695077896118164%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.408711468953854%5D%5D%5D%7D%7D%2C+%7B%22id%22%3A+%221%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.677010536193848%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.41370947536709%5D%5D%5D%7D%7D%5D%7D&time=2018-01-01&filter=amenity%3Drestaurant+and+type%3Anode&timeout=0.001 + body: bpolys=%7B%22type%22%3A+%22FeatureCollection%22%2C+%22features%22%3A+%5B%7B%22id%22%3A+%220%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%22id%22%3A+%220%22%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.695077896118164%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.408711468953854%5D%2C+%5B8.699712753295898%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.41155955732304%5D%2C+%5B8.695077896118164%2C+49.408711468953854%5D%5D%5D%7D%7D%2C+%7B%22id%22%3A+%221%22%2C+%22type%22%3A+%22Feature%22%2C+%22properties%22%3A+%7B%22id%22%3A+%221%22%7D%2C+%22geometry%22%3A+%7B%22type%22%3A+%22Polygon%22%2C+%22coordinates%22%3A+%5B%5B%5B8.677010536193848%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.41370947536709%5D%2C+%5B8.682074546813965%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.416641030041134%5D%2C+%5B8.677010536193848%2C+49.41370947536709%5D%5D%5D%7D%7D%5D%7D&time=2018-01-01&filter=amenity%3Drestaurant+and+type%3Anode&timeout=0.001 headers: Accept: - '*/*' @@ -9,7 +9,7 @@ interactions: Connection: - keep-alive Content-Length: - - '989' + - '1027' Content-Type: - application/x-www-form-urlencoded user-agent: @@ -18,7 +18,7 @@ interactions: uri: https://api.ohsome.org/v1/elements/count response: body: - string: "{\n \"timestamp\" : \"2023-11-17T11:28:40.637302314\",\n \"status\" + string: "{\n \"timestamp\" : \"2023-11-17T15:37:20.131942698\",\n \"status\" : 413,\n \"message\" : \"The given query is too large in respect to the given timeout. Please use a smaller region and/or coarser time period.\",\n \"requestUrl\" : \"https://api.ohsome.org/v1/elements/count\"\n}" @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:39 GMT + - Fri, 17 Nov 2023 15:37:20 GMT Server: - Apache Strict-Transport-Security: diff --git a/ohsome/test/cassettes/test_exceptions/test_log_curl.yaml b/ohsome/test/cassettes/test_exceptions/test_log_curl.yaml new file mode 100644 index 0000000..270b745 --- /dev/null +++ b/ohsome/test/cassettes/test_exceptions/test_log_curl.yaml @@ -0,0 +1,57 @@ +interactions: +- request: + body: bboxes=8.67555%2C49.39885%2C8.69637%2C49.41122&timeout=0.001 + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '60' + Content-Type: + - application/x-www-form-urlencoded + user-agent: + - ohsome-py/0.2.0 + method: POST + uri: https://api.ohsome.org/v1/elements/count + response: + body: + string: "{\n \"timestamp\" : \"2023-11-17T12:28:28.06766606\",\n \"status\" + : 413,\n \"message\" : \"The given query is too large in respect to the given + timeout. Please use a smaller region and/or coarser time period.\",\n \"requestUrl\" + : \"https://api.ohsome.org/v1/elements/count\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization + Access-Control-Allow-Methods: + - POST, GET + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Cache-Control: + - no-cache, no-store, must-revalidate + Connection: + - close + Content-Encoding: + - gzip + Content-Type: + - application/json + Date: + - Fri, 17 Nov 2023 12:28:27 GMT + Server: + - Apache + Strict-Transport-Security: + - max-age=63072000; includeSubdomains; + Transfer-Encoding: + - chunked + vary: + - accept-encoding + status: + code: 413 + message: '' +version: 1 diff --git a/ohsome/test/cassettes/test_exceptions/test_timeout_error.yaml b/ohsome/test/cassettes/test_exceptions/test_timeout_error.yaml index 1795c1a..163c2f9 100644 --- a/ohsome/test/cassettes/test_exceptions/test_timeout_error.yaml +++ b/ohsome/test/cassettes/test_exceptions/test_timeout_error.yaml @@ -18,7 +18,7 @@ interactions: uri: https://api.ohsome.org/v1/elements/geometry response: body: - string: "{\n \"timestamp\" : \"2023-11-17T11:28:39.141569932\",\n \"status\" + string: "{\n \"timestamp\" : \"2023-11-17T15:37:19.028799445\",\n \"status\" : 413,\n \"message\" : \"The given query is too large in respect to the given timeout. Please use a smaller region and/or coarser time period.\",\n \"requestUrl\" : \"https://api.ohsome.org/v1/elements/geometry\"\n}" @@ -44,7 +44,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:28:38 GMT + - Fri, 17 Nov 2023 15:37:19 GMT Server: - Apache Strict-Transport-Security: diff --git a/ohsome/test/cassettes/test_response/test_contributions_centroid.yaml b/ohsome/test/cassettes/test_response/test_contributions_centroid.yaml index 0a90ac6..851930f 100644 --- a/ohsome/test/cassettes/test_response/test_contributions_centroid.yaml +++ b/ohsome/test/cassettes/test_response/test_contributions_centroid.yaml @@ -46,7 +46,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:29:01 GMT + - Fri, 17 Nov 2023 15:37:41 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_contributions_latest.yaml b/ohsome/test/cassettes/test_response/test_contributions_latest.yaml index ff9027b..fed5680 100644 --- a/ohsome/test/cassettes/test_response/test_contributions_latest.yaml +++ b/ohsome/test/cassettes/test_response/test_contributions_latest.yaml @@ -50,7 +50,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:29:01 GMT + - Fri, 17 Nov 2023 15:37:41 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elementsFullHistory_geometry.yaml b/ohsome/test/cassettes/test_response/test_elementsFullHistory_geometry.yaml index 2a676f0..9794e29 100644 --- a/ohsome/test/cassettes/test_response/test_elementsFullHistory_geometry.yaml +++ b/ohsome/test/cassettes/test_response/test_elementsFullHistory_geometry.yaml @@ -50,7 +50,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:28:59 GMT + - Fri, 17 Nov 2023 15:37:39 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count.yaml b/ohsome/test/cassettes/test_response/test_elements_count.yaml index 20b765e..f26a8e6 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:49 GMT + - Fri, 17 Nov 2023 15:37:28 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary.yaml b/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary.yaml index dc1d8b3..cf9a080 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary.yaml @@ -45,7 +45,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:54 GMT + - Fri, 17 Nov 2023 15:37:35 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary_groupby_tag.yaml b/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary_groupby_tag.yaml index ed05b48..de57016 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary_groupby_tag.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_groupby_boundary_groupby_tag.yaml @@ -45,7 +45,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:56 GMT + - Fri, 17 Nov 2023 15:37:35 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_groupby_key.yaml b/ohsome/test/cassettes/test_response/test_elements_count_groupby_key.yaml index 4bc8fea..463059d 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_groupby_key.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_groupby_key.yaml @@ -50,7 +50,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:50 GMT + - Fri, 17 Nov 2023 15:37:30 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_groupby_tag.yaml b/ohsome/test/cassettes/test_response/test_elements_count_groupby_tag.yaml index bc5017e..2bb7b0e 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_groupby_tag.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_groupby_tag.yaml @@ -128,7 +128,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:52 GMT + - Fri, 17 Nov 2023 15:37:32 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_groupby_type.yaml b/ohsome/test/cassettes/test_response/test_elements_count_groupby_type.yaml index 748ba3f..0ee0297 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_groupby_type.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_groupby_type.yaml @@ -50,7 +50,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:54 GMT + - Fri, 17 Nov 2023 15:37:34 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_ratio.yaml b/ohsome/test/cassettes/test_response/test_elements_count_ratio.yaml index 1e8c9c8..fc55c5f 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_ratio.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_ratio.yaml @@ -43,7 +43,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:56 GMT + - Fri, 17 Nov 2023 15:37:36 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_count_ratio_groupby_boundary.yaml b/ohsome/test/cassettes/test_response/test_elements_count_ratio_groupby_boundary.yaml index 87837f7..6d5b0fa 100644 --- a/ohsome/test/cassettes/test_response/test_elements_count_ratio_groupby_boundary.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_count_ratio_groupby_boundary.yaml @@ -50,7 +50,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:57 GMT + - Fri, 17 Nov 2023 15:37:37 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_density.yaml b/ohsome/test/cassettes/test_response/test_elements_density.yaml index fc5fb0c..33d57ec 100644 --- a/ohsome/test/cassettes/test_response/test_elements_density.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_density.yaml @@ -42,7 +42,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:49 GMT + - Fri, 17 Nov 2023 15:37:28 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_elements_geometry.yaml b/ohsome/test/cassettes/test_response/test_elements_geometry.yaml index b9138aa..cb50762 100644 --- a/ohsome/test/cassettes/test_response/test_elements_geometry.yaml +++ b/ohsome/test/cassettes/test_response/test_elements_geometry.yaml @@ -79,7 +79,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:28:57 GMT + - Fri, 17 Nov 2023 15:37:37 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_empty_geodataframe.yaml b/ohsome/test/cassettes/test_response/test_empty_geodataframe.yaml index b7cb90b..6c03783 100644 --- a/ohsome/test/cassettes/test_response/test_empty_geodataframe.yaml +++ b/ohsome/test/cassettes/test_response/test_empty_geodataframe.yaml @@ -41,7 +41,7 @@ interactions: Content-disposition: - attachment;filename=ohsome.geojson Date: - - Fri, 17 Nov 2023 11:29:03 GMT + - Fri, 17 Nov 2023 15:37:42 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_multi_index_false.yaml b/ohsome/test/cassettes/test_response/test_multi_index_false.yaml index bc5017e..2bb7b0e 100644 --- a/ohsome/test/cassettes/test_response/test_multi_index_false.yaml +++ b/ohsome/test/cassettes/test_response/test_multi_index_false.yaml @@ -128,7 +128,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:52 GMT + - Fri, 17 Nov 2023 15:37:32 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_not_implemented_query.yaml b/ohsome/test/cassettes/test_response/test_not_implemented_query.yaml index 4bc8fea..463059d 100644 --- a/ohsome/test/cassettes/test_response/test_not_implemented_query.yaml +++ b/ohsome/test/cassettes/test_response/test_not_implemented_query.yaml @@ -50,7 +50,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:50 GMT + - Fri, 17 Nov 2023 15:37:30 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/cassettes/test_response/test_users_timestamp.yaml b/ohsome/test/cassettes/test_response/test_users_timestamp.yaml index 78c222a..7120779 100644 --- a/ohsome/test/cassettes/test_response/test_users_timestamp.yaml +++ b/ohsome/test/cassettes/test_response/test_users_timestamp.yaml @@ -43,7 +43,7 @@ interactions: Content-Type: - application/json Date: - - Fri, 17 Nov 2023 11:28:59 GMT + - Fri, 17 Nov 2023 15:37:39 GMT Keep-Alive: - timeout=5, max=100 Server: diff --git a/ohsome/test/data/polygons.geojson b/ohsome/test/data/polygons.geojson index 5a25342..f208fc5 100644 --- a/ohsome/test/data/polygons.geojson +++ b/ohsome/test/data/polygons.geojson @@ -4,6 +4,7 @@ { "type": "Feature", "properties": {}, + "id": "0", "geometry": { "type": "Polygon", "coordinates": [ @@ -35,6 +36,7 @@ { "type": "Feature", "properties": {}, + "id": "1", "geometry": { "type": "Polygon", "coordinates": [ diff --git a/ohsome/test/test_client.py b/ohsome/test/test_client.py index 7469dad..7bcd56d 100644 --- a/ohsome/test/test_client.py +++ b/ohsome/test/test_client.py @@ -144,9 +144,9 @@ def test_format_bcircles_list(base_client): @pytest.mark.vcr -def test_format_bcircles_geodataframe(base_client): +def test_format_bcircles_pandas(base_client): """ - Test whether a GeoDataFrame object given as 'bcircles' is formatted correctly. + Test whether a pandas.DataFrame object given as 'bcircles' is formatted correctly. :return: """ bcircles = gpd.read_file(f"{script_path}/data/points.geojson") @@ -175,8 +175,8 @@ def test_format_bcircles_geodataframe_geometry_error(base_client): bcircles=bcircles, time=time, filter=fltr ) assert ( - "The geometry of the 'bcircles' GeoDataFrame may only include 'Point' geometry types." - in e_info.value.message + "The geometry of the 'bcircles' GeoDataFrame may only include 'Point' geometry types and requires a 'radius' " + "column." in e_info.value.message ) del client @@ -251,17 +251,11 @@ def test_format_bboxes_geodataframe(base_client): client = base_client with pytest.raises(ohsome.OhsomeException) as e_info: client.elements.count.post(bboxes=data, time=time, filter=fltr) - if "occurred at index 0" in e_info.value.message: - # Python 3.6 does some weird stuff with the output. So it differs a bit. - assert ( - "Column ('minx', 'occurred at index 0') is missing in the dataframe provided as 'bboxes'." - in e_info.value.message - ) - else: - assert ( - "Use the 'bpolys' parameter to specify the boundaries using a " - "geopandas.GeoDataFrame." in e_info.value.message - ) + + assert ( + "Use the 'bpolys' parameter to specify the boundaries using a " + "geopandas.GeoDataFrame." in e_info.value.message + ) @pytest.mark.vcr diff --git a/ohsome/test/test_exceptions.py b/ohsome/test/test_exceptions.py index 8787051..995f112 100644 --- a/ohsome/test/test_exceptions.py +++ b/ohsome/test/test_exceptions.py @@ -118,7 +118,12 @@ def test_log_bpolys(base_client_without_log, tmpdir): base_client_without_log.elements.count.post( bpolys=bpolys, time=time, filter=fltr, timeout=timeout ) - log_file_patterns = ["ohsome_*_bpolys.geojson", "ohsome_*.json", "ohsome_*raw.txt"] + log_file_patterns = [ + "ohsome_*_bpolys.geojson", + "ohsome_*_curl.sh", + "ohsome_*.json", + "ohsome_*raw.txt", + ] for p in log_file_patterns: log_file = list(Path(base_client_without_log.log_dir).glob(p)) assert len(log_file) == 1, f"Log file {p} not found" @@ -126,6 +131,33 @@ def test_log_bpolys(base_client_without_log, tmpdir): log_file[0].unlink() +@pytest.mark.vcr +def test_log_curl(base_client_without_log, tmpdir): + """ + Test whether log file containing curl command is created when request fails + :return: + """ + + base_client_without_log.log = True + base_client_without_log.log_dir = tmpdir.mkdir("logs").strpath + + bboxes = [8.67555, 49.39885, 8.69637, 49.41122] + timeout = 0.001 + + with pytest.raises(ohsome.OhsomeException): + base_client_without_log.elements.count.post(bboxes=bboxes, timeout=timeout) + + log_file = list(Path(base_client_without_log.log_dir).glob("ohsome_*_curl.sh")) + with open(log_file[0]) as file: + assert file.read() == ( + 'curl -X POST -H "user-agent: ohsome-py/0.2.0" -H "Accept-Encoding: gzip, ' + 'deflate" -H "Accept: */*" -H "Connection: keep-alive" -H "Content-Length: 60" ' + '-H "Content-Type: application/x-www-form-urlencoded" ' + "-d 'bboxes=8.67555%2C49.39885%2C8.69637%2C49.41122&timeout=0.001' " + "https://api.ohsome.org/v1/elements/count" + ) + + def test_metadata_invalid_baseurl(custom_client_with_wrong_url): """ Throw exception if the ohsome API is not available diff --git a/ohsome/test/test_helper.py b/ohsome/test/test_helper.py index ca22f8b..bcbd531 100644 --- a/ohsome/test/test_helper.py +++ b/ohsome/test/test_helper.py @@ -3,19 +3,23 @@ """Tests for utility functions""" import datetime +import json import logging import os import numpy as np import pandas as pd import pytest +from shapely import Polygon +from ohsome import OhsomeException from ohsome.helper import ( find_groupby_names, extract_error_message_from_invalid_json, format_time, convert_arrays, format_list_parameters, + format_bpolys, ) script_path = os.path.dirname(os.path.realpath(__file__)) @@ -210,3 +214,24 @@ def test_format_list_parameters(): output = format_list_parameters(method_input) assert output == expected_output + + +def test_format_bpolys(): + """Test if the formatting of bounding polys works correctly.""" + with pytest.raises(OhsomeException) as e: + format_bpolys({}) + assert ( + e.value.message + == "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) + + 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]]]}}]}" + ) diff --git a/poetry.lock b/poetry.lock index 7362e71..da16bd2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -514,6 +514,17 @@ traitlets = ">=4" [package.extras] test = ["pytest"] +[[package]] +name = "curlify2" +version = "2.0.0" +description = "Library to convert python requests and httpx object to curl command." +optional = false +python-versions = ">=3.7,<4.0" +files = [ + {file = "curlify2-2.0.0-py3-none-any.whl", hash = "sha256:23878b13fa33ed92ae5dff0476e5b881b39fc79a5fcb37e6726f96239a136474"}, + {file = "curlify2-2.0.0.tar.gz", hash = "sha256:6c3a98dc8603b76da990f58754b18ec4e9bfe5b881fc52d33ddf9d9096568809"}, +] + [[package]] name = "debugpy" version = "1.8.0" @@ -3005,4 +3016,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "a88b70fa86e8b500ab8e2ab90a07bbf00091c7dbfb75eaa1410c96ef8574aa71" +content-hash = "1f9dcaf124ff252719359a924e1dc99c248a0839434b194fdaabda7c5662e65b" diff --git a/pyproject.toml b/pyproject.toml index b124e47..d3e96d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ pandas = "^2.1.3" numpy = "^1.20.0" geopandas = "^0.14.1" urllib3 = "^2.1.0" +curlify2 = "^2.0.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.3"