Skip to content

Commit

Permalink
chore: replace unary_union calls and silence warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
RaczeQ committed Sep 11, 2024
1 parent 4460d89 commit 0c54d8d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions quackosm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
6 changes: 5 additions & 1 deletion quackosm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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."""
Expand Down
7 changes: 5 additions & 2 deletions quackosm/osm_extracts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__ = [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions quackosm/pbf_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/base/test_geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/benchmark/test_big_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down

0 comments on commit 0c54d8d

Please sign in to comment.