From 0fe31ac2c88ff46d7e7fc8e4e8f06e979dfd60c0 Mon Sep 17 00:00:00 2001 From: Max Henrik Balsmeier Date: Wed, 28 Jul 2021 01:01:05 +0200 Subject: [PATCH] New GSHHS coastlines implemented including error handling and doc update. --- lib/cartopy/feature/__init__.py | 2 +- lib/cartopy/io/shapereader.py | 42 +++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/cartopy/feature/__init__.py b/lib/cartopy/feature/__init__.py index 2e5ac59d0..3e98e08b0 100644 --- a/lib/cartopy/feature/__init__.py +++ b/lib/cartopy/feature/__init__.py @@ -324,7 +324,7 @@ class GSHHSFeature(Feature): The dataset scale. One of 'auto', 'coarse', 'low', 'intermediate', 'high, or 'full' (default is 'auto'). levels - A list of integers 1-4 corresponding to the desired GSHHS feature + A list of integers 1-6 corresponding to the desired GSHHS feature levels to draw (default is [1] which corresponds to coastlines). Other Parameters diff --git a/lib/cartopy/io/shapereader.py b/lib/cartopy/io/shapereader.py index 795b202c3..f297aded2 100644 --- a/lib/cartopy/io/shapereader.py +++ b/lib/cartopy/io/shapereader.py @@ -31,6 +31,7 @@ import io import itertools import os +from urllib.error import HTTPError import shapely.geometry as sgeom import shapefile @@ -408,9 +409,12 @@ class GSHHSShpDownloader(Downloader): """ FORMAT_KEYS = ('config', 'scale', 'level') - _GSHHS_URL_TEMPLATE = ('https://www.ngdc.noaa.gov/mgg/shorelines/data/' - 'gshhs/oldversions/version2.2.0/' - 'GSHHS_shp_2.2.0.zip') + gshhs_version = '2.3.7' + + _GSHHS_URL_TEMPLATE = ( + 'https://www.ngdc.noaa.gov/mgg/shorelines/data/' + 'gshhs/latest/gshhg-shp-' + gshhs_version + '.zip' + ) def __init__(self, url_template=_GSHHS_URL_TEMPLATE, @@ -435,15 +439,43 @@ def acquire_all_resources(self, format_dict): # Download archive. url = self.url(format_dict) - shapefile_online = self._urlopen(url) + try: + shapefile_online = self._urlopen(url) + # error handling: + except HTTPError: + try: + """ + case if GSHHS has had an update + without changing the naming convention + """ + url = ( + 'https://www.ngdc.noaa.gov/mgg/shorelines/data/' + 'gshhs/oldversions/version' + self.gshhs_version + '/' + 'gshhg-shp-' + self.gshhs_version + '.zip' + ) + shapefile_online = self._urlopen(url) + except HTTPError: + """ + case if GSHHS has had an update + with changing the naming convention + """ + url = ( + 'https://www.ngdc.noaa.gov/mgg/shorelines/data/' + 'gshhs/oldversions/version2.3.6/' + 'gshhg-shp-2.3.6.zip' + ) + shapefile_online = self._urlopen(url) zfh = ZipFile(io.BytesIO(shapefile_online.read()), 'r') shapefile_online.close() # Iterate through all scales and levels and extract relevant files. modified_format_dict = dict(format_dict) scales = ('c', 'l', 'i', 'h', 'f') - levels = (1, 2, 3, 4) + levels = (1, 2, 3, 4, 5, 6) for scale, level in itertools.product(scales, levels): + # the combination c4 does not occur for some reason + if scale == "c" and level == 4: + continue modified_format_dict.update({'scale': scale, 'level': level}) target_path = self.target_path(modified_format_dict) target_dir = os.path.dirname(target_path)