Skip to content

Instantly share code, notes, and snippets.

@v0lat1le
Created October 30, 2015 00:33
Show Gist options
  • Select an option

  • Save v0lat1le/be2b12cfe098838ca49e to your computer and use it in GitHub Desktop.

Select an option

Save v0lat1le/be2b12cfe098838ca49e to your computer and use it in GitHub Desktop.
import re
from collections import namedtuple
MGRS = namedtuple('MGRS', ['zone', 'band', 'col', 'row', 'east', 'north'])
UTM = namedtuple('UTM', ['zone', 'band', 'east', 'north'])
def idx(ch):
idx = ord(ch)-ord('A')
if ch > 'O': idx -= 1 # skip 'O'
if ch > 'I': idx -= 1 # skip 'I'
return idx
def parseMGRS(mgrs_str):
fields = re.match(r"(?P<zone>[0-9][0-9]?)(?P<band>[A-Z])(?P<col>[A-Z])(?P<row>[A-Z])", mgrs_str).groupdict()
return MGRS(int(fields['zone']), idx(fields['band']), idx(fields['col']), idx(fields['row']), 0, 0)
def MGRS2UTM(mgrs):
N = ((mgrs.band-idx('N'))*8*111000)//2000000
north = N*2000000 + mgrs.row*100000 + mgrs.north
if mgrs.zone%2 == 0:
north -= idx('F')*100000
if mgrs.band < idx('N'):
north += 10000000
east = 100000*(mgrs.col - ((mgrs.zone-1)*8)%24 + 1) + mgrs.east
return UTM(mgrs.zone, mgrs.band, east, north)
mgrs = parseMGRS("32TQQ")
print mgrs
utm = MGRS2UTM(mgrs)
print utm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment