Cartopy

Table Of Contents

Previous topic

Cartopy map gridlines and tick labels

Next topic

Cartopy developer interfaces

This Page

The cartopy Feature interface

class cartopy.feature.Feature(crs, **kwargs)[source]

Represents a collection of points, lines and polygons with convenience methods for common drawing and filtering operations.

Args:

  • crs - the coordinate reference system of this Feature
Kwargs:
Keyword arguments to be used when drawing this feature.

See also

To add features to the current matplotlib axes, see GeoAxes.

crs[source]

The cartopy CRS for the geometries in this feature.

geometries()[source]

Returns an iterator of (shapely) geometries for this feature.

intersecting_geometries(extent)[source]

Returns an iterator of shapely geometries that intersect with the given extent. The extent is assumed to be in the CRS of the feature. If extent is None, the method returns all geometries for this dataset.

kwargs[source]

The read-only dictionary of keyword arguments that are used when creating the matplotlib artists for this feature.


Specific Feature subclasses have been defined for common functionality, such as accessing Natural Earth or GSHHS shapefiles.

class cartopy.feature.ShapelyFeature(geometries, crs, **kwargs)[source]

A class capable of drawing a collection of shapely geometries.

Args:

  • geometries:

    A collection of shapely geometries.

  • crs:

    The cartopy CRS in which the provided geometries are defined.

Kwargs:
Keyword arguments to be used when drawing this feature.
class cartopy.feature.NaturalEarthFeature(category, name, scale, **kwargs)[source]

A simple interface to Natural Earth shapefiles.

See http://www.naturalearthdata.com/

Args:

  • category:

    The category of the dataset, i.e. either ‘cultural’ or ‘physical’.

  • name:

    The name of the dataset, e.g. ‘admin_0_boundary_lines_land’.

  • scale:

    The dataset scale, i.e. one of ‘10m’, ‘50m’, or ‘110m’. Corresponding to 1:10,000,000, 1:50,000,000, and 1:110,000,000 respectively.

Kwargs:
Keyword arguments to be used when drawing this feature.
class cartopy.feature.GSHHSFeature(scale='auto', levels=None, **kwargs)[source]

An interface to the GSHHS dataset.

See http://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html

Args:

  • scale:

    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 levels to draw (default is [1] which corresponds to coastlines).

Kwargs:
Keyword arguments to be used when drawing the feature. Defaults are edgecolor=’black’ and facecolor=’none’.

To simplify some very common cases, some pre-defined Features exist as cartopy.feature constants. The pre-defined Features are all small-scale (1:110m) Natural Earth datasets, and can be added with methods such as GeoAxes.add_feature:

Name Description
cartopy.feature.BORDERS
Country boundaries.
cartopy.feature.COASTLINE
Coastline, including major islands.
cartopy.feature.LAKES
Natural and artificial lakes.
cartopy.feature.LAND
Land polygons, including major islands.
cartopy.feature.OCEAN
Ocean polygons.
cartopy.feature.RIVERS
Single-line drainages, including lake centerlines.

Note

Any Natural Earth dataset can easily be used by creating an instance of cartopy.feature.NaturalEarthFeature. For example:

import cartopy.feature as cfeature
land_50m = cfeature.NaturalEarthFeature('physical', 'land', '50m',
                                        edgecolor='face',
                                        facecolor=cfeature.COLORS['land'])

A dictionary of some useful colors for drawing features also exists:

cartopy.feature.COLORS = {'water': array([ 0.59375 , 0.71484375, 0.8828125 ]), 'land': array([ 0.9375 , 0.9375 , 0.859375]), 'land_alt1': array([ 0.859375, 0.859375, 0.859375])}

A dictionary of colors useful for drawing Features.

The named keys in this dictionary represent the “type” of feature being plotted.

For a full list of names in this dictionary:

>>> import cartopy.feature
>>> sorted(cartopy.feature.COLORS.keys())
['land', 'land_alt1', 'water']

Example of using the Feature class with the matplotlib interface

import matplotlib.pyplot as plt

import cartopy.crs as ccrs
import cartopy.feature as cfeature


def main():
    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.set_extent([80, 170, -45, 30])

    # Put a background image on for nice sea rendering.
    ax.stock_img()

    # Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
    states_provinces = cfeature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m',
        facecolor='none')

    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(states_provinces, edgecolor='gray')

    plt.show()


if __name__ == '__main__':
    main()

(Source code)

../_images/feature_creation_01_001.png