Coverage for aisdb/webdata/shore_dist.py: 89%

27 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-30 04:22 +0000

1''' Collect shore/port distances at given coordinates using NASA and Global 

2 Fishing Watch raster files. 

3 A (free) login is required to download Global Fishing Watch rasters, 

4 so these files must be manually downloaded and extracted into ``data_dir``. 

5 

6 Raster data can be downloaded from: 

7 

8 .. code-block:: 

9 

10 https://oceancolor.gsfc.nasa.gov/docs/distfromcoast/GMT_intermediate_coast_distance_01d.zip 

11 https://globalfishingwatch.org/data-download/datasets/public-distance-from-shore-v1 

12 https://globalfishingwatch.org/data-download/datasets/public-distance-from-port-v1 

13 

14 once downloaded, place the unzipped geotiff files in `data_dir` 

15''' 

16 

17import os 

18import zipfile 

19 

20import requests 

21from tqdm import tqdm 

22 

23from aisdb.webdata.load_raster import RasterFile 

24 

25def download_unzip(data_url, data_dir, bytesize=0): 

26 assert os.path.isdir(data_dir), f'not a directory: {data_dir=}' 

27 zipf = os.path.join(data_dir, data_url.rsplit('/',1)[1]) 

28 if not os.path.isfile(zipf): 28 ↛ 29line 28 didn't jump to line 29, because the condition on line 28 was never true

29 try: 

30 with requests.get(data_url, stream=True) as payload: 

31 assert payload.status_code == 200, 'error fetching file' 

32 with open(zipf, 'wb') as f: 

33 with tqdm(total=bytesize, 

34 desc=zipf, 

35 unit='B', 

36 unit_scale=True) as t: 

37 for chunk in payload.iter_content(chunk_size=8192): 

38 _ = t.update(f.write(chunk)) 

39 except Exception as err: 

40 os.remove(zipf) 

41 raise err 

42 with zipfile.ZipFile(zipf, 'r') as zip_ref: 

43 members = list( fpath for fpath in 

44 set(zip_ref.namelist()) - 

45 set(sorted(os.listdir(data_dir))) if '.tif' in fpath) 

46 if len(members) > 0: 46 ↛ 47line 46 didn't jump to line 47, because the condition on line 46 was never true

47 zip_ref.extractall(path=data_dir, members=members) 

48 

49class ShoreDist(RasterFile): 

50 

51 data_url = "https://oceancolor.gsfc.nasa.gov/docs/distfromcoast/GMT_intermediate_coast_distance_01d.zip" 

52 

53 def __init__(self, data_dir, tif_filename='GMT_intermediate_coast_distance_01d.tif'): 

54 download_unzip(self.data_url, data_dir, bytesize=657280) 

55 imgpath = os.path.join(data_dir, tif_filename) 

56 #imgpath = os.path.join(data_dir, 'distance-from-shore.tif') 

57 assert os.path.isfile(imgpath) 

58 super().__init__(imgpath) 

59 

60 def get_distance(self, tracks): 

61 assert hasattr(self, 'imgpath') 

62 return self.merge_tracks(tracks, new_track_key='km_from_shore') 

63 

64 

65class PortDist(RasterFile): 

66 

67 def get_distance(self, tracks): 

68 return self.merge_tracks(tracks, new_track_key='km_from_port')