{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IncidntNumCategoryDescriptDayOfWeekDateTimePdDistrictResolutionAddressXYLocationPdId
0120058272WEAPON LAWSPOSS OF PROHIBITED WEAPONFriday01/29/2016 12:00:00 AM11:00SOUTHERNARREST, BOOKED800 Block of BRYANT ST-122.40340537.775421(37.775420706711, -122.403404791479)12005827212120
1120058272WEAPON LAWSFIREARM, LOADED, IN VEHICLE, POSSESSION OR USEFriday01/29/2016 12:00:00 AM11:00SOUTHERNARREST, BOOKED800 Block of BRYANT ST-122.40340537.775421(37.775420706711, -122.403404791479)12005827212168
2141059263WARRANTSWARRANT ARRESTMonday04/25/2016 12:00:00 AM14:59BAYVIEWARREST, BOOKEDKEITH ST / SHAFTER AV-122.38885637.729981(37.7299809672996, -122.388856204292)14105926363010
3160013662NON-CRIMINALLOST PROPERTYTuesday01/05/2016 12:00:00 AM23:50TENDERLOINNONEJONES ST / OFARRELL ST-122.41297137.785788(37.7857883766888, -122.412970537591)16001366271000
4160002740NON-CRIMINALLOST PROPERTYFriday01/01/2016 12:00:00 AM00:30MISSIONNONE16TH ST / MISSION ST-122.41967237.765050(37.7650501214668, -122.419671780296)16000274071000
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NeighborhoodCount
0SOUTHERN28445
1NORTHERN20100
2MISSION19503
3CENTRAL17666
4BAYVIEW14303
5INGLESIDE11594
6TARAVAL11325
7TENDERLOIN9942
8RICHMOND8922
9PARK8699
\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 }