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

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

 

# -*- coding: utf-8 -*- 

 

u'''Military Grid Reference System (MGRS/NATO) classes L{Mgrs} and 

L{MGRSError} and functions L{parseMGRS} and L{toMgrs}. 

 

Pure Python implementation of MGRS / UTM conversion functions using 

an ellipsoidal earth model, transcribed from JavaScript originals by 

I{(C) Chris Veness 2014-2016} published under the same MIT Licence**, see 

U{MGRS<https://www.Movable-Type.co.UK/scripts/latlong-utm-mgrs.html>} and 

U{Module mgrs<https://www.Movable-Type.co.UK/scripts/geodesy/docs/module-mgrs.html>}. 

 

The MGRS/NATO grid references provides geocoordinate references 

covering the entire globe, based on UTM projections. 

 

MGRS references comprise a grid zone designation, a 100 km square 

identification, and an easting and northing (in metres). 

 

Depending on requirements, some parts of the reference may be omitted 

(implied), and easting/northing may be given to varying resolution. 

 

See also U{United States National Grid 

<https://www.FGDC.gov/standards/projects/FGDC-standards-projects/usng/fgdc_std_011_2001_usng.pdf>} 

and U{Military Grid Reference System<https://WikiPedia.org/wiki/Military_grid_reference_system>}. 

 

@newfield example: Example, Examples 

''' 

 

from pygeodesy.basics import halfs2, property_RO, _xkwds, \ 

_xinstanceof, _xzipairs 

from pygeodesy.datums import Datums, _ellipsoidal_datum 

from pygeodesy.errors import _parseX, _ValueError 

from pygeodesy.interns import NN, _band_, _COMMA_SPACE_, _datum_, \ 

_easting_, _northing_, _SPACE_, \ 

_SQUARE_, _zone_, _0_0 

from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY 

from pygeodesy.named import _NamedBase, _NamedTuple, _Pass, _xnamed 

from pygeodesy.streprs import enstr2 

from pygeodesy.units import Easting, Meter, Northing, Str 

from pygeodesy.utm import toUtm8, _to3zBlat, Utm 

from pygeodesy.utmupsBase import _hemi, UtmUps5Tuple 

 

import re # PYCHOK warning locale.Error 

 

__all__ = _ALL_LAZY.mgrs 

__version__ = '20.09.22' 

 

_100km = Meter( 100e3) # 100 km in meter 

_2000km = Meter(2000e3) # 2,000 km in meter 

# 100 km grid square column (‘e’) letters repeat every third zone 

_Le100k = 'ABCDEFGH', 'JKLMNPQR', 'STUVWXYZ' # grid E colums 

# 100 km grid square row (‘n’) letters repeat every other zone 

_Ln100k = 'ABCDEFGHJKLMNPQRSTUV', 'FGHJKLMNPQRSTUVABCDE' # grid N rows 

 

# split an MGRS string "12ABC1235..." into 3 parts 

_MGRSre = re.compile('([0-9]{1,2}[C-X]{1})([A-Z]{2})([0-9]+)', re.IGNORECASE) # regex 

_ZBGre = re.compile('([0-9]{1,2}[C-X]{1})([A-Z]{2})', re.IGNORECASE) # regex 

 

 

class MGRSError(_ValueError): 

'''Military Grid Reference System (MGRS) parse or other L{Mgrs} issue. 

''' 

pass 

 

 

class Mgrs(_NamedBase): 

'''Military Grid Reference System (MGRS/NATO) references, 

with method to convert to UTM coordinates. 

''' 

_band = NN # latitudinal band (C..X) 

_bandLat = None # band latitude (C{degrees90} or C{None}) 

_datum = Datums.WGS84 # Datum (L{Datum}) 

_easting = 0 # Easting (C{meter}), within 100 km grid square 

_en100k = NN # grid EN digraph (C{str}), 100 km grid square 

_northing = 0 # Northing (C{meter}), within 100 km grid square 

_zone = 0 # longitudinal zone (C{int}), 1..60 

 

def __init__(self, zone, en100k, easting, northing, 

band=NN, datum=Datums.WGS84, name=NN): 

