{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Matching with an AequilibraE Model\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import uuid\nfrom os.path import join\nfrom tempfile import gettempdir\n\nfrom aequilibrae.utils.create_example import create_example\n\nfrom mapmatcher import MapMatcher\nfrom mapmatcher.examples import nauru_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nauru_gps = nauru_data()\n\n# Let's see if the data has all the fields we need\nnauru_gps.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since it does not, let's fix it\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nauru_gps.rename(columns={\"x\": \"longitude\", \"y\": \"latitude\", \"vehicle_unique_id\": \"trace_id\"}, inplace=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get our AequilibraE model for Nauru and create the map-mather from this model\nWe also need to provide the transportation mode we want to consider for the\nmap-matching\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "project = create_example(join(gettempdir(), uuid.uuid4().hex), \"nauru\")\n\n# We need to have at least one centroid connector to the model due to a bug in AequilibraE 0.9.5\nproject.conn.execute(\"Update Nodes set is_centroid=1 where node_id = 1\")\nmmatcher = MapMatcher.from_aequilibrae(project, \"c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "let's add the GPS data to the map-matcher and run it!\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mmatcher.load_gps_traces(nauru_gps)\nmmatcher.map_match(parallel_threads=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for trip in mmatcher.trips:\n if trip.success:\n break" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import folium\nimport geopandas as gpd\nimport numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a geometry list from the GeoDataFrame\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trace = trip.trace.to_crs(4326)\ngeo_df_list = [[point.xy[1][0], point.xy[0][0], ts] for point, ts in zip(trace.geometry, trace.timestamp)]\n\nresult_layer = folium.FeatureGroup(\"Map-match result\")\ntrace_layer = folium.FeatureGroup(\"GPS traces\")\n\n# Iterate through list and add a marker for each GPS ping.\ni = 0\nfor lat, lng, ts in geo_df_list:\n trace_layer.add_child(\n folium.CircleMarker(\n location=[lat, lng],\n radius=1,\n fill=True, # Set fill to True\n color=\"black\",\n tooltip=str(ts),\n fill_opacity=1.0,\n )\n )\n\ngdf = gpd.GeoDataFrame({\"d\": [1]}, geometry=[trip.path_shape], crs=3857).to_crs(4326).explode(ignore_index=True)\nfor _, rec in gdf.iterrows():\n coords = rec.geometry.xy\n coordinates = [[y, x] for x, y in zip(coords[0], coords[1])]\n folium.PolyLine(coordinates, weight=5, color=\"red\").add_to(result_layer)\n\nmap = folium.Map(location=[np.mean(coords[1]), np.mean(coords[0])], tiles=\"OpenStreetMap\", zoom_start=15)\n\nresult_layer.add_to(map)\ntrace_layer.add_to(map)\nfolium.LayerControl(position=\"bottomright\").add_to(map)\nmap" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trip.result" ] } ], "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.10.15" } }, "nbformat": 4, "nbformat_minor": 0 }