Archived
Add logger statements and first plays with accident data in notebook.
This commit is contained in:
1 parent
b9ee59e4ae
commit
5b41db5e7e
2 files changed
+269
-3
No files matched your search
@@ -0,0 +1,266 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:30.748332084Z",
|
||||
"start_time": "2024-01-03T12:40:30.269262153Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Handle imports\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"import geopandas as gpd\n",
|
||||
"import numpy as np\n",
|
||||
"import os \n",
|
||||
"import csv\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fb_df = pd.read_csv('FootBikeCount.csv', low_memory=False)\n",
|
||||
"miv_df = pd.read_csv('MivCount.csv', low_memory=False)\n",
|
||||
"acc_gdf = gpd.read_file('Accidents.geojson')\n",
|
||||
"sig_speeds_gdf = gpd.read_file('signaled_speeds.geojson.geojson')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:51.041189781Z",
|
||||
"start_time": "2024-01-03T12:40:30.749782023Z"
|
||||
}
|
||||
},
|
||||
"id": "ef8bfea977c881d3"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"map_gdf = gpd.read_file('map.geojson')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:51.050765535Z",
|
||||
"start_time": "2024-01-03T12:40:51.048395044Z"
|
||||
}
|
||||
},
|
||||
"id": "eeed8aa197302f36"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": " AccidentUID AccidentYear AccidentMonth \\\n0 A2D2677533867004E0430A865E337004 2011 1 \n1 9FD6441F802C20A6E0430A865E3320A6 2011 1 \n2 9FDA0DC4856A6094E0430A865E336094 2011 1 \n3 A3B66E42396E6000E0430A865E336000 2011 1 \n4 9FDA0DBE8CCE9096E0430A865E339096 2011 1 \n\n AccidentWeekDay_en AccidentHour NKoord EKoord \\\n0 Saturday 0 1245194 2684605 \n1 Saturday 1 1246980 2682382 \n2 Saturday 2 1247749 2682791 \n3 Saturday 2 1247102 2681199 \n4 Saturday 3 1250690 2682479 \n\n AccidentType_en AccidentType \\\n0 Accident with skidding or self-accident at0 \n1 Accident with skidding or self-accident at0 \n2 Accident with skidding or self-accident at0 \n3 Accident when crossing the lane(s) at5 \n4 Accident with skidding or self-accident at0 \n\n AccidentSeverityCategory AccidentInvolvingPedestrian \\\n0 as4 True \n1 as3 True \n2 as4 True \n3 as3 True \n4 as4 True \n\n AccidentInvolvingBicycle AccidentInvolvingMotorcycle RoadType RoadType_en \\\n0 True True rt433 Minor road \n1 True True rt433 Minor road \n2 True True rt439 Other \n3 True True rt433 Minor road \n4 True True rt433 Minor road \n\n geometry \n0 POINT Z (8.55841 47.35217 0.00000) \n1 POINT Z (8.52932 47.36851 0.00000) \n2 POINT Z (8.53488 47.37538 0.00000) \n3 POINT Z (8.51368 47.36976 0.00000) \n4 POINT Z (8.53129 47.40187 0.00000) ",
|
||||
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>AccidentUID</th>\n <th>AccidentYear</th>\n <th>AccidentMonth</th>\n <th>AccidentWeekDay_en</th>\n <th>AccidentHour</th>\n <th>NKoord</th>\n <th>EKoord</th>\n <th>AccidentType_en</th>\n <th>AccidentType</th>\n <th>AccidentSeverityCategory</th>\n <th>AccidentInvolvingPedestrian</th>\n <th>AccidentInvolvingBicycle</th>\n <th>AccidentInvolvingMotorcycle</th>\n <th>RoadType</th>\n <th>RoadType_en</th>\n <th>geometry</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>A2D2677533867004E0430A865E337004</td>\n <td>2011</td>\n <td>1</td>\n <td>Saturday</td>\n <td>0</td>\n <td>1245194</td>\n <td>2684605</td>\n <td>Accident with skidding or self-accident</td>\n <td>at0</td>\n <td>as4</td>\n <td>True</td>\n <td>True</td>\n <td>True</td>\n <td>rt433</td>\n <td>Minor road</td>\n <td>POINT Z (8.55841 47.35217 0.00000)</td>\n </tr>\n <tr>\n <th>1</th>\n <td>9FD6441F802C20A6E0430A865E3320A6</td>\n <td>2011</td>\n <td>1</td>\n <td>Saturday</td>\n <td>1</td>\n <td>1246980</td>\n <td>2682382</td>\n <td>Accident with skidding or self-accident</td>\n <td>at0</td>\n <td>as3</td>\n <td>True</td>\n <td>True</td>\n <td>True</td>\n <td>rt433</td>\n <td>Minor road</td>\n <td>POINT Z (8.52932 47.36851 0.00000)</td>\n </tr>\n <tr>\n <th>2</th>\n <td>9FDA0DC4856A6094E0430A865E336094</td>\n <td>2011</td>\n <td>1</td>\n <td>Saturday</td>\n <td>2</td>\n <td>1247749</td>\n <td>2682791</td>\n <td>Accident with skidding or self-accident</td>\n <td>at0</td>\n <td>as4</td>\n <td>True</td>\n <td>True</td>\n <td>True</td>\n <td>rt439</td>\n <td>Other</td>\n <td>POINT Z (8.53488 47.37538 0.00000)</td>\n </tr>\n <tr>\n <th>3</th>\n <td>A3B66E42396E6000E0430A865E336000</td>\n <td>2011</td>\n <td>1</td>\n <td>Saturday</td>\n <td>2</td>\n <td>1247102</td>\n <td>2681199</td>\n <td>Accident when crossing the lane(s)</td>\n <td>at5</td>\n <td>as3</td>\n <td>True</td>\n <td>True</td>\n <td>True</td>\n <td>rt433</td>\n <td>Minor road</td>\n <td>POINT Z (8.51368 47.36976 0.00000)</td>\n </tr>\n <tr>\n <th>4</th>\n <td>9FDA0DBE8CCE9096E0430A865E339096</td>\n <td>2011</td>\n <td>1</td>\n <td>Saturday</td>\n <td>3</td>\n <td>1250690</td>\n <td>2682479</td>\n <td>Accident with skidding or self-accident</td>\n <td>at0</td>\n <td>as4</td>\n <td>True</td>\n <td>True</td>\n <td>True</td>\n <td>rt433</td>\n <td>Minor road</td>\n <td>POINT Z (8.53129 47.40187 0.00000)</td>\n </tr>\n </tbody>\n</table>\n</div>"
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"acc_gdf.head()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:51.086294164Z",
|
||||
"start_time": "2024-01-03T12:40:51.049756885Z"
|
||||
}
|
||||
},
|
||||
"id": "eb4bf665a06e9923"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import folium"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:51.222351595Z",
|
||||
"start_time": "2024-01-03T12:40:51.077888125Z"
|
||||
}
|
||||
},
|
||||
"id": "602d84c746e89cce"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "<folium.features.GeoJson at 0x7f74c4545590>"
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pandas\n",
|
||||
"import requests\n",
|
||||
"state_geo = requests.get(\n",
|
||||
" \"https://www.ogd.stadt-zuerich.ch/wfs/geoportal/Statistische_Quartiere?service=WFS&version=1.1.0&request=GetFeature&outputFormat=GeoJSON&typename=adm_statistische_quartiere_map\"\n",
|
||||
").json()\n",
|
||||
"state_data = pandas.read_csv(\n",
|
||||
" \"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_unemployment_oct_2012.csv\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"m = folium.Map(location=[47.38, 8.52], zoom_start=13)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"folium.GeoJson(state_geo).add_to(m)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:52.577237632Z",
|
||||
"start_time": "2024-01-03T12:40:51.224665299Z"
|
||||
}
|
||||
},
|
||||
"id": "df0b59f35b494a62"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "<folium.folium.Map at 0x7f74c4bd5750>",
|
||||
"text/html": "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe srcdoc=\"<!DOCTYPE html>\n<html>\n<head>\n \n <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n \n <script>\n L_NO_TOUCH = false;\n L_DISABLE_3D = false;\n </script>\n \n <style>html, body {width: 100%;height: 100%;margin: 0;padding: 0;}</style>\n <style>#map {position:absolute;top:0;bottom:0;right:0;left:0;}</style>\n <script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.js"></script>\n <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>\n <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>\n <script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js"></script>\n <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.css"/>\n <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"/>\n <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"/>\n <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.2.0/css/all.min.css"/>\n <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css"/>\n <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css"/>\n \n <meta name="viewport" content="width=device-width,\n initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />\n <style>\n #map_8eab87ff9e4a5914ed52ff079cd05ee6 {\n position: relative;\n width: 100.0%;\n height: 100.0%;\n left: 0.0%;\n top: 0.0%;\n }\n .leaflet-container { font-size: 1rem; }\n </style>\n \n <script src="https://cdn.jsdelivr.net/gh/python-visualization/folium@main/folium/templates/leaflet_heat.min.js"></script>\n</head>\n<body>\n \n \n <div class="folium-map" id="map_8eab87ff9e4a5914ed52ff079cd05ee6" ></div>\n \n</body>\n<script>\n \n \n var map_8eab87ff9e4a5914ed52ff079cd05ee6 = L.map(\n "map_8eab87ff9e4a5914ed52ff079cd05ee6",\n {\n center: [47.36865, 8.539183],\n crs: L.CRS.EPSG3857,\n zoom: 13,\n zoomControl: false,\n preferCanvas: false,\n dragging: false,\n scrollWheelZoom: false,\n doubleClickZoom: false,\n }\n );\n\n \n\n \n \n var tile_layer_555664b6b08519197019e50997be0eb8 = L.tileLayer(\n "https://tile.openstreetmap.org/{z}/{x}/{y}.png",\n {"attribution": "\\u0026copy; \\u003ca href=\\"https://www.openstreetmap.org/copyright\\"\\u003eOpenStreetMap\\u003c/a\\u003e contributors", "detectRetina": false, "maxNativeZoom": 19, "maxZoom": 19, "minZoom": 0, "noWrap": false, "opacity": 1, "subdomains": "abc", "tms": false}\n );\n \n \n tile_layer_555664b6b08519197019e50997be0eb8.addTo(map_8eab87ff9e4a5914ed52ff079cd05ee6);\n \n \n var heat_map_cdc9e01d94f1ed246edc30e0946806d5 = L.heatLayer(\n [[47.352168, 8.55841], [47.368512, 8.52932], [47.375376, 8.534877], [47.369756, 8.513682], [47.401865, 8.531288], [47.428652, 8.543582], [47.391125, 8.522617], [47.376931, 8.541636], [47.374602, 8.535854], [47.366542, 8.540613], [47.387107, 8.519462], [47.394692, 8.546759], [47.404676, 8.557116], [47.367753, 8.545709], [47.37445, 8.546113], [47.403796, 8.485857], [47.367773, 8.494523], [47.387926, 8.485241], [47.373877, 8.5135], [47.39161, 8.527568], [47.384609, 8.531597], [47.421708, 8.550131], [47.363555, 8.554744], [47.388514, 8.483835], [47.377256, 8.538689], [47.409028, 8.566496], [47.352644, 8.572305], [47.363447, 8.554768], [47.377263, 8.510179], [47.39341, 8.52545Line truncated
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"import folium\n",
|
||||
"from folium.plugins import HeatMap\n",
|
||||
"from pyproj import Transformer\n",
|
||||
"\n",
|
||||
"acc_gdf['latitude'] = acc_gdf.geometry.y\n",
|
||||
"acc_gdf['longitude'] = acc_gdf.geometry.x\n",
|
||||
"zurich_coordinates = [47.368650, \t8.539183]\n",
|
||||
"\n",
|
||||
"fixed_map_zurich_original_coords = folium.Map(\n",
|
||||
" location=zurich_coordinates, \n",
|
||||
" zoom_start=13, \n",
|
||||
" zoom_control=False, \n",
|
||||
" dragging=False, \n",
|
||||
" scrollWheelZoom=False, \n",
|
||||
" doubleClickZoom=False\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"HeatMap(data=acc_gdf[['latitude', 'longitude']], radius=10).add_to(fixed_map_zurich_original_coords)\n",
|
||||
"fixed_map_zurich_original_coords"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:40:59.611801469Z",
|
||||
"start_time": "2024-01-03T12:40:59.323708433Z"
|
||||
}
|
||||
},
|
||||
"id": "cded2fff6806c2dc"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create a base map\n",
|
||||
"gradient = {\n",
|
||||
" 0.1: 'blue',\n",
|
||||
" 0.3: 'cyan',\n",
|
||||
" 0.5: 'lime',\n",
|
||||
" 0.7: 'yellow',\n",
|
||||
" 0.9: 'red'\n",
|
||||
"}\n",
|
||||
"interactive_map = folium.Map(\n",
|
||||
" location=zurich_coordinates, \n",
|
||||
" zoom_start=13, \n",
|
||||
" zoom_control=True, \n",
|
||||
" dragging=False, \n",
|
||||
" scrollWheelZoom=True, \n",
|
||||
" doubleClickZoom=False\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"example_years = [2011, 2012, 2013, 2014, 2014, 2016, 2017, 2018, 2019, 2020, 2021, 2022]\n",
|
||||
"\n",
|
||||
"for year in example_years:\n",
|
||||
" \n",
|
||||
" year_data = acc_gdf[acc_gdf['AccidentYear'] == year]\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" heatmap_layer = HeatMap(\n",
|
||||
" data=year_data[['latitude', 'longitude']],\n",
|
||||
" radius=8, \n",
|
||||
" gradient=gradient,\n",
|
||||
" min_opacity=0.5,\n",
|
||||
" max_opacity=0.8,\n",
|
||||
" blur=10,\n",
|
||||
" show=False,\n",
|
||||
" name=f'Accidents in {year}'\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" heatmap_layer.add_to(interactive_map)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"folium.LayerControl(collapsed=False).add_to(interactive_map)\n",
|
||||
"folium.TileLayer(wfs)\n",
|
||||
"interactive_map.save(\"accident_map_interactive.html\")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-03T12:43:24.525437654Z",
|
||||
"start_time": "2024-01-03T12:43:24.337895855Z"
|
||||
}
|
||||
},
|
||||
"id": "738ca6a4a67ca1bd"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
+3
-3
@@ -125,7 +125,7 @@ def process_foot_bike_data(files_present=True):
|
||||
|
||||
def process_miv_data(files_present=True):
|
||||
miv_df_unified = du.create_unified_df(miv_file_urls, motor_file_u_string, data_dir, files_present=files_present)
|
||||
|
||||
logger.debug("Unified MIV dataframe created.")
|
||||
miv_df_unified[['Datum', "Time"]] = miv_df_unified['MessungDatZeit'].str.split('T', expand=True)
|
||||
miv_df_unified[['Hrs', 'Mins', 'Sec']] = miv_df_unified['Time'].str.split(':', expand=True)
|
||||
|
||||
@@ -180,7 +180,7 @@ def process_all_data_sources(fb_present=True, miv_present=True, accident_present
|
||||
"""
|
||||
# ensure_dirs_exist(data_dir, integrated_dir)
|
||||
logger.info("Started processing all data sources.")
|
||||
fb_to_integrated(fb_present)
|
||||
#fb_to_integrated(fb_present)
|
||||
|
||||
miv_to_integrated_csv(miv_present)
|
||||
|
||||
@@ -239,7 +239,7 @@ def load_tempo_geojson_from_api_to_local():
|
||||
|
||||
if __name__ == '__main__':
|
||||
# ensure_dirs_exist(data_dir, integrated_dir, logs_dir)
|
||||
process_all_data_sources(True, False, False)
|
||||
process_all_data_sources(True, True, False)
|
||||
# miv_to_integrated_csv()
|
||||
# acc_to_cleaned_geojson()
|
||||
load_tempo_geojson_from_api_to_local()
|
||||
Reference in new issue
Block a user