Coverage for src/overturetoosm/utils.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-28 10:12 -0400

1"""Useful functions for the project.""" 

2 

3from typing import Callable, List, Literal, 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 

37 Returns: 

38 dict: The dictionary representation of the GeoJSON that follows OSM's schema. 

39 """ 

40 options = options or {} 

41 new_features = [] 

42 for feature in geojson["features"]: 

43 try: 

44 feature["properties"] = fx(feature["properties"], confidence, **options) 

45 new_features.append(feature) 

46 except (ConfidenceError, UnmatchedError): 

47 pass 

48 

49 geojson["features"] = new_features 

50 return geojson