overturetoosm.utils
Useful functions for the project.
1"""Useful functions for the project.""" 2 3from typing import Callable, List, Optional 4from .objects import ConfidenceError, Sources, UnmatchedError 5 6 7def source_statement(source: List[Sources]) -> str: 8 """Return a source statement from a list of sources.""" 9 return ", ".join(i.dataset.strip(", ") for i in source) + " via overturetoosm" 10 11 12def process_geojson( 13 geojson: dict, 14 fx: Callable, 15 confidence: float = 0.0, 16 options: Optional[dict] = None, 17) -> dict: 18 """Convert an Overture `place` GeoJSON to one that follows OSM's schema. 19 20 Example usage: 21 ```python 22 import json 23 from overturetoosm.places import process_geojson 24 25 with open("overture.geojson", "r", encoding="utf-8") as f: 26 contents: dict = json.load(f) 27 geojson = process_geojson(contents, fx=process_building) 28 29 with open("overture_out.geojson", "w+", encoding="utf-8") as x: 30 json.dump(geojson, x, indent=4) 31 ``` 32 Args: 33 geojson (dict): The dictionary representation of the Overture GeoJSON. 34 fx (Callable): The function to apply to each feature. 35 confidence (float, optional): The minimum confidence level. Defaults to 0.0. 36 options (dict, optional): Function-specific options to pass as arguments to 37 the `fx` function. 38 39 Returns: 40 dict: The dictionary representation of the GeoJSON that follows OSM's schema. 41 """ 42 options = options or {} 43 new_features = [] 44 for feature in geojson["features"]: 45 try: 46 feature["properties"] = fx(feature["properties"], confidence, **options) 47 new_features.append(feature) 48 except (ConfidenceError, UnmatchedError): 49 pass 50 51 geojson["features"] = new_features 52 return geojson
8def source_statement(source: List[Sources]) -> str: 9 """Return a source statement from a list of sources.""" 10 return ", ".join(i.dataset.strip(", ") for i in source) + " via overturetoosm"
Return a source statement from a list of sources.
def
process_geojson( geojson: dict, fx: Callable, confidence: float = 0.0, options: Optional[dict] = None) -> dict:
13def process_geojson( 14 geojson: dict, 15 fx: Callable, 16 confidence: float = 0.0, 17 options: Optional[dict] = None, 18) -> dict: 19 """Convert an Overture `place` GeoJSON to one that follows OSM's schema. 20 21 Example usage: 22 ```python 23 import json 24 from overturetoosm.places import process_geojson 25 26 with open("overture.geojson", "r", encoding="utf-8") as f: 27 contents: dict = json.load(f) 28 geojson = process_geojson(contents, fx=process_building) 29 30 with open("overture_out.geojson", "w+", encoding="utf-8") as x: 31 json.dump(geojson, x, indent=4) 32 ``` 33 Args: 34 geojson (dict): The dictionary representation of the Overture GeoJSON. 35 fx (Callable): The function to apply to each feature. 36 confidence (float, optional): The minimum confidence level. Defaults to 0.0. 37 options (dict, optional): Function-specific options to pass as arguments to 38 the `fx` function. 39 40 Returns: 41 dict: The dictionary representation of the GeoJSON that follows OSM's schema. 42 """ 43 options = options or {} 44 new_features = [] 45 for feature in geojson["features"]: 46 try: 47 feature["properties"] = fx(feature["properties"], confidence, **options) 48 new_features.append(feature) 49 except (ConfidenceError, UnmatchedError): 50 pass 51 52 geojson["features"] = new_features 53 return geojson
Convert an Overture place
GeoJSON to one that follows OSM's schema.
Example usage:
import json
from overturetoosm.places import process_geojson
with open("overture.geojson", "r", encoding="utf-8") as f:
contents: dict = json.load(f)
geojson = process_geojson(contents, fx=process_building)
with open("overture_out.geojson", "w+", encoding="utf-8") as x:
json.dump(geojson, x, indent=4)
Arguments:
- geojson (dict): The dictionary representation of the Overture GeoJSON.
- fx (Callable): The function to apply to each feature.
- confidence (float, optional): The minimum confidence level. Defaults to 0.0.
- options (dict, optional): Function-specific options to pass as arguments to
the
fx
function.
Returns:
dict: The dictionary representation of the GeoJSON that follows OSM's schema.