Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to do a repr on a CRS. #120

Open
pelson opened this issue Oct 25, 2012 · 1 comment
Open

Ability to do a repr on a CRS. #120

pelson opened this issue Oct 25, 2012 · 1 comment

Comments

@pelson
Copy link
Member

pelson commented Oct 25, 2012

Currently there is not enought infrastructure in place to be able to do a code generating repr on a CRS. This would be a very useful feature and is something that we should certainly strive for.

@DPeterK
Copy link
Member

DPeterK commented Nov 14, 2017

I looked into this and made the following changes to add a basic __repr__ (and _repr_html_ for a visual representation of a Projection in a jupyter notebook):

diff --git a/lib/cartopy/_crs.pyx b/lib/cartopy/_crs.pyx
index 81a321c..b4e3ae0 100644
--- a/lib/cartopy/_crs.pyx
+++ b/lib/cartopy/_crs.pyx
@@ -202,8 +202,23 @@ cdef class CRS:
         """
         self.__init__(self, **state)
 
+    def __repr__(self):
+        name = self.__class__.__name__
+        str_params = {'lat_0': 'central_latitude',
+                      'lon_0': 'central_longitude',
+                      'h': 'height',
+                      'ellps': 'ellipse',
+                      'proj': 'projection'}
+        str_items_dict = {}
+        for param_name, param_desc in str_params.items():
+            proj4_param = self.proj4_params.get(param_name, None)
+            if proj4_param is not None:
+                str_items_dict[param_desc] = proj4_param
+        params_str = ', '.join(['{}={}'.format(k, v)
+                                for k, v in str_items_dict.items()])
+        return '<{}: {}>'.format(name, params_str)
+
     # TODO
-    #def __str__
     #def _geod(self): # to return the pyproj.Geod
 
     def _as_mpl_transform(self, axes=None):
diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py
index 46d088e..1ba3187 100644
--- a/lib/cartopy/crs.py
+++ b/lib/cartopy/crs.py
@@ -146,6 +146,13 @@ class Projection(six.with_metaclass(ABCMeta, CRS)):
             domain = self._domain = sgeom.Polygon(self.boundary)
         return domain
 
+    def _repr_html_(self):
+        import matplotlib.pyplot as plt
+        ax = plt.axes(projection=self)
+        ax.set_global()
+        ax.coastlines('110m')
+        plt.show()
+
     def _as_mpl_axes(self):
         import cartopy.mpl.geoaxes as geoaxes
         return geoaxes.GeoAxes, {'map_projection': self}

This exposes the following problems:

  • what information do we want to include in the repr of a CRS? If we miss key information from the repr it may make the repr less worthwhile than not having a repr at all.
  • This is endemic of a larger problem with CRS: it has an inconsistent API (see Variations in Projection subclasses public API  #950).
  • The html repr of a projection renders immediately – instead we should be able to store it locally and display it at our choice in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants