You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The masked cube will have the same shape and coordinates as the original cube,
381
+
but the data array now includes an associated boolean mask, and the cube's
382
+
`data` property is now a :py:class:`numpy.ma.MaskedArray`.
383
+
338
384
.. _masking-from-shapefile:
339
385
340
386
Masking from a shapefile
@@ -346,26 +392,77 @@ Often we want to perform some kind of analysis over a complex geographical featu
346
392
- over a continent, country, or list of countries
347
393
- over a river watershed or lake basin
348
394
- over states or administrative regions of a country
395
+
- extract data along the trajectory of a storm track
396
+
- extract data at specific points of interest such as cities or weather stations
397
+
398
+
These geographical features can often be described by `ESRI Shapefiles`_.
399
+
Shapefiles are a file format first developed for GIS software in the 1990s, and
400
+
`Natural Earth`_ maintain a large freely usable database of shapefiles of many
401
+
geographical and political divisions, accessible via `cartopy`_. Users may also
402
+
provide or create their own custom shapefiles for `cartopy`_ to load, or or any
403
+
other source that can be interpreted as a `shapely.Geometry`_ object, such as
404
+
shapes encoded in a geoJSON or KML file.
405
+
406
+
The :func:`iris.util.mask_cube_from_shape` function facilitates cube masking
407
+
from shapefiles. Once a shape is loaded as a `shapely.Geometry`, and passed to
408
+
the function, any data outside the bounds of the shape geometry is masked.
409
+
410
+
.. important::
411
+
For best masking results, both the cube **and** masking shape (geometry)
412
+
should have a coordinate reference system (CRS) defined. Note that the CRS of
413
+
the masking geometry must be provided explicitly to :func:`iris.util.mask_cube_from_shape`
414
+
(via the ``shape_crs`` keyword argument), whereas the :class:`iris.cube.Cube`
415
+
CRS is read from the cube itself.
416
+
417
+
The cube **must** have a :attr:`iris.coords.Coord.coord_system` defined
418
+
otherwise an error will be raised.
349
419
350
-
These geographical features can often be described by `ESRI Shapefiles`_. Shapefiles are a file format first developed for GIS software in the 1990s, and `Natural Earth`_ maintain a large freely usable database of shapefiles of many geographical and political divisions,
351
-
accessible via `cartopy`_. Users may also provide their own custom shapefiles for `cartopy`_ to load, or their own underlying geometry in the same format as a shapefile geometry.
420
+
.. note::
421
+
Because shape vectors are inherently Cartesian in nature, they contain no
422
+
inherent understanding of the spherical geometry underpinning geographic
423
+
coordinate systems. For this reason, **shapefiles or shape vectors that
424
+
cross the antimeridian or poles are not supported by this function** to
425
+
avoid unexpected masking behaviour.
426
+
427
+
For shapes that do cross these boundaries, this function expects the user
428
+
to undertake fixes upstream of Iris, using tools like `GDAL`_ or
429
+
`antimeridian`_ to ensure correct geometry wrapping.
430
+
431
+
As an introductory example, we load a shapefile of country borders for Brazil
432
+
from `Natural Earth`_ via the `Cartopy_shapereader`_. The `.geometry` attribute
433
+
of the records in the reader contain the `Shapely`_ polygon we're interested in.
434
+
They contain the coordinates that define the polygon being masked under the
435
+
WGS84 coordinate system. We pass this to the :class:`iris.util.mask_cube_from_shape`
436
+
function and this returns a copy of the cube with a :py:class:`numpy.masked_array`
437
+
as the data payload, where the data outside the shape is hidden by the masked
438
+
array.
352
439
353
-
These shapefiles can be used to mask an iris cube, so that any data outside the bounds of the shapefile is hidden from further analysis or plotting.
First, we load the correct shapefile from NaturalEarth via the `Cartopy_shapereader`_ instructions. Here we get one for Brazil.
356
-
The `.geometry` attribute of the records in the reader contain the `Shapely`_ polygon we're interested in. They contain the coordinates that define the polygon (or set of lines) being masked
357
-
and once we have those we just need to provide them to the :class:`iris.util.mask_cube_from_shapefile` function.
358
-
This returns a copy of the cube with a :class:`numpy.masked_array` as the data payload, where the data outside the shape is hidden by the masked array. We can see this in the following example.
443
+
We can see that the dimensions of the cube haven't changed - the plot still has
444
+
a global extent. But only the data over Brazil is plotted - the rest has been
445
+
masked out.
359
446
447
+
.. important::
448
+
Because we do not explicitly pass a CRS for the shape geometry to
449
+
:func:`iris.util.mask_cube_from_shape`, the function assumes the geometry
However, a :class:`iris.cube.Cube` and `Shapely`_ geometry do not need to have
453
+
the same CRS, as long as both have a CRS defined. Where the CRS of the
454
+
:class:`iris.cube.Cube` and geometry differ, :func:`iris.util.mask_cube_from_shape`
455
+
will reproject the geometry (via `GDAL`_) onto the cube's CRS prior to masking.
456
+
The masked cube will be returned in the same CRS as the input cube.
363
457
364
-
We can see that the dimensions of the cube haven't changed - the plot is still global. But only the data over Brazil is plotted - the rest has been masked out.
458
+
In the following example, we load a cube containing satellite derived temperature
459
+
data in a stereographic projection (with projected coordinates with units of
460
+
metres), and mask it to only show data over the United Kingdom, based on a
461
+
shapefile of the UK boundary defined in WGS84 lat-lon coordinates.
While Iris will try to dynamically adjust the shapefile to mask cubes of different projections, it can struggle with rotated pole projections and cubes with Meridians not at 0°
368
-
Converting your Cube's coordinate system may help if you get a fully masked cube as the output from this function unexpectedly.
369
466
370
467
371
468
Cube Iteration
@@ -481,8 +578,11 @@ Similarly, Iris cubes have indexing capability::
0 commit comments