2

I am working on a graph flow model in the context of transport networks. I have the position of sensors (lat/lon) and would like to associate these sensors with nodes on a graph retrieved using osmnx.

At present, I use get_nearest_node to map a sensor to a node. However, this isn't optimal, as I'm at the mercy of the cartographer -- straight roads will be have fewer nodes, and so the mean displacement (and therefore error) will be higher, even when dealing with unsimplified graphs. I had considered using get_nearest_edge, but I'd still need to edit the graph to insert a new node at the position of the sensor.

Instead, I thought a reasonable way of achieving this would be to upsample the graph (perhaps using redistribute_vertices), applying get_nearest_node, and then re-simplifying the graph, but somehow whitelisting the node that is now associated with a sensor to prevent it from being removed.

However, it's not clear to me how to go from the output of redistribute_vertices to a graph -- it returns a LineString or MultiLineString rather than a new graph.

I saw this question posted on the osmnx GitHub project: https://github.com/gboeing/osmnx/issues/304, in which a GeoDataFrame is generated, with a new column containing the redistributed way as a (Multi)LineString. However, I'm not sure how I can map this new gdf back to a Graph -- the corresponding node dataframe hasn't been updated, and u and v values remain the same in the new edges table.

Any pointers (including telling me I'm going about this the wrong way and should be using function XYZ) would be really appreciated.

Joseph Redfern
  • 939
  • 1
  • 6
  • 13
  • 1
    No easy answer there. Unsimplified graphs are an easy way to make it more fine-grained, but may not work if your straight roads are too long. If you go the redistribute_vertices route, you'll have to convert those vertices to nodes, add those nodes to a (new?) graph, add new edges connecting those nodes, and then do a nearest node search. – gboeing Jul 13 '20 at 20:42
  • Thanks for your comment @gboeing (and thanks for your hard work writing and maintaining osmnx). I will try hacking some code together that creates new nodes based on the output of `redistribute_vertices`. If I get something together, I'll share the code here as an answer. One additional question: I mentioned "whitelisting the node that is now associated with a sensor to prevent it from being removed". Is this something supported by osmnx, or would I need to hack `osmnx.simplification.simplify_graph` to facilitate this? – Joseph Redfern Jul 13 '20 at 21:07
  • 1
    You'll have to hack. But off the top of my head it'd be relatively easy: just edit the [_is_endpoint](https://github.com/gboeing/osmnx/blob/v0.15.1/osmnx/simplification.py#L15) function to return True if the node is in your predefined whitelist of nodes. – gboeing Jul 14 '20 at 16:12

0 Answers0