-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
96 lines (81 loc) · 2.88 KB
/
run.py
File metadata and controls
96 lines (81 loc) · 2.88 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
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
import sys
import urllib2
import json
import polyline
from xml.sax.saxutils import escape
from multiprocessing import Pool
base_otp = 'http://planner.plannerstack.org/otp-rest-servlet/ws/plan?time=1%3A09am&date=07-12-2014&mode=WALK&maxWalkDistance=750&arriveBy=false&'
if len(sys.argv) != 2:
print "Usage: %s test.csv" % (sys.argv[0])
sys.exit(-1)
f = open(sys.argv[1])
hit = set([])
nothit = set([])
def query(line):
toprint = []
name, x1, y1, x2, y2 = line[:-1].split(',')
output = json.loads(urllib2.urlopen(base_otp+'fromPlace='+y1+'%2C'+x1+'&toPlace='+y2+'%2C'+x2).read())
if output['plan'] is not None:
for itin in output['plan']['itineraries']:
for leg in itin['legs']:
toprint.append(""" <Placemark>
<name>%s</name>
<description>%s</description>
<styleUrl>#GreenLine</styleUrl>
<LineString>
<coordinates>""" % (escape(name), leg['legGeometry']['length']))
toprint.append(' '.join([','.join([str(p[0]), str(p[1])]) for p in polyline.decode(leg['legGeometry']['points'])]))
toprint.append(""" </coordinates>
</LineString>
</Placemark>""")
hit.add(x1+','+y1)
hit.add(x2+','+y2)
else:
if (x2+','+y2 not in hit and x2+','+y2 not in nothit) or (x1+','+y1 not in hit and x1+','+y1 not in nothit):
toprint.append(""" <Placemark>
<name>%s</name>
<description>%s</description>
<styleUrl>#redLine</styleUrl>""" % (escape(name), "No result"))
if True:
if (x2+','+y2 not in hit and x2+','+y2 not in nothit):
toprint.append(""" <Point>
<coordinates>
%s,%s
</coordinates>
</Point>""" % (x2, y2))
nothit.add(x2+','+y2)
else:
toprint.append(""" <Point>
<coordinates>
%s,%s
</coordinates>
</Point>""" % (x1, y1))
nothit.add(x1+','+y1)
else:
toprint.append(""" <LineString>
<coordinates>""")
toprint.append(' '.join([','.join([str(p[0]), str(p[1])]) for p in [(x1, y1), (x2, y2)]]))
toprint.append(""" </coordinates>
</LineString>""")
toprint.append(""" </Placemark>""")
return '\n'.join(toprint)
print """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Concat</name>"""
""" <Style id="GreenLine">
<LineStyle>
<color>007fff00</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="RedLine">
<LineStyle>
<color>7f000000</color>
<width>1</width>
</LineStyle>
</Style>"""
p = Pool(8)
print '\n'.join(p.map(query, [line for line in f]))
print """ </Document>
</kml>"""