def proj_to_cartopy(proj):
import cartopy.crs as ccrs<EOL>try:<EOL><INDENT>from osgeo import osr<EOL>has_gdal = True<EOL><DEDENT>except ImportError:<EOL><INDENT>has_gdal = False<EOL><DEDENT>proj = check_crs(proj)<EOL>if proj.is_latlong():<EOL><INDENT>return ccrs.PlateCarree()<EOL><DEDENT>srs = proj.srs<EOL>if has_gdal:<EOL><INDENT>s1 = osr.SpatialReference()<EOL>s1.ImportFromProj4(proj.srs)<EOL>srs = s1.ExportToProj4()<EOL><DEDENT>km_proj = {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:k>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>km_globe = {'<STR_LIT:a>': '<STR_LIT>',<EOL>'<STR_LIT:b>': '<STR_LIT>',<EOL>}<EOL>km_std = {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>kw_proj = dict()<EOL>kw_globe = dict()<EOL>kw_std = dict()<EOL>for s in srs.split('<STR_LIT:+>'):<EOL><INDENT>s = s.split('<STR_LIT:=>')<EOL>if len(s) != <NUM_LIT:2>:<EOL><INDENT>continue<EOL><DEDENT>k = s[<NUM_LIT:0>].strip()<EOL>v = s[<NUM_LIT:1>].strip()<EOL>try:<EOL><INDENT>v = float(v)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>if k == '<STR_LIT>':<EOL><INDENT>if v == '<STR_LIT>':<EOL><INDENT>cl = ccrs.TransverseMercator<EOL><DEDENT>if v == '<STR_LIT>':<EOL><INDENT>cl = ccrs.LambertConformal<EOL><DEDENT>if v == '<STR_LIT>':<EOL><INDENT>cl = ccrs.Mercator<EOL><DEDENT>if v == '<STR_LIT>':<EOL><INDENT>cl = ccrs.UTM<EOL><DEDENT><DEDENT>if k in km_proj:<EOL><INDENT>kw_proj[km_proj[k]] = v<EOL><DEDENT>if k in km_globe:<EOL><INDENT>kw_globe[km_globe[k]] = v<EOL><DEDENT>if k in km_std:<EOL><INDENT>kw_std[km_std[k]] = v<EOL><DEDENT><DEDENT>globe = None<EOL>if kw_globe:<EOL><INDENT>globe = ccrs.Globe(**kw_globe)<EOL><DEDENT>if kw_std:<EOL><INDENT>kw_proj['<STR_LIT>'] = (kw_std['<STR_LIT>'], kw_std['<STR_LIT>'])<EOL><DEDENT>if cl.__name__ == '<STR_LIT>':<EOL><INDENT>kw_proj.pop('<STR_LIT>', None)<EOL>kw_proj.pop('<STR_LIT>', None)<EOL><DEDENT>return cl(globe=globe, **kw_proj)<EOL>
Converts a pyproj.Proj to a cartopy.crs.Projection

(Code copied from https://github.com/fmaussion/salem)

Parameters
----------
proj: pyproj.Proj
    the projection to convert
Returns
-------
a cartopy.crs.Projection object