Skip to content

Commit

Permalink
feat: change geocoding logic (#136)
Browse files Browse the repository at this point in the history
* feat: changed geocoding logic

* chore: add geocoding tests

* fix: change pandas dataframe doctests after update to version 2+

* fix: change crs setting because of geopandas bug
  • Loading branch information
RaczeQ authored Jul 25, 2024
1 parent 41b24d0 commit 7b675a6
Show file tree
Hide file tree
Showing 16 changed files with 943 additions and 798 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
- id: mypy
additional_dependencies: ['types-requests', 'types-six']
- repo: https://github.com/pdm-project/pdm
rev: 2.16.1
rev: 2.17.1
hooks:
- id: pdm-lock-check
- id: pdm-export
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Replaced `OSMnx` dependency with `GeoPy` for geometry geocoding [#135](https://github.com/kraina-ai/quackosm/issues/135)

## [0.8.2] - 2024-06-04

### Added
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Optional:

- `typer[all] (>=0.9.0)` (click, colorama, rich, shellingham): Required in CLI

- `osmnx (>=1.3.0)`: For geocoding of strings. Required in CLI
- `geopy (>=1.3.0)`: For geocoding of strings. Required in CLI

- `h3 (>=4.0.0b1)`: For reading H3 strings. Required in CLI

Expand Down Expand Up @@ -238,8 +238,7 @@ files/monaco_nofilter_noclip_compact.parquet

```python
>>> import quackosm as qosm
>>> import osmnx as ox
>>> geometry = ox.geocode_to_gdf("Vatican City").unary_union
>>> geometry = qosm.geocode_to_geometry("Vatican City")
>>> qosm.convert_geometry_to_geodataframe(geometry)
tags geometry
feature_id
Expand Down
10 changes: 5 additions & 5 deletions examples/pbf_file_reader.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"metadata": {},
"outputs": [],
"source": [
"from quackosm import PbfFileReader\n",
"import urllib.request\n",
"import osmnx as ox"
"\n",
"from quackosm import PbfFileReader, geocode_to_geometry"
]
},
{
Expand All @@ -53,8 +53,8 @@
"metadata": {},
"outputs": [],
"source": [
"reykjavik_gdf = ox.geocode_to_gdf(\"Reykjavík, IS\")\n",
"reykjavik_gdf"
"reykjavik_geometry = geocode_to_geometry(\"Reykjavík, IS\")\n",
"reykjavik_geometry"
]
},
{
Expand All @@ -75,7 +75,7 @@
"outputs": [],
"source": [
"reader = PbfFileReader(\n",
" geometry_filter=reykjavik_gdf.geometry.iloc[0], tags_filter={\"building\": True}\n",
" geometry_filter=reykjavik_geometry, tags_filter={\"building\": True}\n",
")\n",
"\n",
"reykjavik_buildings_gpq = reader.convert_pbf_to_parquet(\"iceland.osm.pbf\")\n",
Expand Down
1,380 changes: 698 additions & 682 deletions pdm.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ Changelog = "https://github.com/kraina-ai/quackosm/blob/main/CHANGELOG.md"
[project.optional-dependencies]
cli = [
"typer[all]>=0.9.0",
"osmnx>=1.3.0",
"h3>=4.0.0b1",
"s2>=0.1.9",
"python-geohash>=0.8",
"geopy>=1.3.0",
]

[project.scripts]
Expand Down Expand Up @@ -102,7 +102,6 @@ docs = [
"nbconvert",
"nbformat",
"notebook",
"osmnx",
"matplotlib",
"seaborn>=0.10.0",
"py-cpuinfo",
Expand Down
2 changes: 2 additions & 0 deletions quackosm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
convert_pbf_to_geodataframe,
convert_pbf_to_parquet,
)
from quackosm.geocode import geocode_to_geometry
from quackosm.pbf_file_reader import PbfFileReader

__app_name__ = "QuackOSM"
Expand All @@ -22,4 +23,5 @@
"convert_geometry_to_parquet",
"convert_pbf_to_geodataframe",
"convert_geometry_to_geodataframe",
"geocode_to_geometry",
]
3 changes: 3 additions & 0 deletions quackosm/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class InvalidGeometryFilter(Exception): ...


class MultiprocessingRuntimeError(RuntimeError): ...


class QueryNotGeocodedError(ValueError): ...
7 changes: 3 additions & 4 deletions quackosm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import click
import geopandas as gpd
import h3
import osmnx as ox
import typer
from click import Argument
from click.exceptions import MissingParameter
Expand All @@ -22,6 +21,7 @@
from quackosm._osm_tags_filters import GroupedOsmTagsFilter, OsmTagsFilter
from quackosm._typing import is_expected_type
from quackosm.functions import convert_geometry_to_parquet, convert_pbf_to_parquet
from quackosm.geocode import geocode_to_geometry
from quackosm.osm_extracts import OsmExtractSource
from quackosm.pbf_file_reader import _is_url_path

Expand Down Expand Up @@ -106,8 +106,7 @@ def convert(self, value, param=None, ctx=None): # type: ignore
return None

try:
gdf = ox.geocode_to_gdf(query=value, which_result=None)
return gdf.unary_union
return geocode_to_geometry(value)
except Exception:
raise typer.BadParameter("Cannot geocode provided Nominatim query") from None

Expand Down Expand Up @@ -313,7 +312,7 @@ def main(
help=(
"Geometry to use as a filter in the"
" [bold dark_orange]string to geocode[/bold dark_orange] format - it will be"
" geocoded to the geometry using Nominatim API (OSMnx library)."
" geocoded to the geometry using Nominatim API (GeoPy library)."
" Cannot be used together with"
" [bold bright_cyan]geom-filter-file[/bold bright_cyan] or"
" [bold bright_cyan]geom-filter-geojson[/bold bright_cyan] or"
Expand Down
8 changes: 8 additions & 0 deletions quackosm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from doctest import OutputChecker
from pathlib import Path

import pandas
import pytest

IGNORE_RESULT = doctest.register_optionflag("IGNORE_RESULT")
Expand Down Expand Up @@ -40,3 +41,10 @@ def add_pbf_files(doctest_namespace): # type: ignore
urllib.request.urlretrieve(pbf_file_download_url, pbf_file_path)
doctest_namespace[f"{extract_name}_pbf_path"] = pbf_file_path
shutil.copy(pbf_file_path, geofabrik_pbf_file_path)


@pytest.fixture(autouse=True, scope="session") # type: ignore
def pandas_terminal_width() -> None:
"""Change pandas dataframe display options."""
pandas.set_option("display.width", 90)
pandas.set_option("display.max_colwidth", 35)
Loading

0 comments on commit 7b675a6

Please sign in to comment.