Coverage for attribuutit/tif.py: 90.91%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 15:52:25 +00:00

1import pathlib 

2from typing import no_type_check 

3 

4from geotiff import GeoTiff # type: ignore 

5 

6 

7@no_type_check 

8def load(path: pathlib.Path): 

9 """Load the GeoTIFF file at path.""" 

10 error = '' 

11 try: 

12 # Consider contributing upstream as neither pathlib nor context manager support there 

13 geo_tiff = GeoTiff(str(path)) 

14 crs_code = geo_tiff.crs_code.value 

15 crs_name = geo_tiff.crs_code.name 

16 bounding_box = geo_tiff.tif_bBox 

17 except BaseException as err: 

18 error = f'problem reading GeoTIFF from {path} with {err}' 

19 

20 return error, { 

21 'folder_path': str(path.parent), 

22 'file_name': path.name, 

23 'file_suffixes': path.suffixes, 

24 'crs_name': crs_name, 

25 'crs_code': crs_code, 

26 'bounding_box': [coord for point in bounding_box for coord in point], 

27 } 

28 

29 

30@no_type_check 

31def summary(tif_model, full_path=False): 

32 """Summarize the TIFF file model as single line string.""" 

33 path_disp = pathlib.Path(tif_model['folder_path']) / tif_model['file_name'] if full_path else tif_model['file_name'] 

34 return ( 

35 f'{path_disp} ->' 

36 f" CRS {tif_model['crs_name']} ({tif_model['crs_code']})" 

37 f" within {tif_model['bounding_box']}" 

38 )