Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created November 11, 2014 19:21
Show Gist options
  • Save TimSC/0cb631068b2c68d0f803 to your computer and use it in GitHub Desktop.
Save TimSC/0cb631068b2c68d0f803 to your computer and use it in GitHub Desktop.
Draw OS grid squares in OSM format
#!/usr/bin/env python
#Draw OS grid squares in OSM format
from ostn02python.OSGB import *
from ostn02python.OSTN02 import *
pointStore = []
vertLines = {}
out = file("grid.osm", "wt")
out.write("<?xml version='1.0' encoding='UTF-8'?>\n")
out.write("<osm version='0.6' upload='true' generator='py'>\n")
for e in range(0, 700000, 100000):
vertLines[e] = []
for n in range(0, 1300000, 10000):
try:
(x,y,h) = OSGB36_to_ETRS89 (e, n)
except:
continue
#print "OS X (Eastings) "+str(e)
#print "OS Y (Northings) "+str(n)
#print "Using the OSGB36_to_ETRS89 conversion gives us the grid position:"
#print str(x) + "," + str(y) + "," + str(h)
(gla, glo) = grid_to_ll(x, y)
#print "The grid position converts to ETRS89 lat,lon (using grid_to_ll) of:"
#print str((gla, glo))
try:
ind = pointStore.index((gla, glo))
except ValueError:
pointStore.append((gla, glo))
ind = len(pointStore)-1
vertLines[e].append(ind)
for e in vertLines:
print e, vertLines[e]
horiLines = {}
for n in range(0, 1300000, 100000):
horiLines[n] = []
for e in range(0, 700000, 10000):
try:
(x,y,h) = OSGB36_to_ETRS89 (e, n)
except:
continue
#print "OS X (Eastings) "+str(e)
#print "OS Y (Northings) "+str(n)
#print "Using the OSGB36_to_ETRS89 conversion gives us the grid position:"
#print str(x) + "," + str(y) + "," + str(h)
(gla, glo) = grid_to_ll(x, y)
#print "The grid position converts to ETRS89 lat,lon (using grid_to_ll) of:"
#print str((gla, glo))
try:
ind = pointStore.index((gla, glo))
except ValueError:
pointStore.append((gla, glo))
ind = len(pointStore)-1
horiLines[n].append(ind)
for n in horiLines:
print n, horiLines[n]
for i, pos in enumerate(pointStore):
out.write("<node id='{0}' action='modify' visible='true' lat='{1}' lon='{2}' />\n".format(-i-1, pos[0], pos[1]))
liCount = 1
for e in vertLines:
li = vertLines[e]
out.write("<way id='{0}' action='modify' visible='true'>\n".format(-liCount))
for pid in li:
out.write("<nd ref='{0}' />\n".format(-pid-1))
out.write("</way>\n")
liCount += 1
for n in horiLines:
li = horiLines[n]
out.write("<way id='{0}' action='modify' visible='true'>\n".format(-liCount))
for pid in li:
out.write("<nd ref='{0}' />\n".format(-pid-1))
out.write("</way>\n")
liCount += 1
out.write("</osm>\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment