In [2]:
# Import dependencies
import folium
import numpy as np
import pandas as pd
# Load accident data.
accident_data = pd.read_csv('./data/NYPD_Motor_Vehicle_Collisions_sampled.csv')
In [4]:
accident_data.head()
Out[4]:
In [5]:
accident_data.describe()
Out[5]:
In [6]:
# Num rows in data.
print(accident_data.count())
In [3]:
# Map data.
# Starting coordinates to load map view.
NYC_coordinates = (40.7142700, -74.0059700)
# Create Map object.
map = folium.Map(location=NYC_coordinates,
zoom_start=12)
# Creating different tile layers
cycle_tile = folium.TileLayer(tiles = 'http://b.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png', attr='Attributed').add_to(map)
cycle_tile.layer_name = 'Cycling Route Map'
# Create a feature group for controlling multiple aspects
col_group = folium.FeatureGroup(name='Collisions').add_to(map)
injury_group = folium.FeatureGroup(name='Injuries').add_to(map)
# Plot accidents.
# Limit number of points to plot for testing.
MAX_RECORDS = 1000
collision_cluster = folium.MarkerCluster().add_to(col_group)
for row in accident_data[0:MAX_RECORDS].iterrows():
# Only plot point if lat/long is available.
if (not np.isnan(row[1]['LATITUDE']) and not np.isnan(row[1]['LONGITUDE'])):
accident_metadata = """
<ul>
<li><strong>On street</strong>: {0}</li>
<li><strong>Cross street</strong>: {1}</li>
<li><strong>Reason</strong>: {2}</li>
</ul>""".format(
str(row[1]['ON STREET NAME']), str(row[1]['CROSS STREET NAME']),
str(row[1]['CONTRIBUTING FACTOR VEHICLE 1']))
iframe = folium.element.IFrame(html=accident_metadata, width=250, height=100)
popup = folium.Popup(iframe, max_width=2650)
folium.Marker(
location = [row[1]['LATITUDE'], row[1]['LONGITUDE']],
icon = folium.Icon(color='red', icon='asterisk'),
popup=popup).add_to(collision_cluster)
# Create new cluster group for cyclist injuries
bike_cluster = folium.MarkerCluster().add_to(injury_group)
for row in accident_data[0:MAX_RECORDS].iterrows():
# Only plot point if lat/long is available.
if (not np.isnan(row[1]['LATITUDE']) and not np.isnan(row[1]['LONGITUDE']) and row[1]['NUMBER OF CYCLIST INJURED'] >= 1 or row[1]['NUMBER OF CYCLIST KILLED'] >= 1):
accident_metadata = """
<ul>
<li><strong>On street</strong>: {0}</li>
<li><strong>Cross street</strong>: {1}</li>
<li><strong>Cyclists Injured</strong>: {2}</li>
<li><strong>Cyclists Killed</strong>: {3}</li>
</ul>""".format(
str(row[1]['ON STREET NAME']), str(row[1]['CROSS STREET NAME']),
str(row[1]['NUMBER OF CYCLIST INJURED']), str(row[1]['NUMBER OF CYCLIST KILLED']))
iframe = folium.element.IFrame(html=accident_metadata, width=250, height=100)
popup = folium.Popup(iframe, max_width=2650)
folium.Marker(
location = [row[1]['LATITUDE'], row[1]['LONGITUDE']],
icon = folium.Icon(color='blue', icon='asterisk'),
popup=popup).add_to(bike_cluster)
# Create new cluster group for pedestrian injuries
ped_cluster = folium.MarkerCluster().add_to(injury_group)
for row in accident_data[0:MAX_RECORDS].iterrows():
# Only plot point if lat/long is available.
if (not np.isnan(row[1]['LATITUDE']) and not np.isnan(row[1]['LONGITUDE']) and row[1]['NUMBER OF PEDESTRIANS INJURED'] >= 1 or row[1]['NUMBER OF PEDESTRIANS KILLED'] >= 1):
accident_metadata = """
<ul>
<li><strong>On street</strong>: {0}</li>
<li><strong>Cross street</strong>: {1}</li>
<li><strong>Pedestrians Injured</strong>: {2}</li>
<li><strong>Pedestrians Killed</strong>: {3}</li>
</ul>""".format(
str(row[1]['ON STREET NAME']), str(row[1]['CROSS STREET NAME']),
str(row[1]['NUMBER OF PEDESTRIANS INJURED']), str(row[1]['NUMBER OF PEDESTRIANS KILLED']))
iframe = folium.element.IFrame(html=accident_metadata, width=250, height=100)
popup = folium.Popup(iframe, max_width=2650)
folium.Marker(
location = [row[1]['LATITUDE'], row[1]['LONGITUDE']],
icon = folium.Icon(color='green', icon='asterisk'),
popup=popup).add_to(ped_cluster)
# Create new cluster group for automobile injuries
auto_cluster = folium.MarkerCluster().add_to(injury_group)
for row in accident_data[0:MAX_RECORDS].iterrows():
# Only plot point if lat/long is available.
if (not np.isnan(row[1]['LATITUDE']) and not np.isnan(row[1]['LONGITUDE']) and row[1]['NUMBER OF MOTORIST INJURED'] >= 1 or row[1]['NUMBER OF MOTORIST KILLED'] >= 1):
accident_metadata = """
<ul>
<li><strong>On street</strong>: {0}</li>
<li><strong>Cross street</strong>: {1}</li>
<li><strong>Motorists Injured</strong>: {2}</li>
<li><strong>Motorists Killed</strong>: {3}</li>
</ul>""".format(
str(row[1]['ON STREET NAME']), str(row[1]['CROSS STREET NAME']),
str(row[1]['NUMBER OF MOTORIST INJURED']), str(row[1]['NUMBER OF MOTORIST KILLED']))
iframe = folium.element.IFrame(html=accident_metadata, width=250, height=100)
popup = folium.Popup(iframe, max_width=2650)
folium.Marker(
location = [row[1]['LATITUDE'], row[1]['LONGITUDE']],
icon = folium.Icon(color='red', icon='asterisk'),
popup=popup).add_to(auto_cluster)
# layer names for items separately
# collision_cluster.layer_name = 'Collisions'
# bike_cluster.layer_name = 'Cyclist Injuries'
# ped_cluster.layer_name = 'Pedestrian Injuries'
folium.LayerControl().add_to(map)
map
Out[3]:
In [4]:
# Save html version of map.
map.save('accidents_by_type_map.html')