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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-07-22 06:43 -0400

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

2 

3from collections.abc import Callable 

4 

5from .objects import ConfidenceError, UnmatchedError 

6 

7 

8def process_geojson( 

9 geojson: dict, 

10 fx: Callable, 

11 confidence: float | None = None, 

12 options: dict | None = 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: 42 ↛ 43line 42 didn't jump to line 43 because the condition on line 42 was never true

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