{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Matching with a generic network\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import uuid\nfrom os.path import join\nfrom tempfile import gettempdir\n\nimport geopandas as gpd\nimport numpy as np\nfrom aequilibrae.utils.create_example import create_example\nfrom aequilibrae.paths import Graph\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": [ "Let's get a Nauru example from AequilibraE and extract the link network from it\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "project = create_example(join(gettempdir(), uuid.uuid4().hex), \"nauru\")\n\nsql = \"SELECT link_id, a_node, b_node, direction, distance, Hex(ST_AsBinary(geometry)) as geom FROM links\"\ngdf = gpd.GeoDataFrame.from_postgis(sql, project.conn, geom_col=\"geom\", crs=4326)\ngdf.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's build a graph with the network.\nFor that, you need ta few key fields in the network: [link_id, a_node, b_node, direction]\nYou also need a cost field for your graph, which is *distance* in our case\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "g = Graph()\ng.cost = gdf[\"distance\"].to_numpy()\n\ng.network = gdf\n\n# Let's say nodes 1 through 133 are centroids\ng.prepare_graph(centroids=np.arange(133) + 1)\ng.set_graph(\"distance\")\ng.set_skimming([\"distance\"])\ng.set_blocked_centroid_flows(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "let's build the map-matcher object and run it!\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mmatcher = MapMatcher()\nmmatcher.load_network(graph=g, links=gdf)\nmmatcher.load_gps_traces(nauru_gps)\n\n# Let's run it single-threaded\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 }