'''New L{Mgrs} Military grid reference. 

 

@arg zone: 6° longitudinal zone (C{int}), 1..60 covering 180°W..180°E. 

@arg en100k: Two-letter EN digraph (C{str}), 100 km grid square. 

@arg easting: Easting (C{meter}), within 100 km grid square. 

@arg northing: Northing (C{meter}), within 100 km grid square. 

@kwarg band: Optional 8° latitudinal band (C{str}), C..X covering 80°S..84°N. 

@kwarg datum: Optional this reference's datum (L{Datum}, L{Ellipsoid}, 

L{Ellipsoid2} or L{a_f2Tuple}). 

@kwarg name: Optional name (C{str}). 

 

@raise MGRSError: Invalid MGRS grid reference, B{C{zone}}, B{C{en100k}}, 

B{C{easting}}, B{C{northing}} or B{C{band}}. 

 

@raise TypeError: Invalid B{C{datum}}. 

 

@example: 

 

>>> from pygeodesy import Mgrs 

>>> m = Mgrs('31U', 'DQ', 48251, 11932) # 31U DQ 48251 11932 

''' 

if name: 

self.name = name 

 

self._zone, self._band, self._bandLat = _to3zBlat(zone, band, MGRSError) 

 

try: 

en = str(en100k).upper() 

if len(en) != 2: 

raise IndexError # caught below 

self._en100k = en 

self._en100k2m() 

except IndexError: 

raise MGRSError(en100k=en100k) 

 

self._easting = Easting(easting, Error=MGRSError) 

self._northing = Northing(northing, Error=MGRSError) 

if datum not in (None, self._datum): 

self._datum = _ellipsoidal_datum(datum, name=name) 

 

def _en100k2m(self): 

# check and convert grid letters to meter 

z = self._zone - 1 

# get easting specified by e100k (note, +1 because 

# eastings start at 166e3 due to 500 km false origin) 

e = float(_Le100k[z % 3].index(self._en100k[0]) + 1) * _100km # metres 

# similarly, get northing specified by n100k 

n = float(_Ln100k[z % 2].index(self._en100k[1])) * _100km # metres 

return e, n 

 

@property_RO 

def band(self): 

'''Get the latitudinal band (C{str, 'A'|'B'..'Y'|'Z'}). 

''' 

return self._band 

 

@property_RO 

def bandLatitude(self): 

'''Get the band latitude (C{degrees90} or C{None}). 

''' 

return self._bandLat 

 

@property_RO 

def datum(self): 

'''Get the datum (L{Datum}). 

''' 

return self._datum 

 

@property_RO 

def en100k(self): 

'''Get the 2-character grid EN digraph (C{str}). 

''' 

return self._en100k 

 

digraph = en100k 

 

@property_RO 

def easting(self): 

'''Gets the easting (C{meter}). 

''' 

return self._easting 

 

@property_RO 

def northing(self): 

'''Get the northing (C{meter}). 

''' 

return self._northing 

 

def parse(self, strMGRS, name=NN): 

'''Parse a string to a similar L{Mgrs} instance. 

 

@arg strMGRS: The MGRS reference (C{str}), 

see function L{parseMGRS}. 

@kwarg name: Optional instance name (C{str}), 

overriding this name. 

 

@return: The similar instance (L{Mgrs}). 

 

@raise MGRSError: Invalid B{C{strMGRS}}. 

''' 

return parseMGRS(strMGRS, datum=self.datum, Mgrs=self.classof, 

name=name or self.name) 

 

def toRepr(self, prec=10, fmt=_SQUARE_, sep=_COMMA_SPACE_): # PYCHOK expected 

'''Return a string representation of this MGRS grid reference. 

 

@kwarg prec: Optional number of digits (C{int}), 4:km, 10:m. 

@kwarg fmt: Optional enclosing backets format (C{str}). 

@kwarg sep: Optional separator between name:values (C{str}). 

 

@return: This Mgrs as "[Z:00B, G:EN, E:meter, N:meter]" (C{str}). 

''' 

t = self.toStr(prec=prec, sep=None) 

return _xzipairs('ZGEN', t, sep=sep, fmt=fmt) 

 

def toStr(self, prec=10, sep=_SPACE_): # PYCHOK expected 

'''Return a string representation of this MGRS grid reference. 

 

Note that MGRS grid references are truncated, not rounded 

(unlike UTM coordinates). 

 

@kwarg prec: Optional number of digits (C{int}), 4:km, 10:m. 

@kwarg sep: Optional separator to join (C{str}) or C{None} 

to return an unjoined C{tuple} of C{str}s. 

 

@return: This Mgrs as "00B EN easting northing" (C{str}). 

 

@raise ValueError: Invalid B{C{prec}}. 

 

@example: 

 

>>> m = Mgrs(31, 'DQ', 48251, 11932, band='U') 

>>> m.toStr() # '31U DQ 48251 11932' 

''' 

