Coverage for qubalab/objects/geojson.py: 91%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-31 11:24 +0000

1import geojson 

2from typing import Iterable, Union 

3 

4 

5def geojson_features_from_string(json_string: str, **kwargs) -> Union[Iterable[geojson.Feature], geojson.Feature]: 

6 """ 

7 Read features from a GeoJSON string. 

8 

9 If the string encodes a feature collection, the features themselves will be extracted. 

10 

11 NaNs values are allowed. However, converting a returned feature with a NaN value to a string 

12 will throw an exception. 

13 

14 :param json_string: a string representing a GeoJSON 

15 :param kwargs: additional parameters to pass to the geojson loader 

16 :returns: a single or a collection of features representing the provided string 

17 """ 

18 results = _geojson_from_string(json_string, **kwargs) 

19 if 'features' in results: 

20 results = results['features'] 

21 return results 

22 

23 

24def _geojson_from_string(json: str, **kwargs): 

25 """ 

26 Read a GeoJSON string. 

27 This is a wrapper around geojson.loads that allows for NaNs by default (and is generally non-strict with numbers). 

28 """ 

29 # Default parse constant is _enforce_strict_numbers, which fails on NaNs 

30 if 'parse_constant' in kwargs: 

31 return geojson.loads(json, **kwargs) 

32 else: 

33 return geojson.loads(json, parse_constant=None, **kwargs)