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