Checks if the crs represents a valid grid, projection or ESPG string. (Code copied and adapted from https://github.com/fmaussion/salem) Examples -------- >>> p = check_crs('epsg:26915 +units=m') >>> p.srs '+proj=utm +zone=15 +datum=NAD83 +units=m +no_defs' >>> p =
(crs)
| 94 | |
| 95 | |
| 96 | def check_crs(crs): |
| 97 | """ |
| 98 | Checks if the crs represents a valid grid, projection or ESPG string. |
| 99 | |
| 100 | (Code copied and adapted from https://github.com/fmaussion/salem) |
| 101 | |
| 102 | Examples |
| 103 | -------- |
| 104 | >>> p = check_crs('epsg:26915 +units=m') |
| 105 | >>> p.srs |
| 106 | '+proj=utm +zone=15 +datum=NAD83 +units=m +no_defs' |
| 107 | >>> p = check_crs('wrong') |
| 108 | >>> p is None |
| 109 | True |
| 110 | |
| 111 | Returns |
| 112 | ------- |
| 113 | A valid crs if possible, otherwise None. |
| 114 | """ |
| 115 | import pyproj |
| 116 | |
| 117 | try: |
| 118 | crs_type = pyproj.crs.CRS |
| 119 | except AttributeError: |
| 120 | |
| 121 | class Dummy: |
| 122 | pass |
| 123 | |
| 124 | crs_type = Dummy |
| 125 | |
| 126 | if isinstance(crs, pyproj.Proj): |
| 127 | out = crs |
| 128 | elif isinstance(crs, crs_type): |
| 129 | out = pyproj.Proj(crs.to_wkt(), preserve_units=True) |
| 130 | elif isinstance(crs, dict) or isinstance(crs, str): |
| 131 | if isinstance(crs, str): |
| 132 | try: |
| 133 | crs = pyproj.CRS.from_wkt(crs) |
| 134 | except RuntimeError: |
| 135 | # quick fix for https://github.com/pyproj4/pyproj/issues/345 |
| 136 | crs = crs.replace(' ', '').replace('+', ' +') |
| 137 | try: |
| 138 | out = pyproj.Proj(crs, preserve_units=True) |
| 139 | except RuntimeError: |
| 140 | out = None |
| 141 | else: |
| 142 | out = None |
| 143 | return out |
| 144 | |
| 145 | |
| 146 | def proj_is_latlong(proj): |
no outgoing calls
searching dependent graphs…