From 0c54d8d669bbe554e04971dbc524cbe6ab3ec379 Mon Sep 17 00:00:00 2001 From: Kamil Raczycki Date: Wed, 11 Sep 2024 10:34:57 +0200 Subject: [PATCH] chore: replace unary_union calls and silence warnings --- CHANGELOG.md | 2 ++ quackosm/cli.py | 8 ++++---- quackosm/conftest.py | 6 +++++- quackosm/osm_extracts/__init__.py | 7 +++++-- quackosm/pbf_file_reader.py | 4 ++++ tests/base/test_geocoding.py | 2 +- tests/benchmark/test_big_file.py | 3 +++ 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdd5d4d..e7421c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bumped minimal DuckDB version to `1.1.0` - Excluded `conftest.py` file from the final library build +- Replaced `unary_union` calls with `union_all()` on all GeoDataFrames +- Silenced `pooch` library warnings regarding empty SHA hash ## [0.9.3] - 2024-09-10 diff --git a/quackosm/cli.py b/quackosm/cli.py index 59fe303..e9d09ea 100644 --- a/quackosm/cli.py +++ b/quackosm/cli.py @@ -96,7 +96,7 @@ def convert(self, value, param=None, ctx=None): # type: ignore import geopandas as gpd gdf = gpd.read_file(value) - return gdf.unary_union + return gdf.union_all() except Exception: raise typer.BadParameter("Cannot parse provided geo file") from None @@ -140,7 +140,7 @@ def convert(self, value, param=None, ctx=None): # type: ignore geometries.append( box(minx=bounds["w"], miny=bounds["s"], maxx=bounds["e"], maxy=bounds["n"]) ) - return gpd.GeoSeries(geometries).unary_union + return gpd.GeoSeries(geometries).union_all() except Exception: raise typer.BadParameter(f"Cannot parse provided Geohash value: {geohash}") from None @@ -165,7 +165,7 @@ def convert(self, value, param=None, ctx=None): # type: ignore geometries.append( Polygon([coords[::-1] for coords in h3.cell_to_boundary(h3_cell.strip())]) ) - return gpd.GeoSeries(geometries).unary_union + return gpd.GeoSeries(geometries).union_all() except Exception as ex: raise typer.BadParameter(f"Cannot parse provided H3 values: {value}") from ex @@ -190,7 +190,7 @@ def convert(self, value, param=None, ctx=None): # type: ignore geometries.append( Polygon(s2.s2_to_geo_boundary(s2_index.strip(), geo_json_conformant=True)) ) - return gpd.GeoSeries(geometries).unary_union + return gpd.GeoSeries(geometries).union_all() except Exception: raise typer.BadParameter(f"Cannot parse provided S2 value: {s2_index}") from None diff --git a/quackosm/conftest.py b/quackosm/conftest.py index e5eb5b7..8402511 100644 --- a/quackosm/conftest.py +++ b/quackosm/conftest.py @@ -9,6 +9,7 @@ import duckdb import pandas import pytest +from pooch import get_logger as get_pooch_logger from pooch import retrieve from quackosm.osm_extracts.extract import OsmExtractSource @@ -53,13 +54,15 @@ def add_pbf_files(doctest_namespace): # type: ignore shutil.copy(pbf_file_path, geofabrik_pbf_file_path) - @pytest.fixture(autouse=True, scope="session") def download_osm_extracts_indexes(): # type: ignore """Download OSM extract indexes files to cache.""" download_directory = Path("cache") download_directory.mkdir(parents=True, exist_ok=True) + logger = get_pooch_logger() + logger.setLevel("WARNING") + for osm_extract in OsmExtractSource: if osm_extract == OsmExtractSource.any: continue @@ -81,6 +84,7 @@ def install_spatial_extension(): # type: ignore """Install duckdb spatial extension.""" duckdb.install_extension("spatial") + @pytest.fixture(autouse=True, scope="session") # type: ignore def pandas_terminal_width() -> None: """Change pandas dataframe display options.""" diff --git a/quackosm/osm_extracts/__init__.py b/quackosm/osm_extracts/__init__.py index 097e40f..3c082da 100644 --- a/quackosm/osm_extracts/__init__.py +++ b/quackosm/osm_extracts/__init__.py @@ -17,6 +17,7 @@ import geopandas as gpd from pandas.util._decorators import deprecate +from pooch import get_logger as get_pooch_logger from pooch import retrieve from rich import get_console from rich import print as rprint @@ -35,7 +36,7 @@ from quackosm.osm_extracts.geofabrik import _get_geofabrik_index from quackosm.osm_extracts.osm_fr import _get_openstreetmap_fr_index -if TYPE_CHECKING: # pragma: no cover +if TYPE_CHECKING: # pragma: no cover import pandas as pd __all__ = [ @@ -65,6 +66,8 @@ def download_extracts_pbf_files( list[Path]: List of downloaded file paths. """ downloaded_extracts_paths = [] + logger = get_pooch_logger() + logger.setLevel("WARNING") for extract in extracts: file_path = retrieve( extract.url, @@ -816,7 +819,7 @@ def _simplify_selected_extracts( warnings.simplefilter("ignore", category=FutureWarning) other_geometries = matching_extracts.loc[ sorted_extracts_gdf["id"] != extract_id - ].unary_union + ].union_all() if extract_geometry.covered_by(other_geometries): extract_to_remove = extract_id simplify_again = True diff --git a/quackosm/pbf_file_reader.py b/quackosm/pbf_file_reader.py index 03045f7..2ed0a60 100644 --- a/quackosm/pbf_file_reader.py +++ b/quackosm/pbf_file_reader.py @@ -32,6 +32,7 @@ import shapely.wkt as wktlib from geoarrow.pyarrow import io from pandas.util._decorators import deprecate, deprecate_kwarg +from pooch import get_logger as get_pooch_logger from pooch import retrieve from pooch.utils import parse_url from shapely.geometry import LinearRing, Polygon @@ -792,6 +793,9 @@ def _parse_pbf_file( save_as_wkt: bool = False, ) -> Path: if _is_url_path(pbf_path): + logger = get_pooch_logger() + logger.setLevel("WARNING") + pbf_path = retrieve( pbf_path, fname=Path(pbf_path).name, diff --git a/tests/base/test_geocoding.py b/tests/base/test_geocoding.py index ead7dc5..76ab1b7 100644 --- a/tests/base/test_geocoding.py +++ b/tests/base/test_geocoding.py @@ -21,7 +21,7 @@ ) def test_geocoding(query: Union[str, list[str]]) -> None: """Test if geocoding works the same as osmnx.""" - assert geocode_to_gdf(query).unary_union.equals(geocode_to_geometry(query)) + assert geocode_to_gdf(query).union_all().equals(geocode_to_geometry(query)) @pytest.mark.parametrize( # type: ignore diff --git a/tests/benchmark/test_big_file.py b/tests/benchmark/test_big_file.py index a77b5ab..56505d4 100644 --- a/tests/benchmark/test_big_file.py +++ b/tests/benchmark/test_big_file.py @@ -4,6 +4,7 @@ from pathlib import Path from parametrization import Parametrization as P +from pooch import get_logger as get_pooch_logger from pooch import retrieve from quackosm import PbfFileReader, geocode_to_geometry @@ -22,6 +23,8 @@ def test_big_file(extract_name: str, geocode_filter: list[str], tags_filter: Osm files_dir = Path("files") shutil.rmtree(files_dir) file_name = files_dir / f"{extract_name}.osm.pbf" + logger = get_pooch_logger() + logger.setLevel("WARNING") retrieve( f"https://download.geofabrik.de/europe/{extract_name}-latest.osm.pbf", fname=f"{extract_name}.osm.pbf",