Cartopy developer interfaces¶
Cartopy exposes several interfaces to help make it easier to add new functionality quickly and easily.
Data/Download API¶
In order to keep the size of a cartopy release low, the majority of data is
not included as standard. This means that, when developing new features, it is
often necessary to provide interfaces which can acquire data from external
sources (normally via HTTP). The Downloader
class
has been designed to make this process as easy as possible for developers to
extend, whilst still making it possible for users to configure in their own
way.
-
class
cartopy.io.
Downloader
(url_template, target_path_template, pre_downloaded_path_template='')[source]¶ Represents a resource, that can be configured easily, which knows how to acquire itself (perhaps via HTTP).
The key interface method is
path()
- typically all external calls will be made to that method. To get hold of an appropriateDownloader
instance theDownloader.from_config()
static method should be considered.Parameters: - url_template – The template of the full URL representing this resource.
- target_path_template – The template of the full path to the file
that this Downloader represents. Typically the path will be a
subdirectory of
config['data_dir']
, but this is not a strict requirement. If the file does not exist when callingDownloader.path()
it will be downloaded to this location. - pre_downloaded_path_template (optional) – The template of a full path of a file which has been downloaded
outside of this Downloader which should be used as the file that
this resource represents. If the file does not exist when
Downloader.path()
is called it will not be downloaded to this location (unlike thetarget_path_template
argument).
Note
All
*_template
arguments should be formattable using the standardstring.format()
rules. The formatting itself is not done until a call to a subsequent method (such asDownloader.path()
).-
FORMAT_KEYS
= ('config',)¶ The minimum keys which should be provided in the
format_dict
argument for thepath
,url
,target_path
,pre_downloaded_path
andacquire_resource
methods.
-
acquire_resource
(target_path, format_dict)[source]¶ Download, via HTTP, the file that this resource represents. Subclasses will typically override this method.
Parameters: format_dict – The dictionary which is used to replace certain template variables. Subclasses should document which keys are expected as a minimum in their FORMAT_KEYS
class attribute.
-
static
from_config
(specification, config_dict=None)[source]¶ The
from_config
static method implements the logic for acquiring a Downloader (sub)class instance from the config dictionary.Parameters: - specification – Should be iterable, as it will be traversed in
reverse order to find the most appropriate Downloader instance
for this specification. An example specification is
('shapefiles', 'natural_earth')
for the Natural Earth shapefiles. - config_dict (optional) – typically this is left as None to use the
default
cartopy.config
“downloaders” dictionary.
Examples
>>> from cartopy.io import Downloader >>> >>> dnldr = Downloader('https://example.com/{name}', './{name}.txt') >>> config = {('level_1', 'level_2'): dnldr} >>> d1 = Downloader.from_config(('level_1', 'level_2', 'level_3'), ... config_dict=config) >>> print(d1.url_template) https://example.com/{name} >>> print(d1.url({'name': 'item_name'})) https://example.com/item_name
- specification – Should be iterable, as it will be traversed in
reverse order to find the most appropriate Downloader instance
for this specification. An example specification is
-
path
(format_dict)[source]¶ Returns the path to a file on disk that this resource represents.
If the file doesn’t exist in
pre_downloaded_path()
then it will check whether it exists intarget_path()
, otherwise the resource will be downloaded viaacquire_resouce()
fromurl()
totarget_path()
.Typically, this is the method that most applications will call, allowing implementors of new Downloaders to specialise
acquire_resource()
.Parameters: format_dict – The dictionary which is used to replace certain template variables. Subclasses should document which keys are expected as a minimum in their FORMAT_KEYS
class attribute.
-
pre_downloaded_path
(format_dict)[source]¶ The path on disk of the file that this resource represents, if it does not exist, then no further action will be taken with this path, and all further processing will be done using
target_path()
instead.Parameters: format_dict – The dictionary which is used to replace certain template variables. Subclasses should document which keys are expected as a minimum in their FORMAT_KEYS
class attribute.
-
target_path
(format_dict)[source]¶ The path on disk of the file that this resource represents, must either exist, or be writable by the current user. This method does not check either of these conditions.
Parameters: format_dict – The dictionary which is used to replace certain template variables. Subclasses should document which keys are expected as a minimum in their FORMAT_KEYS
class attribute.
An example of specialising this class can be found in
cartopy.io.shapereader.NEShpDownloader
which enables the downloading of
zipped shapefiles from the http://www.naturalearthdata.com website. All
known subclasses of Downloader
are listed below for
reference:
cartopy.io.shapereader.NEShpDownloader
cartopy.io.srtm.SRTMDownloader
Raster images¶
The abstraction between retrieval and visualisation of raster data means
that the cartopy.io.RasterSource
class exists to retrieve an image
(given sufficient context of projection, extent, resolution etc.) while in the
matplotlib interface the
cartopy.mpl.slippy_image_artist.SlippyImageArtist
class feeds the appropriate information to the
RasterSource
and visualises it on a map.
The orchestration in Matplotlib is made more convenient
to the user of a GeoAxes
through the
add_raster
method. Anything which exposes
the validate_projection
and fetch_raster
methods in the form described
in RasterSource
can be used as a slippy maps source in
this way.
-
class
cartopy.io.
RasterSource
[source]¶ Define the cartopy raster fetching interface.
A
RasterSource
instance is able to supply images and associated extents (as a sequence ofLocatedImage
instances) through itsfetch_raster()
method.As a result, further interfacing classes, such as
cartopy.mpl.slippy_image_artist.SlippyImageArtist
, can then make use of the interface for functionality such as interactive image retrieval with pan and zoom functionality.-
fetch_raster
(projection, extent, target_resolution)[source]¶ Return a sequence of images with extents given some constraining information.
Parameters: - projection (
cartopy.crs.Projection
) – The desired projection of the image. - extent (iterable of length 4) – The extent of the requested image in projected coordinates.
The resulting image may not be defined exactly by these extents,
and so the extent of the resulting image is also returned. The
extents must be defined in the form
(min_x, max_x, min_y, max_y)
. - target_resolution (iterable of length 2) – The desired resolution of the image as
(width, height)
in pixels.
Returns: images – A sequence of
LocatedImage
instances.- projection (
-
validate_projection
(projection)[source]¶ Raise an error if this raster source cannot provide images in the specified projection.
Parameters: projection ( cartopy.crs.Projection
) – The desired projection of the image.
-
-
class
cartopy.io.
LocatedImage
[source]¶ - Define an image and associated extent in the form:
image, (min_x, max_x, min_y, max_y)
Create new instance of LocatedImage(image, extent)
The SlippyImageArtist
class
provides panning and zooming of image sources which are able to
re-retrieve data (such as that from a web service) for efficient and
interactive visualisation. Generally the SlippyImageArtist is a developer’s
interface, with users often creating a
SlippyImageArtist
instance through
the GeoAxes’ add_raster()
method.