def add_world_ocean_basemap(self, target_resolution=[600, 600]): """ Adds ESRI's Ocean Basemap to the map background. Args ---- target_resolution (tuple or list): the resolution in pixels of the basemap. Note ---- This function required the following lines to be copy-pasted into the file ogc_clients.py of the cartopy package, in order to work: ** START COPY BELOW ** METERS_PER_UNIT = { 'urn:ogc:def:crs:EPSG::27700': 1, 'urn:ogc:def:crs:EPSG::900913': 1, 'urn:ogc:def:crs:EPSG:6.18.3:3857': 1, 'urn:ogc:def:crs:EPSG::4326': _WGS84_METERS_PER_UNIT, 'urn:ogc:def:crs:OGC:1.3:CRS84': _WGS84_METERS_PER_UNIT, 'urn:ogc:def:crs:EPSG::3031': 1, 'urn:ogc:def:crs:EPSG::3413': 1 } _URN_TO_CRS = collections.OrderedDict([ ('urn:ogc:def:crs:OGC:1.3:CRS84', ccrs.PlateCarree()), ('urn:ogc:def:crs:EPSG::4326', ccrs.PlateCarree()), ('urn:ogc:def:crs:EPSG::900913', ccrs.GOOGLE_MERCATOR), ('urn:ogc:def:crs:EPSG:6.18.3:3857', ccrs.GOOGLE_MERCATOR), ('urn:ogc:def:crs:EPSG::27700', ccrs.OSGB()), ('urn:ogc:def:crs:EPSG::3031', ccrs.Stereographic( central_latitude=-90, true_scale_latitude=-71)), ('urn:ogc:def:crs:EPSG::3413', ccrs.Stereographic( central_longitude=-45, central_latitude=90, true_scale_latitude=70)) ]) ** STOP COPY ABOVE ** """ # Web map server wmts = 'http://services.arcgisonline.com/arcgis/rest/services/Ocean/' wmts += 'World_Ocean_Base/MapServer/WMTS/1.0.0/WMTSCapabilities.xml' wmts = WMTSRasterSource(wmts, 'Ocean_World_Ocean_Base') proj = self.projection extent = self.get_extent() img = wmts.fetch_raster(proj, extent, target_resolution) self.imshow(img[0][0], extent=img[0][1], transform=proj, origin='upper', interpolation='bicubic')
def test_wmts_tile_caching(): image_cache = WMTSRasterSource._shared_image_cache image_cache.clear() assert len(image_cache) == 0 url = 'http://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi' wmts = WebMapTileService(url) layer_name = 'MODIS_Terra_CorrectedReflectance_TrueColor' source = WMTSRasterSource(wmts, layer_name) gettile_counter = CallCounter(wmts, 'gettile') crs = ccrs.PlateCarree() extent = (-180, 180, -90, 90) resolution = (20, 10) with gettile_counter: source.fetch_raster(crs, extent, resolution) n_tiles = 2 assert gettile_counter.count == n_tiles, ('Too many tile requests - ' 'expected {}, got {}.'.format( n_tiles, gettile_counter.count) ) gc.collect() assert len(image_cache) == 1 assert len(image_cache[wmts]) == 1 tiles_key = (layer_name, '0') assert len(image_cache[wmts][tiles_key]) == n_tiles # Second time around we shouldn't request any more tiles so the # call count will stay the same. with gettile_counter: source.fetch_raster(crs, extent, resolution) assert gettile_counter.count == n_tiles, ('Too many tile requests - ' 'expected {}, got {}.'.format( n_tiles, gettile_counter.count) ) gc.collect() assert len(image_cache) == 1 assert len(image_cache[wmts]) == 1 tiles_key = (layer_name, '0') assert len(image_cache[wmts][tiles_key]) == n_tiles # Once there are no live references the weak-ref cache should clear. del source, wmts, gettile_counter gc.collect() assert len(image_cache) == 0
def test_wmts_tile_caching(): image_cache = WMTSRasterSource._shared_image_cache image_cache.clear() assert len(image_cache) == 0 url = 'https://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi' wmts = WebMapTileService(url) layer_name = 'MODIS_Terra_CorrectedReflectance_TrueColor' source = WMTSRasterSource(wmts, layer_name) gettile_counter = CallCounter(wmts, 'gettile') crs = ccrs.PlateCarree() extent = (-180, 180, -90, 90) resolution = (20, 10) with gettile_counter: source.fetch_raster(crs, extent, resolution) n_tiles = 2 assert gettile_counter.count == n_tiles, ('Too many tile requests - ' 'expected {}, got {}.'.format( n_tiles, gettile_counter.count) ) gc.collect() assert len(image_cache) == 1 assert len(image_cache[wmts]) == 1 tiles_key = (layer_name, '0') assert len(image_cache[wmts][tiles_key]) == n_tiles # Second time around we shouldn't request any more tiles so the # call count will stay the same. with gettile_counter: source.fetch_raster(crs, extent, resolution) assert gettile_counter.count == n_tiles, ('Too many tile requests - ' 'expected {}, got {}.'.format( n_tiles, gettile_counter.count) ) gc.collect() assert len(image_cache) == 1 assert len(image_cache[wmts]) == 1 tiles_key = (layer_name, '0') assert len(image_cache[wmts][tiles_key]) == n_tiles # Once there are no live references the weak-ref cache should clear. del source, wmts, gettile_counter gc.collect() assert len(image_cache) == 0
def test_wmts_tile_caching(): image_cache = WMTSRasterSource._shared_image_cache image_cache.clear() assert len(image_cache) == 0 url = 'https://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi' wmts = WebMapTileService(url) layer_name = 'MODIS_Terra_CorrectedReflectance_TrueColor' source = WMTSRasterSource(wmts, layer_name) crs = ccrs.PlateCarree() extent = (-180, 180, -90, 90) resolution = (20, 10) n_tiles = 2 with mock.patch.object(wmts, 'gettile', wraps=wmts.gettile) as gettile_counter: source.fetch_raster(crs, extent, resolution) assert gettile_counter.call_count == n_tiles, ( f'Too many tile requests - expected {n_tiles}, got ' f'{gettile_counter.call_count}.') del gettile_counter gc.collect() assert len(image_cache) == 1 assert len(image_cache[wmts]) == 1 tiles_key = (layer_name, '0') assert len(image_cache[wmts][tiles_key]) == n_tiles # Second time around we shouldn't request any more tiles. with mock.patch.object(wmts, 'gettile', wraps=wmts.gettile) as gettile_counter: source.fetch_raster(crs, extent, resolution) gettile_counter.assert_not_called() del gettile_counter gc.collect() assert len(image_cache) == 1 assert len(image_cache[wmts]) == 1 tiles_key = (layer_name, '0') assert len(image_cache[wmts][tiles_key]) == n_tiles # Once there are no live references the weak-ref cache should clear. del source, wmts gc.collect() assert len(image_cache) == 0