def setup_cartopy(): # Configures cartopy to download NaturalEarth shapefiles from S3 instead of naciscdn # Taken from https://github.com/SciTools/cartopy/issues/1325#issuecomment-904343657 target_path_template = NEShpDownloader.default_downloader().target_path_template downloader = NEShpDownloader(url_template=CARTOPY_SOURCE_TEMPLATE, target_path_template=target_path_template) cartopy.config['downloaders'][('shapefiles', 'natural_earth')] = downloader
def test_natural_earth_downloader(): # downloads a file to a temporary location, and uses that temporary # location, then: # * Checks that the file is only downloaded once even when calling # multiple times # * Checks that shapefiles have all the necessary files when downloaded # * Checks that providing a path in a download item gets used rather # than triggering another download tmp_dir = tempfile.mkdtemp() shp_path_template = os.path.join(tmp_dir, '{category}_{resolution}_{name}.shp') # picking a small-ish file to speed up download times, the file itself # isn't important - it is the download mechanism that is. format_dict = {'category': 'physical', 'name': 'rivers_lake_centerlines', 'resolution': '110m'} try: dnld_item = NEShpDownloader(target_path_template=shp_path_template) # check that the file gets downloaded the first time path is called with CallCounter(dnld_item, 'acquire_resource') as counter: shp_path = dnld_item.path(format_dict) assert counter.count == 1, 'Item not downloaded.' assert_equal(shp_path_template.format(**format_dict), shp_path) # check that calling path again doesn't try re-downloading with CallCounter(dnld_item, 'acquire_resource') as counter: assert_equal(dnld_item.path(format_dict), shp_path) assert counter.count == 0, 'Item was re-downloaded.' # check that we have the shp and the shx exts = ['.shp', '.shx'] for ext in exts: stem = os.path.splitext(shp_path)[0] msg = "Shapefile's {0} file doesn't exist in {1}{0}".format(ext, stem) assert os.path.exists(stem + ext), msg # check that providing a pre downloaded path actually works pre_dnld = NEShpDownloader(target_path_template='/not/a/real/file.txt', pre_downloaded_path_template=shp_path ) # check that the pre_dnld downloader doesn't re-download, but instead # uses the path of the previously downloaded item with CallCounter(pre_dnld, 'acquire_resource') as counter: assert_equal(pre_dnld.path(format_dict), shp_path) assert counter.count == 0, 'Aquire resource called more than once.' finally: shutil.rmtree(tmp_dir)
def test_natural_earth_downloader(): # downloads a file to a temporary location, and uses that temporary # location, then: # * Checks that the file is only downloaded once even when calling # multiple times # * Checks that shapefiles have all the necessary files when downloaded # * Checks that providing a path in a download item gets used rather # than triggering another download tmp_dir = tempfile.mkdtemp() shp_path_template = os.path.join(tmp_dir, '{category}_{resolution}_{name}.shp') # picking a small-ish file to speed up download times, the file itself # isn't important - it is the download mechanism that is. format_dict = { 'category': 'physical', 'name': 'rivers_lake_centerlines', 'resolution': '110m' } try: dnld_item = NEShpDownloader(target_path_template=shp_path_template) # check that the file gets downloaded the first time path is called with CallCounter(dnld_item, 'acquire_resource') as counter: shp_path = dnld_item.path(format_dict) assert counter.count == 1, 'Item not downloaded.' assert_equal(shp_path_template.format(**format_dict), shp_path) # check that calling path again doesn't try re-downloading with CallCounter(dnld_item, 'acquire_resource') as counter: assert_equal(dnld_item.path(format_dict), shp_path) assert counter.count == 0, 'Item was re-downloaded.' # check that we have the shp and the shx exts = ['.shp', '.shx'] for ext in exts: stem = os.path.splitext(shp_path)[0] msg = "Shapefile's {0} file doesn't exist in {1}{0}".format( ext, stem) assert os.path.exists(stem + ext), msg # check that providing a pre downloaded path actually works pre_dnld = NEShpDownloader(target_path_template='/not/a/real/file.txt', pre_downloaded_path_template=shp_path) # check that the pre_dnld downloader doesn't re-download, but instead # uses the path of the previously downloaded item with CallCounter(pre_dnld, 'acquire_resource') as counter: assert_equal(pre_dnld.path(format_dict), shp_path) assert counter.count == 0, 'Aquire resource called more than once.' finally: shutil.rmtree(tmp_dir)
def test_natural_earth_downloader(tmp_path): # downloads a file to a temporary location, and uses that temporary # location, then: # * Checks that the file is only downloaded once even when calling # multiple times # * Checks that shapefiles have all the necessary files when downloaded # * Checks that providing a path in a download item gets used rather # than triggering another download shp_path_template = str(tmp_path / '{category}_{resolution}_{name}.shp') # picking a small-ish file to speed up download times, the file itself # isn't important - it is the download mechanism that is. format_dict = { 'category': 'physical', 'name': 'rivers_lake_centerlines', 'resolution': '110m' } dnld_item = NEShpDownloader(target_path_template=shp_path_template) # check that the file gets downloaded the first time path is called with mock.patch.object(dnld_item, 'acquire_resource', wraps=dnld_item.acquire_resource) as counter: with pytest.warns(cartopy.io.DownloadWarning, match="Downloading:"): shp_path = dnld_item.path(format_dict) counter.assert_called_once() assert shp_path_template.format(**format_dict) == shp_path # check that calling path again doesn't try re-downloading with mock.patch.object(dnld_item, 'acquire_resource', wraps=dnld_item.acquire_resource) as counter: assert dnld_item.path(format_dict) == shp_path counter.assert_not_called() # check that we have the shp and the shx exts = ['.shp', '.shx'] for ext in exts: stem = os.path.splitext(shp_path)[0] assert os.path.exists(stem + ext), \ f"Shapefile's {ext} file doesn't exist in {stem}{ext}" # check that providing a pre downloaded path actually works pre_dnld = NEShpDownloader(target_path_template='/not/a/real/file.txt', pre_downloaded_path_template=shp_path) # check that the pre_dnld downloader doesn't re-download, but instead # uses the path of the previously downloaded item with mock.patch.object(pre_dnld, 'acquire_resource', wraps=pre_dnld.acquire_resource) as counter: assert pre_dnld.path(format_dict) == shp_path counter.assert_not_called()