{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import folium\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# define the world map\n",
"world_map = folium.Map()\n",
"\n",
"# display world map\n",
"world_map"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# San Francisco latitude and longitude values\n",
"latitude = 37.77\n",
"longitude = -122.42\n",
"\n",
"# Create map and display it\n",
"san_map = folium.Map(location=[latitude, longitude], zoom_start=12)\n",
"\n",
"# Display the map of San Francisco\n",
"san_map"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Change tiles of map\n",
"san_map = folium.Map(location=[latitude, longitude], zoom_start=12, tiles='Stamen Toner')\n",
"san_map"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" IncidntNum | \n",
" Category | \n",
" Descript | \n",
" DayOfWeek | \n",
" Date | \n",
" Time | \n",
" PdDistrict | \n",
" Resolution | \n",
" Address | \n",
" X | \n",
" Y | \n",
" Location | \n",
" PdId | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 120058272 | \n",
" WEAPON LAWS | \n",
" POSS OF PROHIBITED WEAPON | \n",
" Friday | \n",
" 01/29/2016 12:00:00 AM | \n",
" 11:00 | \n",
" SOUTHERN | \n",
" ARREST, BOOKED | \n",
" 800 Block of BRYANT ST | \n",
" -122.403405 | \n",
" 37.775421 | \n",
" (37.775420706711, -122.403404791479) | \n",
" 12005827212120 | \n",
"
\n",
" \n",
" | 1 | \n",
" 120058272 | \n",
" WEAPON LAWS | \n",
" FIREARM, LOADED, IN VEHICLE, POSSESSION OR USE | \n",
" Friday | \n",
" 01/29/2016 12:00:00 AM | \n",
" 11:00 | \n",
" SOUTHERN | \n",
" ARREST, BOOKED | \n",
" 800 Block of BRYANT ST | \n",
" -122.403405 | \n",
" 37.775421 | \n",
" (37.775420706711, -122.403404791479) | \n",
" 12005827212168 | \n",
"
\n",
" \n",
" | 2 | \n",
" 141059263 | \n",
" WARRANTS | \n",
" WARRANT ARREST | \n",
" Monday | \n",
" 04/25/2016 12:00:00 AM | \n",
" 14:59 | \n",
" BAYVIEW | \n",
" ARREST, BOOKED | \n",
" KEITH ST / SHAFTER AV | \n",
" -122.388856 | \n",
" 37.729981 | \n",
" (37.7299809672996, -122.388856204292) | \n",
" 14105926363010 | \n",
"
\n",
" \n",
" | 3 | \n",
" 160013662 | \n",
" NON-CRIMINAL | \n",
" LOST PROPERTY | \n",
" Tuesday | \n",
" 01/05/2016 12:00:00 AM | \n",
" 23:50 | \n",
" TENDERLOIN | \n",
" NONE | \n",
" JONES ST / OFARRELL ST | \n",
" -122.412971 | \n",
" 37.785788 | \n",
" (37.7857883766888, -122.412970537591) | \n",
" 16001366271000 | \n",
"
\n",
" \n",
" | 4 | \n",
" 160002740 | \n",
" NON-CRIMINAL | \n",
" LOST PROPERTY | \n",
" Friday | \n",
" 01/01/2016 12:00:00 AM | \n",
" 00:30 | \n",
" MISSION | \n",
" NONE | \n",
" 16TH ST / MISSION ST | \n",
" -122.419672 | \n",
" 37.765050 | \n",
" (37.7650501214668, -122.419671780296) | \n",
" 16000274071000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" IncidntNum Category Descript \\\n",
"0 120058272 WEAPON LAWS POSS OF PROHIBITED WEAPON \n",
"1 120058272 WEAPON LAWS FIREARM, LOADED, IN VEHICLE, POSSESSION OR USE \n",
"2 141059263 WARRANTS WARRANT ARREST \n",
"3 160013662 NON-CRIMINAL LOST PROPERTY \n",
"4 160002740 NON-CRIMINAL LOST PROPERTY \n",
"\n",
" DayOfWeek Date Time PdDistrict Resolution \\\n",
"0 Friday 01/29/2016 12:00:00 AM 11:00 SOUTHERN ARREST, BOOKED \n",
"1 Friday 01/29/2016 12:00:00 AM 11:00 SOUTHERN ARREST, BOOKED \n",
"2 Monday 04/25/2016 12:00:00 AM 14:59 BAYVIEW ARREST, BOOKED \n",
"3 Tuesday 01/05/2016 12:00:00 AM 23:50 TENDERLOIN NONE \n",
"4 Friday 01/01/2016 12:00:00 AM 00:30 MISSION NONE \n",
"\n",
" Address X Y \\\n",
"0 800 Block of BRYANT ST -122.403405 37.775421 \n",
"1 800 Block of BRYANT ST -122.403405 37.775421 \n",
"2 KEITH ST / SHAFTER AV -122.388856 37.729981 \n",
"3 JONES ST / OFARRELL ST -122.412971 37.785788 \n",
"4 16TH ST / MISSION ST -122.419672 37.765050 \n",
"\n",
" Location PdId \n",
"0 (37.775420706711, -122.403404791479) 12005827212120 \n",
"1 (37.775420706711, -122.403404791479) 12005827212168 \n",
"2 (37.7299809672996, -122.388856204292) 14105926363010 \n",
"3 (37.7857883766888, -122.412970537591) 16001366271000 \n",
"4 (37.7650501214668, -122.419671780296) 16000274071000 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Read Dataset \n",
"cdata = pd.read_csv('https://cocl.us/sanfran_crime_dataset')\n",
"cdata.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get the first 200 crimes in the cdata\n",
"limit = 200\n",
"data = cdata.iloc[0:limit, :]\n",
"\n",
"# Instantiate a feature group for the incidents in the dataframe\n",
"incidents = folium.map.FeatureGroup()\n",
"\n",
"# Loop through the 200 crimes and add each to the incidents feature group\n",
"for lat, lng, in zip(cdata.Y, data.X):\n",
" incidents.add_child(\n",
" folium.CircleMarker(\n",
" [lat, lng],\n",
" radius=7, # define how big you want the circle markers to be\n",
" color='yellow',\n",
" fill=True,\n",
" fill_color='red',\n",
" fill_opacity=0.4\n",
" )\n",
" )\n",
"\n",
"# Add incidents to map\n",
"san_map = folium.Map(location=[latitude, longitude], zoom_start=12)\n",
"san_map.add_child(incidents)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# add pop-up text to each marker on the map\n",
"latitudes = list(data.Y)\n",
"longitudes = list(data.X)\n",
"labels = list(data.Category)\n",
"\n",
"for lat, lng, label in zip(latitudes, longitudes, labels):\n",
" folium.Marker([lat, lng], popup=label).add_to(san_map) \n",
" \n",
"# add incidents to map\n",
"san_map.add_child(incidents)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from folium import plugins\n",
"\n",
"# let's start again with a clean copy of the map of San Francisco\n",
"san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)\n",
"\n",
"# instantiate a mark cluster object for the incidents in the dataframe\n",
"incidents = plugins.MarkerCluster().add_to(san_map)\n",
"\n",
"# loop through the dataframe and add each data point to the mark cluster\n",
"for lat, lng, label, in zip(data.Y, data.X, cdata.Category):\n",
" folium.Marker(\n",
" location=[lat, lng],\n",
" icon=None,\n",
" popup=label,\n",
" ).add_to(incidents)\n",
"\n",
"# add incidents to map\n",
"san_map.add_child(incidents)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import json\n",
"import requests\n",
"\n",
"url = 'https://cocl.us/sanfran_geojson'\n",
"san_geo = f'{url}'\n",
"san_map = folium.Map(location=[37.77, -122.4], zoom_start=12)\n",
"folium.GeoJson(\n",
" san_geo,\n",
" style_function=lambda feature: {\n",
" 'fillColor': '#ffff00',\n",
" 'color': 'black',\n",
" 'weight': 2,\n",
" 'dashArray': '5, 5'\n",
" }\n",
").add_to(san_map)\n",
"\n",
"#display map\n",
"san_map"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Neighborhood | \n",
" Count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" SOUTHERN | \n",
" 28445 | \n",
"
\n",
" \n",
" | 1 | \n",
" NORTHERN | \n",
" 20100 | \n",
"
\n",
" \n",
" | 2 | \n",
" MISSION | \n",
" 19503 | \n",
"
\n",
" \n",
" | 3 | \n",
" CENTRAL | \n",
" 17666 | \n",
"
\n",
" \n",
" | 4 | \n",
" BAYVIEW | \n",
" 14303 | \n",
"
\n",
" \n",
" | 5 | \n",
" INGLESIDE | \n",
" 11594 | \n",
"
\n",
" \n",
" | 6 | \n",
" TARAVAL | \n",
" 11325 | \n",
"
\n",
" \n",
" | 7 | \n",
" TENDERLOIN | \n",
" 9942 | \n",
"
\n",
" \n",
" | 8 | \n",
" RICHMOND | \n",
" 8922 | \n",
"
\n",
" \n",
" | 9 | \n",
" PARK | \n",
" 8699 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Neighborhood Count\n",
"0 SOUTHERN 28445\n",
"1 NORTHERN 20100\n",
"2 MISSION 19503\n",
"3 CENTRAL 17666\n",
"4 BAYVIEW 14303\n",
"5 INGLESIDE 11594\n",
"6 TARAVAL 11325\n",
"7 TENDERLOIN 9942\n",
"8 RICHMOND 8922\n",
"9 PARK 8699"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Count crime numbers in each neighborhood\n",
"disdata = pd.DataFrame(cdata['PdDistrict'].value_counts())\n",
"disdata.reset_index(inplace=True)\n",
"disdata.rename(columns={'index':'Neighborhood','PdDistrict':'Count'},inplace=True)\n",
"disdata"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"san_map = folium.Map(location=[37.77, -122.4], zoom_start=12)\n",
"\n",
"# Create Choropleth map\n",
"folium.Choropleth(\n",
" geo_data=san_geo,\n",
" data=disdata,\n",
" columns=['Neighborhood','Count'],\n",
" key_on='feature.properties.DISTRICT',\n",
" #fill_color='red',\n",
" fill_color='YlOrRd',\n",
" fill_opacity=0.7,\n",
" line_opacity=0.2,\n",
" highlight=True,\n",
" legend_name='Crime Counts in San Francisco'\n",
").add_to(san_map)\n",
"\n",
"san_map"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"san_map = folium.Map(location=[37.77, -122.4], zoom_start=12)\n",
"\n",
"# Create Choropleth map\n",
"folium.Choropleth(\n",
" geo_data=san_geo,\n",
" data=disdata,\n",
" columns=['Neighborhood','Count'],\n",
" key_on='feature.properties.DISTRICT',\n",
" #fill_color='red',\n",
" fill_color='YlGn',\n",
" fill_opacity=0.7,\n",
" line_opacity=0.2,\n",
" highlight=True,\n",
" legend_name='Crime Counts in San Francisco'\n",
").add_to(san_map)\n",
"\n",
"san_map"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from folium.plugins import HeatMap\n",
"\n",
"# let's start again with a clean copy of the map of San Francisco\n",
"san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)\n",
"\n",
"# Convert data format\n",
"heatdata = data[['Y','X']].values.tolist()\n",
"\n",
"# add incidents to map\n",
"HeatMap(heatdata).add_to(san_map)\n",
"\n",
"san_map"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}