{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import geopandas as gpd\n",
"\n",
"import altair as alt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" year | \n",
" quarter | \n",
" hour_of_day | \n",
" segment_id | \n",
" start_junction_id | \n",
" end_junction_id | \n",
" osm_way_id | \n",
" osm_start_node_id | \n",
" osm_end_node_id | \n",
" speed_kph_mean | \n",
" speed_kph_stddev | \n",
" speed_kph_p50 | \n",
" speed_kph_p85 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2019 | \n",
" 2 | \n",
" 0 | \n",
" d0034ae2336f81ef5933a211f2ce2d979f0aff2a | \n",
" 66081f9fe2860af2a498e1248334cb51d6ea5073 | \n",
" 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 | \n",
" 9932085 | \n",
" 1236497729 | \n",
" 1236497752 | \n",
" 41.258 | \n",
" 9.239 | \n",
" 41.330 | \n",
" 46.680 | \n",
"
\n",
" \n",
" 1 | \n",
" 2019 | \n",
" 2 | \n",
" 23 | \n",
" d0034ae2336f81ef5933a211f2ce2d979f0aff2a | \n",
" 66081f9fe2860af2a498e1248334cb51d6ea5073 | \n",
" 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 | \n",
" 9932085 | \n",
" 1236497729 | \n",
" 1236497752 | \n",
" 35.794 | \n",
" 6.706 | \n",
" 35.832 | \n",
" 41.398 | \n",
"
\n",
" \n",
" 2 | \n",
" 2019 | \n",
" 2 | \n",
" 22 | \n",
" d0034ae2336f81ef5933a211f2ce2d979f0aff2a | \n",
" 66081f9fe2860af2a498e1248334cb51d6ea5073 | \n",
" 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 | \n",
" 9932085 | \n",
" 1236497729 | \n",
" 1236497752 | \n",
" 32.766 | \n",
" 13.401 | \n",
" 35.171 | \n",
" 43.469 | \n",
"
\n",
" \n",
" 3 | \n",
" 2019 | \n",
" 2 | \n",
" 23 | \n",
" 277640ca389fc7f0fc8a583386e6063df80485f0 | \n",
" ad26106a25d52c3409bd0165f05de3047a4e96b5 | \n",
" 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 | \n",
" 9932085 | \n",
" 81398215 | \n",
" 1236497752 | \n",
" 34.507 | \n",
" 4.639 | \n",
" 35.207 | \n",
" 38.160 | \n",
"
\n",
" \n",
" 4 | \n",
" 2019 | \n",
" 2 | \n",
" 1 | \n",
" 277640ca389fc7f0fc8a583386e6063df80485f0 | \n",
" ad26106a25d52c3409bd0165f05de3047a4e96b5 | \n",
" 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 | \n",
" 9932085 | \n",
" 81398215 | \n",
" 1236497752 | \n",
" 41.512 | \n",
" 6.049 | \n",
" 41.128 | \n",
" 47.681 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" year quarter hour_of_day segment_id \\\n",
"0 2019 2 0 d0034ae2336f81ef5933a211f2ce2d979f0aff2a \n",
"1 2019 2 23 d0034ae2336f81ef5933a211f2ce2d979f0aff2a \n",
"2 2019 2 22 d0034ae2336f81ef5933a211f2ce2d979f0aff2a \n",
"3 2019 2 23 277640ca389fc7f0fc8a583386e6063df80485f0 \n",
"4 2019 2 1 277640ca389fc7f0fc8a583386e6063df80485f0 \n",
"\n",
" start_junction_id \\\n",
"0 66081f9fe2860af2a498e1248334cb51d6ea5073 \n",
"1 66081f9fe2860af2a498e1248334cb51d6ea5073 \n",
"2 66081f9fe2860af2a498e1248334cb51d6ea5073 \n",
"3 ad26106a25d52c3409bd0165f05de3047a4e96b5 \n",
"4 ad26106a25d52c3409bd0165f05de3047a4e96b5 \n",
"\n",
" end_junction_id osm_way_id osm_start_node_id \\\n",
"0 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 9932085 1236497729 \n",
"1 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 9932085 1236497729 \n",
"2 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 9932085 1236497729 \n",
"3 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 9932085 81398215 \n",
"4 9c00a9375aeaba9c08f4f2fd507cc1808ba83b89 9932085 81398215 \n",
"\n",
" osm_end_node_id speed_kph_mean speed_kph_stddev speed_kph_p50 \\\n",
"0 1236497752 41.258 9.239 41.330 \n",
"1 1236497752 35.794 6.706 35.832 \n",
"2 1236497752 32.766 13.401 35.171 \n",
"3 1236497752 34.507 4.639 35.207 \n",
"4 1236497752 41.512 6.049 41.128 \n",
"\n",
" speed_kph_p85 \n",
"0 46.680 \n",
"1 41.398 \n",
"2 43.469 \n",
"3 38.160 \n",
"4 47.681 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#https://movement.uber.com/cities/berlin/downloads/speeds?lang=en-US&tp[y]=2019&tp[q]=2\n",
"uber_data= pd.read_csv(r\"movement-speeds-quarterly-by-hod-berlin-2019-Q2.csv\\movement-speeds-quarterly-by-hod-berlin-2019-Q2.csv\")\n",
"uber_data.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" @id | \n",
" cycleway:right | \n",
" highway | \n",
" lanes | \n",
" lit | \n",
" maxspeed | \n",
" name | \n",
" oneway | \n",
" postal_code | \n",
" ... | \n",
" after_construction:lanes | \n",
" after_construction:parking:condition:right | \n",
" after_construction:parking:lane:right | \n",
" construction | \n",
" construction:end_date | \n",
" disused:cycleway:left | \n",
" protected_bike_lane:right | \n",
" cycleway:right:buffer | \n",
" geometry | \n",
" osm_way_id | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" way/4045243 | \n",
" way/4045243 | \n",
" track | \n",
" primary | \n",
" 3 | \n",
" yes | \n",
" 50 | \n",
" Frankfurter Allee | \n",
" yes | \n",
" 10247 | \n",
" ... | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" LINESTRING (13.45421 52.51571, 13.45439 52.515... | \n",
" 4045243 | \n",
"
\n",
" \n",
" 1 | \n",
" way/4045656 | \n",
" way/4045656 | \n",
" None | \n",
" secondary | \n",
" 2 | \n",
" None | \n",
" 50 | \n",
" Hauptstraße | \n",
" None | \n",
" None | \n",
" ... | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" LINESTRING (13.37282 52.59334, 13.37281 52.593... | \n",
" 4045656 | \n",
"
\n",
" \n",
"
\n",
"
2 rows × 431 columns
\n",
"
"
],
"text/plain": [
" id @id cycleway:right highway lanes lit maxspeed \\\n",
"0 way/4045243 way/4045243 track primary 3 yes 50 \n",
"1 way/4045656 way/4045656 None secondary 2 None 50 \n",
"\n",
" name oneway postal_code ... after_construction:lanes \\\n",
"0 Frankfurter Allee yes 10247 ... None \n",
"1 Hauptstraße None None ... None \n",
"\n",
" after_construction:parking:condition:right \\\n",
"0 None \n",
"1 None \n",
"\n",
" after_construction:parking:lane:right construction construction:end_date \\\n",
"0 None None None \n",
"1 None None None \n",
"\n",
" disused:cycleway:left protected_bike_lane:right cycleway:right:buffer \\\n",
"0 None None None \n",
"1 None None None \n",
"\n",
" geometry osm_way_id \n",
"0 LINESTRING (13.45421 52.51571, 13.45439 52.515... 4045243 \n",
"1 LINESTRING (13.37282 52.59334, 13.37281 52.593... 4045656 \n",
"\n",
"[2 rows x 431 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#http://overpass-turbo.eu/s/PVN\n",
"osm_links = gpd.read_file('berlin.geojson')\n",
"osm_links['osm_way_id']=osm_links['id'].str[4:]\n",
"osm_links.osm_way_id=osm_links.osm_way_id.astype(int)\n",
"osm_links.head(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### filtern"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"osm_herm= osm_links[(osm_links.name=='Hermannstraße') | (osm_links.name=='Hermannbrücke')].copy()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"uber_data_g=uber_data.groupby(['osm_way_id','hour_of_day'])['speed_kph_mean'].mean().reset_index().copy()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"data_merg= pd.merge(osm_herm[['geometry','osm_way_id','name','postal_code', 'maxspeed']],uber_data_g[['speed_kph_mean','osm_way_id','hour_of_day']] )"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" geometry | \n",
" osm_way_id | \n",
" name | \n",
" postal_code | \n",
" maxspeed | \n",
" speed_kph_mean | \n",
" hour_of_day | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" LINESTRING (13.42487 52.48166, 13.42486 52.48128) | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 28.319 | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" LINESTRING (13.42487 52.48166, 13.42486 52.48128) | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 31.935 | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" LINESTRING (13.42487 52.48166, 13.42486 52.48128) | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 36.163 | \n",
" 2 | \n",
"
\n",
" \n",
" 3 | \n",
" LINESTRING (13.42487 52.48166, 13.42486 52.48128) | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 36.777 | \n",
" 3 | \n",
"
\n",
" \n",
" 4 | \n",
" LINESTRING (13.42487 52.48166, 13.42486 52.48128) | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 39.343 | \n",
" 4 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" geometry osm_way_id \\\n",
"0 LINESTRING (13.42487 52.48166, 13.42486 52.48128) 4526452 \n",
"1 LINESTRING (13.42487 52.48166, 13.42486 52.48128) 4526452 \n",
"2 LINESTRING (13.42487 52.48166, 13.42486 52.48128) 4526452 \n",
"3 LINESTRING (13.42487 52.48166, 13.42486 52.48128) 4526452 \n",
"4 LINESTRING (13.42487 52.48166, 13.42486 52.48128) 4526452 \n",
"\n",
" name postal_code maxspeed speed_kph_mean hour_of_day \n",
"0 Hermannstraße 12049 50 28.319 0 \n",
"1 Hermannstraße 12049 50 31.935 1 \n",
"2 Hermannstraße 12049 50 36.163 2 \n",
"3 Hermannstraße 12049 50 36.777 3 \n",
"4 Hermannstraße 12049 50 39.343 4 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_merg.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### easy shot with unweighted mean\n",
"Disclaimer: in Q2 2019, war noch kein Tempo 30 in Hermannstraße angeordnet!!!!"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
""
],
"text/plain": [
"alt.LayerChart(...)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source=data_merg\n",
"\n",
"## main chart\n",
"chart= alt.Chart(source).mark_point().encode(\n",
" x=alt.X('hour_of_day', axis=alt.Axis(title='Time')),\n",
" y=alt.Y('speed_kph_mean:Q', axis=alt.Axis(title='Speed')),\n",
" color='maxspeed',\n",
").properties(\n",
" width=700,\n",
" height=460,\n",
" )\n",
"\n",
"## line\n",
"chart_line= alt.Chart(source).mark_line().encode(\n",
" x='hour_of_day',\n",
" y='mean(speed_kph_mean):Q',\n",
" color='maxspeed',\n",
")\n",
"\n",
"chart+chart_line"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### now the real deal with weighted mean (by linklenth)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Simon\\Anaconda3\\envs\\geo_julab\\lib\\site-packages\\pyproj\\crs.py:77: FutureWarning: '+init=:' syntax is deprecated. ':' is the preferred initialization method.\n",
" return _prepare_from_string(\" \".join(pjargs))\n"
]
}
],
"source": [
"data_merg=data_merg.to_crs('epsg:3035')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"#length of each link\n",
"data_merg['len']=data_merg.geometry.apply(lambda x: x.length)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"data_merg['weight']=data_merg.len/(data_merg.len.sum()/24)\n",
"data_merg['weighted_speed']=data_merg.weight*data_merg.speed_kph_mean"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"data_merg['weighted_speed_mean']=data_merg.groupby(['hour_of_day'])['weighted_speed'].transform('sum')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" geometry | \n",
" osm_way_id | \n",
" name | \n",
" postal_code | \n",
" maxspeed | \n",
" speed_kph_mean | \n",
" hour_of_day | \n",
" len | \n",
" weight | \n",
" weighted_speed | \n",
" weighted_speed_mean | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" LINESTRING (4553585.112 3269069.619, 4553586.9... | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 28.319 | \n",
" 0 | \n",
" 42.347656 | \n",
" 0.012485 | \n",
" 0.353571 | \n",
" 37.680660 | \n",
"
\n",
" \n",
" 1 | \n",
" LINESTRING (4553585.112 3269069.619, 4553586.9... | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 31.935 | \n",
" 1 | \n",
" 42.347656 | \n",
" 0.012485 | \n",
" 0.398718 | \n",
" 39.135229 | \n",
"
\n",
" \n",
" 2 | \n",
" LINESTRING (4553585.112 3269069.619, 4553586.9... | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 36.163 | \n",
" 2 | \n",
" 42.347656 | \n",
" 0.012485 | \n",
" 0.451506 | \n",
" 40.349164 | \n",
"
\n",
" \n",
" 3 | \n",
" LINESTRING (4553585.112 3269069.619, 4553586.9... | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 36.777 | \n",
" 3 | \n",
" 42.347656 | \n",
" 0.012485 | \n",
" 0.459172 | \n",
" 40.783351 | \n",
"
\n",
" \n",
" 4 | \n",
" LINESTRING (4553585.112 3269069.619, 4553586.9... | \n",
" 4526452 | \n",
" Hermannstraße | \n",
" 12049 | \n",
" 50 | \n",
" 39.343 | \n",
" 4 | \n",
" 42.347656 | \n",
" 0.012485 | \n",
" 0.491209 | \n",
" 41.464624 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" geometry osm_way_id \\\n",
"0 LINESTRING (4553585.112 3269069.619, 4553586.9... 4526452 \n",
"1 LINESTRING (4553585.112 3269069.619, 4553586.9... 4526452 \n",
"2 LINESTRING (4553585.112 3269069.619, 4553586.9... 4526452 \n",
"3 LINESTRING (4553585.112 3269069.619, 4553586.9... 4526452 \n",
"4 LINESTRING (4553585.112 3269069.619, 4553586.9... 4526452 \n",
"\n",
" name postal_code maxspeed speed_kph_mean hour_of_day len \\\n",
"0 Hermannstraße 12049 50 28.319 0 42.347656 \n",
"1 Hermannstraße 12049 50 31.935 1 42.347656 \n",
"2 Hermannstraße 12049 50 36.163 2 42.347656 \n",
"3 Hermannstraße 12049 50 36.777 3 42.347656 \n",
"4 Hermannstraße 12049 50 39.343 4 42.347656 \n",
"\n",
" weight weighted_speed weighted_speed_mean \n",
"0 0.012485 0.353571 37.680660 \n",
"1 0.012485 0.398718 39.135229 \n",
"2 0.012485 0.451506 40.349164 \n",
"3 0.012485 0.459172 40.783351 \n",
"4 0.012485 0.491209 41.464624 "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_merg.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
""
],
"text/plain": [
"alt.VConcatChart(...)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source=data_merg\n",
"\n",
"## POINTS: Straßenabschnitte je h\n",
"chart_point= alt.Chart(source).mark_point(opacity=0.3).encode(\n",
" x=alt.X('hour_of_day', axis=alt.Axis(title='Time [h]')),\n",
" y=alt.Y('speed_kph_mean:Q', axis=alt.Axis(title='Speed [km/h]')),\n",
" size=alt.Size('len',legend=alt.Legend(title=\"Länge Straßenabschnitte [m]\")),\n",
").properties(\n",
" width=700,\n",
" height=460,\n",
" #title='(gewichtete) Geschwindigkeitsverteilung in der Hermannstraße 2019-Q2'\n",
" )\n",
"\n",
"\n",
"## LIN: gewichtete durchschnittliche Geschwindigkeit je h\n",
"source['text9']=''\n",
"chart_line= alt.Chart(source).mark_line().encode(\n",
" x='hour_of_day',\n",
" y='weighted_speed_mean:Q',\n",
" color=alt.Color('text9',legend=alt.Legend(title=\"gewichtete Ø-Geschw.\"))\n",
")\n",
"\n",
"\n",
"\n",
"## style1\n",
"source['line_pos_y']=50\n",
"source['text1']='aktuelle Geschwindigkeitsbegrenzung'\n",
"source['text_pos']=12#source.speed_kph_mean.max()*0.8\n",
"\n",
"rule = alt.Chart(source).mark_rule(color='red', opacity=0.3).encode(\n",
" y='line_pos_y',\n",
" size=alt.value(0.1)\n",
")\n",
"\n",
"text=alt.Chart(source).mark_text(color='red', baseline='top', opacity=0.7).encode(\n",
" y='line_pos_y', #alt.value(50)\n",
" x='text_pos',\n",
" text='text1')\n",
"\n",
"\n",
"## style2\n",
"source['line_pos_y2']=30\n",
"source['text2']='mögliche Geschwindigkeitsbegrenzung?'\n",
"source['text_pos2']=4#source.speed_kph_mean.max()*0.8\n",
"\n",
"rule2 = alt.Chart(source).mark_rule(color='green', opacity=0.3).encode(\n",
" y='line_pos_y2',\n",
" size=alt.value(0.1)\n",
")\n",
"\n",
"text2=alt.Chart(source).mark_text(color='green', baseline='top', opacity=0.7).encode(\n",
" y='line_pos_y2',\n",
" x='text_pos2',\n",
" text='text2')\n",
"\n",
"\n",
"chart=chart_point+chart_line + (rule+text)+ (rule2+text2)\n",
"\n",
"\n",
"# adding Titel and Subtitle\n",
"title = alt.Chart(\n",
" {\"values\": [{\"text\": \"Geschwindigkeitsverteilung in der Hermannstraße/Berlin 2019-Q2\"}]}\n",
").mark_text(size=20, align='left').encode( #,anchor='start' , align='left'\n",
" text=\"text:N\",\n",
" x=alt.value(0)\n",
")\n",
"\n",
"\n",
"subtitle = alt.Chart(\n",
" {\"values\": [{\"text\": 'Basierend auf veröffentlichten Uber-Geschwindigkeitsdaten je h je OSM-Straßenabschnitt'}]}\n",
").mark_text(size=11, align='left').encode(\n",
" text=\"text:N\",\n",
" x=alt.value(0)\n",
" #x=alt.value(100)\n",
" # xOffset=-20\n",
")\n",
" \n",
"fullchart_title= alt.vconcat(\n",
" title,\n",
" subtitle,#\n",
" chart\n",
").configure_view(\n",
" stroke=None\n",
").configure_concat(\n",
" spacing=1\n",
")\n",
"\n",
"\n",
"\n",
"#export\n",
"fullchart_title.save('output/herm/chart_herm.html')\n",
"fullchart_title\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Map"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import folium\n",
"import branca"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"## TEST\n",
"#\n",
"#df= data_merg[data_merg.hour_of_day==12].copy()\n",
"#df.maxspeed=df.maxspeed.astype(int)\n",
"#\n",
"#field='speed_kph_mean'\n",
"#\n",
"#\n",
"#colorscale = branca.colormap.LinearColormap(['green','lightgreen','orange','red'], \n",
"# index=[20,30,40,50], \n",
"# vmin=20,#df.speed_kph_mean.min(), \n",
"# vmax=50,#df.speed_kph_mean.max(), \n",
"# caption='')\n",
"#def style_function(feature):\n",
"# col=feature['properties'][field]\n",
"# return {\n",
"# 'weight': 3,\n",
"# 'color': 'grey' if col is None else colorscale(col)\n",
"# }\n",
"#\n",
"#m = folium.Map(location=[52.46, 13.45],\n",
"# tiles=\"cartodbpositron\",\n",
"# name=\"CartoDB light\",\n",
"# zoom_start=14)\n",
"#\n",
"#folium.GeoJson(\n",
"# df,\n",
"# style_function=style_function,\n",
"#).add_to(m)\n",
"#\n",
"#\n",
"#colorscale.caption = 'Speed [km/h]'\n",
"#m.add_child(colorscale)\n",
"#\n",
"#m"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"# helper\n",
"import folium.plugins\n",
"from folium.features import *\n",
"\n",
"class DivIcon(MacroElement):\n",
" def __init__(self, html='', size=(30,30), anchor=(0,0), style=''):\n",
" \"\"\"TODO : docstring here\"\"\"\n",
" super(DivIcon, self).__init__()\n",
" self._name = 'DivIcon'\n",
" self.size = size\n",
" self.anchor = anchor\n",
" self.html = html\n",
" self.style = style\n",
"\n",
" self._template = Template(u\"\"\"\n",
" {% macro header(this, kwargs) %}\n",
" \n",
" {% endmacro %}\n",
" {% macro script(this, kwargs) %}\n",
" var {{this.get_name()}} = L.divIcon({\n",
" className: '{{this.get_name()}}',\n",
" iconSize: [{{ this.size[0] }},{{ this.size[1] }}],\n",
" iconAnchor: [{{ this.anchor[0] }},{{ this.anchor[1] }}],\n",
" html : \"{{this.html}}\",\n",
" });\n",
" {{this._parent.get_name()}}.setIcon({{this.get_name()}});\n",
" {% endmacro %}\n",
" \"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"for h in uber_data.hour_of_day.unique():\n",
" # Tempo 30\n",
"#get ready\n",
" uber_data_h=uber_data[uber_data.hour_of_day==h].copy()\n",
" uber_data_h=uber_data_h.groupby('osm_way_id')['speed_kph_mean'].mean().reset_index().copy()\n",
"\n",
" osm_herm= osm_links[(osm_links.name=='Hermannstraße') | (osm_links.name=='Hermannbrücke')].copy()\n",
" #merge uber and osm\n",
" data_merg= pd.merge(osm_herm[['geometry','osm_way_id','name','postal_code', 'maxspeed']],uber_data_h[['speed_kph_mean','osm_way_id']] )\n",
"\n",
" data_merg.maxspeed= data_merg.maxspeed.apply(lambda x: 50 if x=='DE:urban' else x)\n",
" data_merg.maxspeed=data_merg.maxspeed.astype(float)\n",
" \n",
" df=data_merg\n",
" #df= data_merg[data_merg.hour_of_day==h].copy()\n",
" #df.maxspeed=df.maxspeed.astype(int)\n",
"\n",
" field='speed_kph_mean'\n",
"\n",
"\n",
" colorscale = branca.colormap.LinearColormap(['green','lightgreen','orange','red'], \n",
" index=[20,30,40,50], \n",
" vmin=20,#df.speed_kph_mean.min(), \n",
" vmax=50,#df.speed_kph_mean.max(), \n",
" caption='')\n",
" def style_function(feature):\n",
" col=feature['properties'][field]\n",
" return {\n",
" 'weight': 3,\n",
" 'color': 'grey' if col is None else colorscale(col)\n",
" }\n",
"\n",
" m = folium.Map(location=[52.475, 13.43],\n",
" tiles=\"cartodbpositron\",\n",
" name=\"CartoDB light\",\n",
" zoom_start=15)\n",
"\n",
" folium.GeoJson(\n",
" df,\n",
" style_function=style_function,\n",
" ).add_to(m)\n",
"\n",
" \n",
" folium.map.Marker(\n",
" #[34.0302, -118.2352],\n",
" #[52.449463, 13.404811],\n",
" [52.47149,13.41880],\n",
" icon=DivIcon(\n",
" size=(150,36),\n",
" anchor=(150,0),\n",
" html=str(h)+' Uhr',\n",
" style=\"\"\"\n",
" font-size:36px;\n",
" background-color: transparent;\n",
" border-color: transparent;\n",
" text-align: right;\n",
" \"\"\"\n",
" )\n",
" ).add_to(m) \n",
"\n",
" colorscale.caption = 'Speed [km/h]'\n",
" m.add_child(colorscale)\n",
"\n",
" #m\n",
" m.save(\"output/herm/map_\"+str(h)+\".html\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"from selenium import webdriver #requires chromesdriver\n",
"from PIL import Image\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
"options = webdriver.ChromeOptions()\n",
"#options.add_argument('--headless')\n",
"driver = webdriver.Chrome(options=options)\n",
"\n",
"for h in uber_data.hour_of_day.unique():\n",
" driver.get(os.getcwd() + '\\\\output\\\\herm\\\\map_'+str(h)+'.html')\n",
" driver.save_screenshot('output\\\\herm\\\\map_'+str(h)+'.png')\n",
" \n",
" # newImg = Image.open('map_'+str(h)+'.png')\n",
" # newImg.save('map_'+str(h)+'_compressed.png', 'PNG', dpi=[200,200])\n",
"\n",
"driver.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['output\\\\herm\\\\map_0.png', 'output\\\\herm\\\\map_1.png', 'output\\\\herm\\\\map_2.png', 'output\\\\herm\\\\map_3.png', 'output\\\\herm\\\\map_4.png', 'output\\\\herm\\\\map_5.png', 'output\\\\herm\\\\map_6.png', 'output\\\\herm\\\\map_7.png', 'output\\\\herm\\\\map_8.png', 'output\\\\herm\\\\map_9.png', 'output\\\\herm\\\\map_10.png', 'output\\\\herm\\\\map_11.png', 'output\\\\herm\\\\map_12.png', 'output\\\\herm\\\\map_13.png', 'output\\\\herm\\\\map_14.png', 'output\\\\herm\\\\map_15.png', 'output\\\\herm\\\\map_16.png', 'output\\\\herm\\\\map_17.png', 'output\\\\herm\\\\map_18.png', 'output\\\\herm\\\\map_19.png', 'output\\\\herm\\\\map_20.png', 'output\\\\herm\\\\map_21.png', 'output\\\\herm\\\\map_22.png', 'output\\\\herm\\\\map_23.png']\n"
]
}
],
"source": [
"import glob\n",
"filenames= glob.glob(\"output\\\\herm\\\\map*.png\")\n",
"\n",
"# sort by hour\n",
"import re\n",
"def atoi(text):\n",
" return int(text) if text.isdigit() else text\n",
"def natural_keys(text):\n",
" return [ atoi(c) for c in re.split(r'(\\d+)', text) ]\n",
"filenames.sort(key=natural_keys)\n",
"print(filenames)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import imageio\n",
"\n",
"images = []\n",
"for filename in filenames:\n",
" images.append(imageio.imread(filename))\n",
"imageio.mimsave('output\\\\herm\\\\uber_berlin2019Q2_herm_24h.gif', images, format='GIF', duration=0.2)\n",
"#imageio.mimsave(exportname, frames, format='GIF', duration=5)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#compress gif with gifsicle (install gifsicle first)\n",
"os.system(\"C:\\ProgramData\\gifsicle-1.92-win64\\gifsicle-1.92\\gifsicle.exe -O3 --colors 256 output\\\\herm\\\\uber_berlin2019Q2_herm_24h.gif -o output\\\\herm\\\\uber_berlin2019Q2_herm_24h.gif\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}