-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpx2png.py
executable file
·422 lines (341 loc) · 13.2 KB
/
gpx2png.py
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
© 2015 John Drinkwater <[email protected]>
http://johndrinkwater.name/code/gpx2png/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import Image, ImageDraw
import math, os, sys
import urllib
import zipfile
from xml.dom.minidom import parse, parseString
from optparse import OptionParser
# defaults
verbose = False
# need to include CC notice if we use tiles
cnotice = "CC BY-SA OpenStreetMap"
# variables
__version__ = 0.50
# XXX we are just using defaults now
# Static methods for tile maths
class Tile:
# Returns an OSM tile coordinate for the lat, long provided
@staticmethod
def getNumber( lat, long, zoom ):
# Code from OSM
latrad = math.radians(lat)
n = 2.0 ** zoom
xtile = int((long + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(latrad) + (1 / math.cos(latrad))) / math.pi) / 2.0 * n)
return (xtile, ytile)
# Returns a lat, long for the provided OSM tile coordinate
@staticmethod
def getCoords( xtile, ytile, zoom ):
# Code from OSM
n = 2.0 ** zoom
long = xtile / n * 360.0 - 180.0
latrad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat = math.degrees(latrad)
return(lat, long)
# Return a URL for the tile at the tileserver
@staticmethod
def getTileURL( tileserver, tilex, tiley, zoom ):
return '/'.join( [tileserver, zoom, str(tilex), str(tiley)] ) + '.png'
# returns tile bounding box for the points at this zoom level
@staticmethod
def calculateTiles( bounds, zoom = 10 ):
global verbose
if verbose:
print 'Track.calculateTiles()'
tilexmin = tileymin = 200000
tilexmax = tileymax = 0
[tilexmin, tileymin] = Tile.getNumber( bounds[0][0], bounds[0][1], zoom )
[tilexmax, tileymax] = Tile.getNumber( bounds[1][0], bounds[1][1], zoom )
return {'x': { 'min':tilexmin, 'max':tilexmax , 'count': tilexmax - tilexmin +1 },
'y': { 'min':tileymin, 'max':tileymax , 'count': tileymax - tileymin +1 },
'zoom': zoom }
# returns tile bounding box that is automatically scaled to a correct zoom level.
# The BB is +1 in all directions so we can trim the image later ()
@staticmethod
def calculateTilesAuto( bounds, size ):
global verbose
if verbose:
print 'Track.calculateTilesAuto()'
zoomdefault = 16
# get the default scale tiles
tiles = Tile.calculateTiles( bounds, zoomdefault )
while ( (tiles['x']['count']) * (tiles['y']['count']) >= ( size * size) ):
zoomdefault -= 1
tiles['x']['count'] >>= 1
tiles['y']['count'] >>= 1
# get the re-scaled tiles
return Tile.calculateTiles( bounds, zoomdefault )
@staticmethod
def getPixelForCoord( point, bounds, imagesize ):
return (int((bounds[0][1] - point[1] ) / bounds[2][1] * imagesize[0]) ,
int((bounds[0][0] - point[0] ) / bounds[2][0] * imagesize[1]))
# TODO fetch more bordering tiles than we need, so we can better fit our image!
@staticmethod
def populateBackground( server, cachelocation, tiles, image ):
global verbose
rootx = tiles['x']['min']
rooty = tiles['y']['min']
zoom = str(tiles['zoom'])
if not os.path.isdir(cachelocation):
os.makedirs(cachelocation)
for x in range(tiles['x']['min'],tiles['x']['min'] + tiles['x']['count'] + 1):
for y in range(tiles['y']['min'],tiles['y']['min'] + tiles['y']['count'] + 1):
fromx = abs(rootx - x)
fromy = abs(rooty - y)
temptilename = '-'.join( [zoom, str(x), str(y) ] ) + '.png'
temptilename = os.path.join(cachelocation, temptilename)
# TODO thread this?
# TODO also support it failing
if not os.path.isfile( temptilename ):
if verbose:
print 'Fetching tile' , x, '×', y, '…'
urllib.urlretrieve( Tile.getTileURL( server, x, y, zoom ), temptilename )
tile = Image.open( temptilename )
image.paste( tile, (256*fromx, 256*fromy ))
return image
# GPX helper class, for singular files
class GPX:
points = []
pointsbounds = [(),()]
tiles = []
tilesbounds = [(),(), ()]
options = {
'size': 2, # Max tile w×h for output image
'border': 20, # TODO distance from edge of image to nearest path?
'background': True, # Use OSM tiles to flesh the background out
'antialiased': True,
'linecolour': 'black',
'linewidth': 1,
'filename': '', # Default output filename if not provided
'renderer': 'mapnik', # OSM server to use
'cache': 'cache', # Default cache location
'notice': 'normal'
}
def __init__( self ):
global verbose
if verbose:
print 'GPX()'
def setOptions( self, opt ):
self.options.update(opt)
# Push the selected tile server into options
tileservers = { 'mapnik': 'http://tile.openstreetmap.org',
'osmarender': 'http://tah.openstreetmap.org/Tiles/tile/',
'cyclemap': 'http://andy.sandbox.cloudmade.com/tiles/cycle/',
}
tileserver = { 'tileserver' : tileservers.get( self.options.get('renderer') ) }
self.options.update(tileserver)
global verbose
if verbose:
print 'GPX.setOptions(', self.options, ')'
def load( self, dom ):
global verbose
if verbose:
print 'GPX.load()'
# we're going to be ignorant of anything but trkpt for now
# TODO support waypoints, track segments
trackPoints = dom.getElementsByTagName('trkpt')
self.points = map( lambda x: [float(x.getAttribute('lat')), float(x.getAttribute('lon'))], trackPoints)
self.computeBounds()
def loadFromFile( self, file ):
global verbose
if verbose:
print 'loadFromFile(', file, ')'
self.trackname = file
dom = parse(file)
self.load(dom)
def loadFromString( self, string ):
global verbose
if verbose:
print 'loadFromString()'
dom = parseString(string)
self.load(dom)
# calculate lat/long bounds of path
# calculate tile area, and produce tile bounds
def computeBounds( self ):
global verbose
if verbose:
print 'GPX.computeBounds()'
latmin = longmin = 200000
latmax = longmax = -200000
for point in self.points:
latmin = min(point[0], latmin)
latmax = max(point[0], latmax)
longmin = min(point[1], longmin)
longmax = max(point[1], longmax)
self.pointsbounds = [(latmax, longmin), (latmin, longmax)]
self.tiles = Tile.calculateTilesAuto( self.pointsbounds, self.options.get('size') )
self.tilesbounds[0] = Tile.getCoords( self.tiles['x']['min'], self.tiles['y']['min'], self.tiles['zoom'] )
# because tile coords are from top left
self.tilesbounds[1] = Tile.getCoords( self.tiles['x']['max']+1, self.tiles['y']['max']+1, self.tiles['zoom'] )
self.tilesbounds[2] = ( self.tilesbounds[0][0] - self.tilesbounds[1][0],
self.tilesbounds[0][1] - self.tilesbounds[1][1] )
def drawTrack( self, filename = '' ):
global verbose
if verbose:
print 'GPX.drawTrack()'
if filename == '' or filename == None:
filename = self.options.get('filename')
if filename == '' or filename == None:
trackFile, trackType = os.path.splitext( self.trackname )
filename = trackFile + '.png'
imagesize = ( self.tiles['x']['count'] * 256, self.tiles['y']['count'] * 256 )
image = Image.new("RGB", imagesize, '#ffffff')
# If user wants OSM tile background, do it
# TODO without OSM tiles, our current code wont crop the track well
if self.options.get('background'):
cachelocation = os.path.join('.', self.options.get('cache'), self.options.get('renderer'))
image = Tile.populateBackground(self.options.get('tileserver'), cachelocation, self.tiles, image)
# compute pixel locations
pointlist = map( lambda x: Tile.getPixelForCoord(x, self.tilesbounds, imagesize), self.points)
# TODO give user option to style
# XXX Supersample our line to make it smarter
if self.options.get('antialiased'):
newsize = (imagesize[0]*4, imagesize[1]*4)
background = image.resize( newsize )
draw = ImageDraw.ImageDraw(background)
pointlist = map( lambda x: Tile.getPixelForCoord(x, self.tilesbounds, newsize), self.points)
draw.line(pointlist, fill=self.options.get('linecolour'), width=self.options.get('linewidth')*4)
image = background.resize( imagesize, Image.ANTIALIAS )
else:
draw = ImageDraw.Draw(image)
pointlist = map( lambda x: Tile.getPixelForCoord(x, self.tilesbounds, imagesize), self.points)
draw.line(pointlist, fill=self.options.get('linecolour'), width=self.options.get('linewidth'))
# Attempt to intelligently trim the image if its over
# TODO give user a gutter option
# TODO give user a scale option
# TODO move to function
size = self.options.get('size')
if size*size < self.tiles['x']['count']*self.tiles['y']['count']:
path = [ Tile.getPixelForCoord( self.pointsbounds[0], self.tilesbounds, imagesize),
Tile.getPixelForCoord( self.pointsbounds[1], self.tilesbounds, imagesize) ]
imagebox = [ [0,0], list(imagesize) ]
# so here we have a bounding box for the path, can we trim edges of image?
if imagesize[0] > size * 256:
# TODO assumption is, we can trim a tile, might need 2 × in future
if path[1][0] - path [0][0] < imagesize[0] - 256:
# We can trim
centrex = (path[1][0] - path [0][0])/2 + path[0][0]
halfwidth = ((imagesize[0] - 256) / 2)
imagebox[0][0] = centrex - halfwidth
imagebox[1][0] = centrex + halfwidth
if imagesize[1] > size * 256:
# TODO same as above
if path[1][1] - path [0][1] < imagesize[1] - 256:
centrey = (path[1][1] - path [0][1])/2 + path[0][1]
halfwidth = ((imagesize[1] - 256) / 2)
imagebox[0][1] = centrey - halfwidth
imagebox[1][1] = centrey + halfwidth
imagebox = reduce(lambda x,y: x+y,imagebox)
image = image.crop( imagebox )
#trim = int(256/2)
#image = image.crop( tuple( [trim, trim] + map( lambda x: x-trim, image.size) ) )
# Only draw if OSM background used.
if self.options.get('background'):
# Draw CC licence image
ccimage = 'cc-by-sa.' + self.options.get('notice') + '.png'
# TODO fail if image is missing
cclogo = Image.open(ccimage)
cclocation = {
'small': (85,20), # small 80 × 15
'normal': (93,36), # normal 88 × 31
}.get( self.options.get('notice'), (85,20) )
cclocation = (image.size[0] - cclocation[0], image.size[1] - cclocation[1] )
image.paste(cclogo, cclocation, cclogo)
# Draw OSM logo
osmlogo = Image.open('osm.png')
osmlogosize = {
'small': 16, # small 80 × 15
'normal': 32, # normal 88 × 31
}.get( self.options.get('notice'), 32 )
osmlogo = osmlogo.resize( (osmlogosize,osmlogosize), Image.ANTIALIAS)
osmlocation = (cclocation[0] - osmlogosize - 5, cclocation[1])
image.paste(osmlogo, osmlocation, osmlogo)
# write file
image.save(filename, "PNG")
class KML(GPX):
def load( self, dom ):
global verbose
if verbose:
print 'KML.load()'
# we're going to be ignorant of anything but gx:coord for now
# TODO support waypoints, track segments
trackPoints = dom.getElementsByTagName('gx:coord')
trackPoints = [ x.firstChild.data.split() for x in trackPoints ]
self.points = map( lambda x: [float(x[1]), float(x[0])], trackPoints)
self.computeBounds()
class KMZ(KML):
def loadFromFile( self, file ):
global verbose
if verbose:
print 'KMX.loadFromFile(', file, ')'
if not zipfile.is_zipfile( file ):
print 'File is not a valid ZIP'
sys.exit(-1)
self.trackname = file
with zipfile.ZipFile( file, 'r' ) as kml:
file_contents = kml.read( 'doc.kml' )
dom = parseString(file_contents)
self.load(dom)
def loadFromFile( trackpath ):
trackFile, trackType = os.path.splitext( trackpath )
# since OS do not love mime types :'( we do the stupid thing, test on extension!!!
if trackType == '.gpx':
if verbose:
print 'Selected GPX parser'
track = GPX()
elif trackType == '.kml':
if verbose:
print 'Selected KML parser'
track = KML()
elif trackType == '.kmz':
if verbose:
print 'Selected KMZ parser'
track = KMZ()
else:
print 'Invalid filetype provided: ', track
sys.exit(-1)
track.loadFromFile( trackpath )
return track
if __name__ == "__main__":
# Now support CLI arguments!
parser = OptionParser(usage="usage: gpx2png.py [options] file.gpx")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="output progress messages to stdout")
parser.add_option("-o", "--output",
action="store", dest="filename", default='',
help="filename to write the track image to")
parser.add_option("-b", "--background",
action="store_false", dest="background", default=True,
help="disable output of OSM tile background")
(options, args) = parser.parse_args()
verbose = options.verbose
if len(args) == 0:
parser.print_help()
sys.exit(-1)
if len(args) == 1:
track = loadFromFile( args[0] )
track.setOptions( options.__dict__ )
track.drawTrack()
else:
# TODO Support more than one file in the same image
for path in args:
track = loadFromFile( path )
track.setOptions( options.__dict__ )
# atm, with multiple, we just let each one output once
track.drawTrack()