t = '%02d%s' % (self._zone, self._band) 

t = enstr2(self._easting, self._northing, prec, t, self._en100k) 

return t if sep is None else sep.join(t) 

 

def toUtm(self, Utm=Utm): 

'''Convert this MGRS grid reference to a UTM coordinate. 

 

@kwarg Utm: Optional class to return the UTM coordinate 

(L{Utm}) or C{None}. 

 

@return: The UTM coordinate (L{Utm}) or a 

L{UtmUps5Tuple}C{(zone, hemipole, easting, 

northing, band)} if B{C{Utm}} is C{None}. 

 

@example: 

 

>>> m = Mgrs('31U', 'DQ', 448251, 11932) 

>>> u = m.toUtm() # 31 N 448251 5411932 

>>> u = m.toUtm(None) # 31 N 448251 5411932 U 

''' 

# get northing of the band bottom, extended to 

# include entirety of bottom-most 100 km square 

n = toUtm8(self.bandLatitude, _0_0, datum=self.datum).northing 

nb = int(n / _100km) * _100km 

 

e, n = self._en100k2m() 

# 100 km grid square row letters repeat every 2,000 km north; 

# add enough 2,000 km blocks to get into required band 

e += self.easting 

n += self.northing 

while n < nb: 

n += _2000km 

 

z = self.zone 

h = _hemi(self.bandLatitude) # if self.band < _N_ 

B = self.band 

r = UtmUps5Tuple(z, h, e, n, B, Error=MGRSError) if Utm is None else \ 

Utm(z, h, e, n, band=B, datum=self.datum) 

return self._xnamed(r) 

 

@property_RO 

def zone(self): 

'''Get the longitudal zone (C{int}, 1..60). 

''' 

return self._zone 

 

 

class Mgrs4Tuple(_NamedTuple): 

'''4-Tuple C{(zone, digraph, easting, northing)}, C{zone} and 

C{digraph} as C{str}, C{easting} and C{northing} in C{meter}. 

''' 

_Names_ = (_zone_, 'digraph', _easting_, _northing_) 

_Units_ = ( Str, Str, Easting, Northing) 

 

def __new__(cls, z, di, e, n, Error=MGRSError): 

if Error is not None: 

e = Easting( e, Error=Error) 

n = Northing(n, Error=Error) 

return _NamedTuple.__new__(cls, z, di, e, n) 

 

def to6Tuple(self, band, datum): 

'''Extend this L{Mgrs4Tuple} to a L{Mgrs6Tuple}. 

 

@arg band: The band to add (C{str} or C{None}). 

@arg datum: The datum to add (L{Datum} or C{None}). 

 

@return: A L{Mgrs6Tuple}C{(zone, digraph, easting, 

northing, band, datum)}. 

''' 

return self._xtend(Mgrs6Tuple, band, datum) 

 

 

class Mgrs6Tuple(_NamedTuple): # XXX only used in the line above 

'''6-Tuple C{(zone, digraph, easting, northing, band, datum)}, 

C{zone}, C{digraph} and C{band} as C{str}, C{easting} and 

C{northing} in C{meter} and C{datum} a L{Datum}. 

''' 

_Names_ = Mgrs4Tuple._Names_ + (_band_, _datum_) 

_Units_ = Mgrs4Tuple._Units_ + ( Str, _Pass) 

 

 

def parseMGRS(strMGRS, datum=Datums.WGS84, Mgrs=Mgrs, name=NN): 

'''Parse a string representing a MGRS grid reference, 

consisting of C{"zoneBand, grid, easting, northing"}. 

 

@arg strMGRS: MGRS grid reference (C{str}). 

@kwarg datum: Optional datum to use (L{Datum}). 

@kwarg Mgrs: Optional class to return the MGRS grid 

reference (L{Mgrs}) or C{None}. 

@kwarg name: Optional B{C{Mgrs}} name (C{str}). 

 

@return: The MGRS grid reference (B{L{Mgrs}}) or an 

L{Mgrs4Tuple}C{(zone, digraph, easting, northing)} 

if B{C{Mgrs}} is C{None}. 

 

@raise MGRSError: Invalid B{C{strMGRS}}. 

 

@example: 

 

>>> m = parseMGRS('31U DQ 48251 11932') 

>>> str(m) # 31U DQ 48251 11932 

>>> m = parseMGRS('31UDQ4825111932') 

>>> repr(m) # [Z:31U, G:DQ, E:48251, N:11932] 

>>> m = mgrs.parseMGRS('42SXD0970538646') 

>>> str(m) # 42S XD 09705 38646 

>>> m = mgrs.parseMGRS('42SXD9738') # Km 

>>> str(m) # 42S XD 97000 38000 

''' 

def _mg(cre, s): # return re.match groups 

m = cre.match(s) 

if not m: 

raise ValueError 

return m.groups() 

 

def _s2m(g): # e or n string to float meter 

# convert to meter if less than 5 digits 

m = g + '00000' 

return float(m[:5]) 

 

def _MGRS_(strMGRS, datum, Mgrs, name): 

m = tuple(strMGRS.replace(',', ' ').strip().split()) 

if len(m) == 1: # 01ABC1234512345' 

m = _mg(_MGRSre, m[0]) 

m = m[:2] + halfs2(m[2]) 

elif len(m) == 2: # 01ABC 1234512345' 

m = _mg(_ZBGre, m[0]) + halfs2(m[1]) 

elif len(m) == 3: # 01ABC 12345 12345' 

m = _mg(_ZBGre, m[0]) + m[1:] 

if len(m) != 4: # 01A BC 1234 12345 

raise ValueError 

e, n = map(_s2m, m[2:]) 

 

z, EN = m[0], m[1].upper() 

r = Mgrs4Tuple(z, EN, e, n) if Mgrs is None else \ 

Mgrs(z, EN, e, n, datum=datum) 

return _xnamed(r, name, force=True) 

 

return _parseX(_MGRS_, strMGRS, datum, Mgrs, name, 

strMGRS=strMGRS, Error=MGRSError) 

 

 

def toMgrs(utm, Mgrs=Mgrs, name=NN, **Mgrs_kwds): 

'''Convert a UTM coordinate to an MGRS grid reference. 

 

@arg utm: A UTM coordinate (L{Utm} or L{Etm}). 

@kwarg Mgrs: Optional class to return the MGRS grid 

reference (L{Mgrs}) or C{None}. 

@kwarg name: Optional B{C{Mgrs}} name (C{str}). 

@kwarg Mgrs_kwds: Optional, additional B{C{Mgrs}} keyword 

arguments, ignored if B{C{Mgrs=None}}. 

 

@return: The MGRS grid reference (B{L{Mgrs}}) or an 

L{Mgrs6Tuple}C{(zone, digraph, easting, northing, 

band, datum)} if B{L{Mgrs}} is C{None}. 

 

@raise TypeError: If B{C{utm}} is not L{Utm} nor L{Etm}. 

 

@raise MGRSError: Invalid B{C{utm}}. 

 

@example: 

 

>>> u = Utm(31, 'N', 448251, 5411932) 

>>> m = u.toMgrs() # 31U DQ 48251 11932 

''' 

_xinstanceof(Utm, utm=utm) # Utm, Etm 

 

e, n = utm.to2en(falsed=True) 

# truncate east-/northing to within 100 km grid square 

# XXX add rounding to nm precision? 

E, e = divmod(e, _100km) 

N, n = divmod(n, _100km) 

 

# columns in zone 1 are A-H, zone 2 J-R, zone 3 S-Z, then 

# repeating every 3rd zone (note -1 because eastings start 

# at 166e3 due to 500km false origin) 

z = utm.zone - 1 

en = (_Le100k[z % 3][int(E) - 1] + 

# rows in even zones are A-V, in odd zones are F-E 

_Ln100k[z % 2][int(N) % len(_Ln100k[0])]) 

 

if Mgrs is None: 

r = Mgrs4Tuple(utm.zone, en, e, n).to6Tuple(utm.band, utm.datum) 

else: 

kwds = _xkwds(Mgrs_kwds, band=utm.band, datum=utm.datum) 

r = Mgrs(utm.zone, en, e, n, **kwds) 

return _xnamed(r, name or utm.name) 

 

 

__all__ += _ALL_DOCS(Mgrs4Tuple, Mgrs6Tuple) 

 

# **) MIT License 

# 

# Copyright (C) 2016-2020 -- mrJean1 at Gmail -- All Rights Reserved. 

# 

# Permission is hereby granted, free of charge, to any person obtaining a 

# copy of this software and associated documentation files (the "Software"), 

# to deal in the Software without restriction, including without limitation 

# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

# and/or sell copies of the Software, and to permit persons to whom the 

# Software is furnished to do so, subject to the following conditions: 

# 

# The above copyright notice and this permission notice shall be included 

# in all copies or substantial portions of the Software. 

# 

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

# OTHER DEALINGS IN THE SOFTWARE.