Create projections from CF attributes #2548
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes #2099.
Rationale
As detailed in #2099, the
from_cfclass method inherited frompyprojdoes not work with cartopy as it creates apyprojclass, and not the cartopy class it was called from.The following was the closest I could get:
However, as
p.bounds is Nonehere, plotting with matplotlib fails if you pass this as the projection of the axes.Thus, I thought it would be useful to be able to create the exact cartopy projection needed from a set of CF convention attributes.
Implications
The only "breaking" change here is that calling
from_cfon a projection that does not map to a CF grid mapping will fail with aNotImplementedErrorinstead of returning apyprojobject. But that behaviour was already broken I would say.This PR exposes
cartopy.crs.from_cfthat takes in all attributes of the grid mapping variable. It uses a lookup dict to map from the CFgrid_mapping_nameto cartopy's Projection and calls that projection'sfrom_cfconstructor.The PR does not try to validate CF attributes, except that it will raise a
KeyErrorif an attribute that the conventions does not flag as optional is missing. The mappings of grid mapping names and attributes is based on the description from the conventions themselves and on this (old) note from the CF conventions wiki.Checklist
Tests added (minimal)
I did sign the CLA.