/
folium_test.py
175 lines (138 loc) · 6.48 KB
/
folium_test.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
# see: https://fastkml.readthedocs.org/en/latest/usage_guide.html#read-a-kml-file
# and: https://github.com/python-visualization/folium
from fastkml import kml
import pandas as pd
import folium
UNCLASSIFIED = '#808080'
ALL_ABILITIES = '#00FF00'
INTERMEDIATE = '#FFFF00'
DIFFICULT = '#FF0000'
difficulties = { 'u' : UNCLASSIFIED,
'e' : ALL_ABILITIES,
'i' : INTERMEDIATE,
'd' : DIFFICULT }
def read_kml(filename):
f = open(filename, 'r')
return f.read()
def process_kml(kml):
the_map = kml.features().next()
print "Map: %s" % (the_map.name)
layers = the_map.features()
segment_records = { 'name' : [],
'description' : [],
'polyline': [],
'district' : [],
'difficulty' : [],
'difficulty_code' : []
}
for layer in layers:
print("Layer: %s" % (layer.name))
try:
segments = layer.features()
except:
continue
for segment in segments:
print "Segment: %s" % (segment.name)
try:
polyline = [(g.y, g.x) for g in segment.geometry.geoms]
segment_records['name'].append(segment.name)
segment_records['description'].append(segment.description)
segment_records['polyline'].append(polyline)
segment_records['district'].append(layer.name)
segment_records['difficulty'].append(UNCLASSIFIED)
segment_records['difficulty_code'].append('u')
# if 'Multi use Trail' == segment.description or \
# 'Enhanced Street' == segment.description:
# segment_records['difficulty'].append(ALL_ABILITIES)
# segment_records['difficulty_code'].append('e')
# elif 'In street major separation' == segment.description:
# segment_records['difficulty'].append(INTERMEDIATE)
# segment_records['difficulty_code'].append('i')
# elif 'Sharrows' == segment.description or \
# 'In street minor separation':
# segment_records['difficulty'].append(DIFFICULT)
# segment_records['difficulty_code'].append('d')
# else:
# segment_records['difficulty'].append(UNCLASSIFIED)
# segment_records['difficulty_code'].append('u')
except:
print "no geometry"
segment_df = pd.DataFrame(segment_records)
return segment_df
def draw_lines(folium_map, segment_df):
for (index, polyline, difficulty) in segment_df[['polyline', 'difficulty']].itertuples():
folium_map.line(polyline, line_color=difficulty, line_weight=5)
def segments_to_map(coords, segment_df, included=['e', 'i'], filename='osm.html'):
map_osm = folium.Map(location=coords,tiles='Stamen Toner')
# Copy this by hand to greenways_edited.csv, edit in Open Office, fill in the difficulty codes.
# Then re-run the script.
segment_df.to_csv('greenways.csv',
columns=['name', 'difficulty_code', 'district', 'difficulty', 'description', 'polyline'])
plot_df = segment_df.loc[segment_df['difficulty_code'].isin(included)]
draw_lines(map_osm, plot_df)
map_osm.create_map(path=filename)
def kmls_to_segments(kml_files):
first = True
for kml_file in kml_files:
k = kml.KML()
k.from_string(read_kml(kml_file))
if first:
segment_df = process_kml(k)
first = False
else:
segment_df = segment_df.append(process_kml(k)).reset_index(drop=True)
return segment_df
def bike_share_docks(folium_map, csv_file):
dock_df = pd.read_csv(csv_file)
for (_, lattitude, longitude, name) in dock_df[['lat', 'long', 'name']].itertuples():
folium_map.simple_marker([lattitude, longitude], popup=name)
def csv_to_map(coords, segment_df, csv_file, output_html):
edited_segment_df = pd.read_csv(csv_file, "\t")
edited_segment_df['difficulty'] = edited_segment_df['difficulty_code'].map(lambda d: difficulties[d.lower()])
segment_df = segment_df.reindex()
edited_segment_df[['polyline']] = segment_df[['polyline']]
map_osm = folium.Map(location=coords, tiles='Stamen Toner')
draw_lines(map_osm, edited_segment_df)
bike_share_docks(map_osm, '2015_station_data.csv')
map_osm.create_map(path=output_html)
if __name__ == '__main__':
coords = [47.6131746,-122.4878834] # Seattle
# coords = [45.5236, -122.6750] # Portland
# from http://seattlegreenways.org/neighborhoods/
# Download the KML for the maps
# Copy the .kmz file to kmz/
# unzip <neighborhood>.kmz; mv doc.kml <neighborhood>.kml
# eastlake and montlake weren't found on the page
kml_files = ['kmz/bmp/bmp_toplayer.kml']
kml_files2 = ['kmz/beacon_hill.kml',
'kmz/central_seattle.kml',
'kmz/west_seattle.kml',
'kmz/rainier_valley.kml',
'kmz/ballard.kml',
'kmz/fremont.kml',
'kmz/queen_anne.kml',
'kmz/wallingford.kml',
'kmz/green_lake.kml',
'kmz/u_district.kml',
'kmz/maple_leaf.kml',
'kmz/ne_seattle.kml',
'kmz/montlake.kml',
'kmz/lake_city.kml',
'kmz/timf_meadowbrook.kml',
# from http://www.seattleoutdoorsinfo.com/hiking-and-biking/seattle-biking/seattle-bike-trails
'kmz/trails/alki_trail.kml',
'kmz/trails/green_river_interurban.kml',
'kmz/trails/burke_gilman.kml',
'kmz/trails/sodo_trail.kml',
'kmz/trails/west_seattle_bridge.kml',
'kmz/trails/duwamish_trail.kml',
'kmz/trails/cedar_lake.kml',
'kmz/trails/lake_wilderness.kml',
'kmz/trails/soos_creek.kml',
'kmz/trails/i_90_1.kml',
'kmz/trails/i_90_2.kml',
'kmz/trails/interurban_north_1.kml',
'kmz/trails/interurban_north_2.kml']
segments_df = kmls_to_segments(kml_files)
segments_to_map(coords, segments_df)
csv_to_map(coords, segments_df, 'greenways_edited.csv', 'osm2.html')