-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathosm_render_tool.py
More file actions
71 lines (58 loc) · 2.16 KB
/
osm_render_tool.py
File metadata and controls
71 lines (58 loc) · 2.16 KB
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
#!/usr/bin/env python2
from mapnik import *
import sys
from locationSearch import str2coords
from urllib2 import urlopen, URLError, HTTPError
import time
from PIL import Image
def parseInput():
## To run search, run python osm_render_tool.py search "Boston Copley Square"
if 'search' in sys.argv:
searchStr = sys.argv[sys.argv.index('search') + 1]
try:
minLat, maxLat, minLon, maxLon = str2coords(searchStr)
return minLat, maxLat, minLon, maxLon
except:
sys.exit("Could not find '" + searchStr + "'. Try rephrasing or using coordinate input: \npython osm_render_tool.py coords 'minLat, maxLat, minLon, maxLon'")
## To render a map with user-defined lat/lon edges
elif 'coords' in sys.argv:
coords = sys.argv[sys.argv.index('coords') + 1].split(',')
if len(coords) == 4:
minLat = float(coords[0])
maxLat = float(coords[1])
minLon = float(coords[2])
maxLon = float(coords[3])
return minLat, maxLat, minLon, maxLon
else:
sys.exit("Error: Incorrect number of coordinates. \nFormat is: python osm_render_tool.py coords 'minLon, maxLat, maxLon, minLat'")
else:
sys.exit("Error: No command recognized. \nUsage: python osm_render_tool.py search 'query' OR python osm_render_tool.py coords 'minLat, maxLat, minLon, maxLon'")
def downloadMaps(minLat, maxLat, minLon, maxLon):
url = 'http://overpass.osm.rambler.ru/cgi/xapi_meta?*[bbox=' + str(minLon) + ',' + str(minLat) + ',' + str(maxLon) + ',' + str(maxLat) + ']'
# Download map from server
try:
f = urlopen(url)
print "Downloading " + url
# Open our local file for writing
with open(os.path.basename('temp_map.osm'), "wb") as map_file:
map_file.write(f.read())
#handle errors
except HTTPError, e:
print "HTTP Error:", e.code, url
except URLError, e:
print "URL Error:", e.reason, url
return 0
minLat, maxLat, minLon, maxLon = parseInput()
downloadMaps(minLat,maxLat,minLon,maxLon)
print "Rendering Map from XML"
mapfile = 'style.xml'
map_output = 'map.png'
m = Map(4*1024,4*1024)
load_map(m, mapfile)
bbox = (Envelope(minLon, maxLat, maxLon, minLat))
m.zoom_to_box(bbox)
print "Scale = " , m.scale()
render_to_file(m, map_output)
im = Image.open('map.png')
im.format = "PNG"
im.show()