Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

""" Projection handling - OSGEO/Rasterio, CF/NetCDF, and Cartopy 

 

See Also 

-------- 

 

* https://trac.osgeo.org/gdal/wiki/NetCDF_ProjectionTestingStatus 

* http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/cf-conventions.html#appendix-grid-mappings 

 

""" 

from collections import OrderedDict 

from functools import singledispatch 

import logging 

 

from rasterio.crs import CRS 

from osgeo import osr 

 

from .utils import crs2osr 

from .. import errors 

 

logger = logging.getLogger(__name__) 

 

 

#: dict: Mapping between OGC WKT <-> CF projection names 

CF_PROJECTION_NAMES = { 

'Albers_Conic_Equal_Area': 'albers_conic_equal_area', 

'Azimuthal_Equidistant': 'azimuthal_equidistant', 

'Lambert_Azimuthal_Equal_Area': 'lambert_azimuthal_equal_area', 

# ... 

'Sinusoidal': 'sinusoidal', 

'Transverse_Mercator': 'transverse_mercator', 

} 

 

#: dict: Mapping between CF <-> OGC WKT projection parameters for projections 

CF_PROJECTION_DEFS = { 

# http://cfconventions.org/cf-conventions/cf-conventions.html#lambert-azimuthal-equal-area 

'albers_conic_equal_area': ( 

('latitude_of_projection_origin', 'latitude_of_center'), 

('longitude_of_central_meridian', 'longitude_of_center'), 

('standard_parallel', ('standard_parallel_1', 'standard_parallel_2')), 

('false_easting', 'false_easting'), 

('false_northing', 'false_northing'), 

), 

'lambert_azimuthal_equal_area': ( 

('longitude_of_projection_origin', 'longitude_of_center'), 

('latitude_of_projection_origin', 'latitude_of_center'), 

('false_easting', 'false_easting'), 

('false_northing', 'false_northing'), 

), 

# http://cfconventions.org/cf-conventions/cf-conventions.html#_transverse_mercator 

'transverse_mercator': ( 

('latitude_of_projection_origin', 'latitude_of_origin'), 

('longitude_of_central_meridian', 'central_meridian'), 

('scale_factor_at_central_meridian', 'scale_factor'), 

('false_easting', 'false_easting'), 

('false_northing', 'false_northing'), 

), 

'universal_transverse_mercator': ( 

('utm_zone_number', 'utm_zone_number') 

), 

# http://cfconventions.org/cf-conventions/cf-conventions.html#_sinusoidal 

'sinusoidal': ( 

('longitude_of_central_meridian', 'central_meridian'), 

('false_northing', 'false_northing'), 

('false_easting', 'false_easting'), 

), 

'latitude_longitude': (), # None 

} 

 

#: tuple: Mapping between CF <-> OGC WKT projection attribute name definitions 

# https://trac.osgeo.org/gdal/wiki/NetCDF_ProjectionTestingStatus#CoordinatesystemkeywordsstartingwithCF-1.7 

CF_CRS_NAMES = ( 

('horizontal_datum_name', 'GEOGCS|DATUM'), 

('reference_ellipsoid_name', 'GEOGCS|DATUM|SPHEROID'), 

('towgs84', 'GEOGCS|DATUM|TOWGS84'), 

('prime_meridian_name', 'GEOGCS|PRIMEM') 

) 

 

#: tuple: mapping from CF ellipsoid parameter to SpatialReference method calls 

CF_ELLIPSOID_DEFS = ( 

('semi_major_axis', 'GetSemiMajor'), 

('semi_minor_axis', 'GetSemiMinor'), 

('inverse_flattening', 'GetInvFlattening') 

) 

# TODO: support more... 

# TODO: Cartopy mappings 

 

 

# ============================================================================ 

# EPSG code 

@singledispatch 

def epsg_code(crs): 

"""Return EPSG code for a CRS 

 

Parameters 

---------- 

crs : CRS or osr.SpatialReference 

Coordinate reference system defined by an EPSG code 

 

Returns 

------- 

str 

EPSG code string 

 

Raises 

------ 

ValueError 

Raised if projection does not correspond to an EPSG code 

""" 

raise TypeError('Input `crs` needs to be rasterio `CRS` or ' 

'osr `SpatialReference`') 

 

 

@epsg_code.register(osr.SpatialReference) 

def _epsg_code_osr(crs): 

try: 

status = crs.AutoIdentifyEPSG() 

except RuntimeError as e: 

raise ValueError('Cannot decode EPSG code from CRS') 

else: 

if status == 0: 

auth = crs.GetAuthorityName(None) 

code = crs.GetAuthorityCode(None) 

return '%s:%s' % (auth.lower(), code) 

 

 

@epsg_code.register(CRS) 

def _epsg_code_rasterio(crs): 

osr_crs = crs2osr(crs) 

return _epsg_code_osr(osr_crs) 

 

 

def crs_longname(crs): 

""" Return name of a CRS / ellipsoid pair 

 

Parameters 

---------- 

crs : rasterio.crs.CRS 

CRS 

 

Returns 

------- 

str 

Lowercase projection name (see keys of :py:data:`CF_PROJECTION_DEFS`) 

 

Examples 

-------- 

>>> crs_longname(CRS.from_epsg(3857)) # Web Mercator 

'WGS 84 / Pseudo-Mercator' 

>>> crs_longname(CRS.from_epsg(32619)) # UTM19N 

'WGS 84 / UTM zone 19N' 

""" 

# This doesn't necessarily relate to CF but it's nice to have 

crs_osr = crs2osr(crs) 

if crs.is_projected: 

return crs_osr.GetAttrValue('PROJCS') 

elif crs.is_geographic: 

return crs_osr.GetAttrValue('GEOGCS') 

 

 

# ============================================================================ 

# CF info / parameters 

def cf_crs_name(crs): 

""" Return CF name of a CRS projection 

 

Parameters 

---------- 

crs : rasterio.crs.CRS 

CRS 

 

Returns 

------- 

str 

Lowercase projection name (see keys of :py:mod:`CF_PROJECTION_DEFS`) 

 

Examples 

-------- 

>>> cf_crs_attrs(CRS.from_epsg(4326)) # WGS84 

'latitude_longitude' 

>>> cf_crs_attrs(CRS.from_epsg(32619)) # UTM19N 

'transverse_mercator' 

""" 

crs_osr = crs2osr(crs) 

if crs.is_projected: 

name = crs_osr.GetAttrValue('PROJECTION') 

if name not in CF_PROJECTION_NAMES: 

if crs.is_valid: 

raise errors.TODO(f'Cannot handle "{name}" CRS types ' 

f'yet: {crs.wkt}') 

else: 

raise KeyError('Unsupported CRS "{0!r}"'.format(crs)) 

return CF_PROJECTION_NAMES[name] 

else: 

return 'latitude_longitude' 

 

 

def cf_crs_attrs(crs): 

""" Return CF-compliant CRS info to prevent "unknown" CRS/Ellipse/Geoid 

 

Parameters 

---------- 

crs: rasterio.crs.CRS 

CRS 

 

Returns 

------- 

OrderedDict 

CF attributes 

 

References 

---------- 

.. [1] https://cf-pcmdi.llnl.gov/trac/wiki/Cf2CrsWkt#Table2-FutureCF-1.7CFGridMappingAttributes 

 

Examples 

-------- 

>>> cf_crs_attrs(CRS.from_epsg(4326)) # WGS84 

OrderedDict([('geographic_coordinate_system_name', 'WGS 84'), 

('horizontal_datum_name', 'WGS_1984'), 

('reference_ellipsoid_name', 'WGS 84'), 

('prime_meridian_name', 'Greenwich')]) 

>>> cf_crs_attrs(CRS.from_epsg(32619)) # UTM19N 

OrderedDict([('projected_coordinate_system_name', 'WGS 84 / UTM zone 19N'), 

('horizontal_datum_name', 'WGS_1984'), 

('reference_ellipsoid_name', 'WGS 84'), 

('prime_meridian_name', 'Greenwich')]) 

""" 

osr_crs = crs2osr(crs) 

attrs = OrderedDict() 

 

long_name = crs_longname(crs) 

if crs.is_projected: 

attrs['projected_coordinate_system_name'] = long_name 

elif crs.is_geographic: 

attrs['geographic_coordinate_system_name'] = long_name 

 

for cf_parm, osgeo_parm in CF_CRS_NAMES: 

value = osr_crs.GetAttrValue(osgeo_parm) 

if value: # only record if not None so we can serialize 

attrs[cf_parm] = value 

return attrs 

 

 

def cf_proj_params(crs): 

""" Return projection parameters for a CRS 

 

Parameters 

---------- 

crs: rasterio.crs.CRS 

CRS 

 

Returns 

------- 

OrderedDict 

CRS parameters and values 

 

Raises 

------ 

stems.errors.TODO 

Raise if CRS isn't supported yet 

 

Examples 

-------- 

>>> cf_crs_attrs(CRS.from_epsg(4326)) # WGS84 

OrderedDict() 

>>> cf_crs_attrs(CRS.from_epsg(32619)) # UTM19N 

OrderedDict([('latitude_of_projection_origin', 0.0), 

('longitude_of_central_meridian', -69.0), 

('scale_factor_at_central_meridian', 0.9996), 

('false_easting', 500000.0), 

('false_northing', 0.0)]) 

""" 

name = cf_crs_name(crs) 

osr_crs = crs2osr(crs) 

 

if name not in CF_PROJECTION_DEFS: 

raise errors.TODO(f'Cannot handle "{name}" CRS types yet') 

 

parms = OrderedDict() # "parm" is eww, but ref to `GetProjParm` 

for cf_parm, osgeo_parm in CF_PROJECTION_DEFS[name]: 

if isinstance(osgeo_parm, (list, tuple)): 

parms[cf_parm] = tuple(osr_crs.GetProjParm(p) for p in osgeo_parm) 

else: 

parms[cf_parm] = osr_crs.GetProjParm(osgeo_parm) 

 

return parms 

 

 

def cf_ellps_params(crs): 

""" Return ellipsoid parameters for a CRS 

 

Parameters 

---------- 

crs: rasterio.crs.CRS 

CRS 

 

Returns 

------- 

OrderedDict 

Ellipsoid parameters and values 

 

Examples 

-------- 

>>> cf_crs_attrs(CRS.from_epsg(4326)) # WGS84 

OrderedDict([('semi_major_axis', 6378137.0), 

('semi_minor_axis', 6356752.314245179), 

('inverse_flattening', 298.257223563)]) 

>>> cf_crs_attrs(CRS.from_epsg(32619)) # UTM19N 

OrderedDict([('semi_major_axis', 6378137.0), 

('semi_minor_axis', 6356752.314245179), 

('inverse_flattening', 298.257223563)]) 

""" 

osr_crs = crs2osr(crs) 

 

return OrderedDict( 

(key, getattr(osr_crs, func)()) 

for (key, func) in CF_ELLIPSOID_DEFS 

) 

 

 

def cf_xy_coord_names(crs): 

""" Returns appropriate names for coordinates given CRS 

 

Parameters 

---------- 

crs : rasterio.crs.CRS 

Coordinate reference system 

 

Returns 

------- 

str : x_coord_name 

X coordinate name 

str : y_coord_name 

Y coordinate name 

 

Examples 

-------- 

>>> cf_crs_attrs(CRS.from_epsg(4326)) # WGS84 

('longitude', 'latitude') 

>>> cf_crs_attrs(CRS.from_epsg(32619)) # UTM19N 

('x', 'y') 

""" 

# CRSError is raised if neither is true, so we don't handle 

if crs.is_geographic: 

return 'longitude', 'latitude' 

elif crs.is_projected: 

return 'x', 'y' 

else: 

raise ValueError('CRS must either be geographic or projected')