Skip to content

Commit

Permalink
Adding and fixing address tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whubsch committed Aug 1, 2024
1 parent 1c888f7 commit 2c5f5e1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![PyPI - Version](https://img.shields.io/pypi/v/overturetoosm)
![Pepy Total Downlods](https://img.shields.io/pepy/dt/overturetoosm)

This Python project translates objects from the Overture maps schema to the OpenStreetMap (OSM) tagging scheme. The goal is to provide a seamless way to convert map data from Overture's format to a format that can be utilized within the OSM ecosystem. The package currently only supports Overture's `places` and `buildings` layers. You can improve the Overture categorization that this package uses by editing [the Overture categories page](https://wiki.openstreetmap.org/wiki/Overture_categories) on the OSM Wiki or submitting a pull request to the [tags.json](https://github.com/whubsch/overturetoosm/blob/main/scripts/tags.json) file.
This Python project translates objects from the Overture maps schema to the OpenStreetMap (OSM) tagging scheme. The goal is to provide a seamless way to convert map data from Overture's format to a format that can be utilized within the OSM ecosystem. The package currently only supports Overture's `places`, `buildings`, and `addresses` layers. You can improve the Overture categorization that this package uses by editing [the Overture categories page](https://wiki.openstreetmap.org/wiki/Overture_categories) on the OSM Wiki or submitting a pull request to the [tags.json](https://github.com/whubsch/overturetoosm/blob/main/scripts/tags.json) file.

> [!NOTE]
> Use of this package does not absolve you from following OSM's [import guidelines](https://wiki.openstreetmap.org/wiki/Import/Guidelines).
Expand Down
2 changes: 1 addition & 1 deletion src/overturetoosm/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Top-level package for overturetoosm."""

__version__ = "0.2.0"
__version__ = "0.2.1"
6 changes: 4 additions & 2 deletions src/overturetoosm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""`overturetoosm` is a Python package to convert objects tagged in the
Overture schema for use in OSM. Only Overture's `places` and `buildings`
layers are currently supported.
Overture schema for use in OSM. Only Overture's `places`, `buildings`,
and `addresses` layers are currently supported.
Links:
* [Project GitHub](https://github.com/whubsch/overturetoosm)
Expand All @@ -10,6 +10,7 @@

from .places import process_place
from .buildings import process_building
from .addresses import process_address
from .utils import process_geojson
from . import places
from . import buildings
Expand All @@ -20,6 +21,7 @@
__all__ = [
"process_place",
"process_building",
"process_address",
"process_geojson",
"places",
"buildings",
Expand Down
16 changes: 10 additions & 6 deletions src/overturetoosm/addresses.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
from typing import Dict, Literal
"""Convert Overture's `addresses` features to OSM tags."""

from typing import Dict
from .objects import AddressProps


def process_address(
props: dict,
style: Literal["US"] = "US",
style: str = "US",
) -> Dict[str, str]:
"""Convert Overture's address properties to OSM tags.
Args:
props (dict): The feature properties from the Overture GeoJSON.
style (str, optional): How to handle the `address_levels` field. Open a pull request or issue to add support for other regions. Defaults to "US".
style (str, optional): How to handle the `address_levels` field. Open
a pull request or issue to add support for other regions. Defaults to "US".
Returns:
Dict[str, str]: The reshaped and converted properties in OSM's flat str:str schema.
"""
bad_tags = ["version", "theme", "type", "address_levels"]
print(props)
prop = AddressProps(**props)

obj_dict = prop.model_dump(exclude_none=True, by_alias=True)
if prop.address_levels:

if obj_dict["address_levels"] and len(obj_dict["address_levels"]) > 0:
if style == "US":
obj_dict["addr:state"] = prop.address_levels[0]["value"]
obj_dict["addr:state"] = str(obj_dict["address_levels"][0]["value"])

for tag in bad_tags:
obj_dict.pop(tag, None)
Expand Down
2 changes: 1 addition & 1 deletion src/overturetoosm/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ class AddressProps(OvertureBase):
street: Optional[str] = Field(serialization_alias="addr:street")
postcode: Optional[str] = Field(serialization_alias="addr:postcode")
country: Optional[str] = Field(serialization_alias="addr:country")
address_levels: Optional[List[AddressLevel]] = None
address_levels: Optional[List[AddressLevel]] = Field(default_factory=list)
48 changes: 48 additions & 0 deletions tests/test_addresses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import Dict, Any
import pytest
from src.overturetoosm.addresses import process_address


@pytest.fixture(name="clean_dict")
def clean_fix() -> Dict[str, Any]:
"""Fixture with the clean address properties"""
return {
"addr:country": "US",
"addr:postcode": "02459",
"addr:street": "COMMONWEALTH AVE",
"addr:housenumber": "1000",
"addr:state": "MA",
}


@pytest.fixture(name="props_dict")
def props_fix() -> Dict[str, Any]:
"""Fixture with the raw address properties"""
return {
"theme": "addresses",
"type": "address",
"version": 0,
"country": "US",
"address_levels": [{"value": "MA"}, {"value": "NEWTON CENTRE"}],
"postcode": "02459",
"street": "COMMONWEALTH AVE",
"number": "1000",
}


def test_process_address(props_dict, clean_dict) -> None:
"""Test that address properties are processed correctly"""
assert process_address(props_dict) == clean_dict


def test_process_address_no_levels(props_dict, clean_dict) -> None:
"""Test that address properties are processed correctly"""
props_dict.pop("address_levels", None)
clean_dict.pop("addr:state", None)
assert process_address(props_dict) == clean_dict


def test_process_address_not_us(props_dict, clean_dict) -> None:
"""Test that address properties are processed correctly"""
clean_dict.pop("addr:state", None)
assert process_address(props_dict, style="CA") == clean_dict

0 comments on commit 2c5f5e1

Please sign in to comment.