2

Is there a method to get the CRS from a figure created with cartopy? Say for example, I only had access to the ax created here:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
ax = plt.axes(projection=ccrs.PlateCarree())

I was hoping to be able to do something like ax.get_crs() to return 'PlateCarree'

2 Answers 2

0

By using following function I speed up my search.

import re

def getPat2(keyword, L):
     
    list = [ item for item in L if re.search(keyword, item) is not None ]

    return list  #return results

So, I ran your code lines:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
ax = plt.axes(projection=ccrs.PlateCarree())

and afterward, I ran my function with:

>>>L = dir(ax)
>>>getPat2('crs', L)
[]  #not results
>>>getPat2('proj', L)
['projection']

So, method is 'projection'; not 'get_crs'.

Running my function again but with dir(ax.projection):

>>>M = dir(ax.projection)
>>>getPat2('proj', M)
['_project_line_string', '_project_linear_ring', '_project_multiline', '_project_multipoint', '_project_multipolygon', '_project_point', '_project_polygon', 'proj4_init', 'proj4_params', 'project_geometry']

With following command lines you can get proj4 parameters and project geometry.

>>>ax.projection.proj4_params
{'ellps': 'WGS84', 'a': 57.29577951308232, 'proj': 'eqc', 'lon_0': 0.0}
>>>ax.projection.proj4_init
'+ellps=WGS84 +a=57.29577951308232 +proj=eqc +lon_0=0.0 +no_defs'
>>>ax.projection.project_geometry
<bound method Projection.project_geometry of <cartopy.crs.PlateCarree object at 0x7f6274275678

You can use above function for exploring other 'methods' where explicitly also appears 'PlateCarree'.

1
  • That's a bit of a rough solution, but thanks it answers the question.
    – bbuzz31
    Commented Jan 29, 2021 at 20:56
0

Maybe it is too late, but just use

crs_ax = ax.projection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.