My ‘long hike’ for 2023 is the Colorado Trail (CT), approximately 500 miles, from just outside Denver to Durango, near the southern state border with New Mexico.
The average elevation is 10.3k, and the highest elevation is 13.3k. I hiked a good part of the CT way back in 2014, where the CDT in Colorado shares a good portion of the route.
Join me on my journey, by visiting this blog or subscribing.
Let us take the tools we developed in the last post and apply to the much larger graph of the Gila National Forest, and see what needs debugging. We follow nearly the same steps as the previous post, so refer to that article for an explanation of why we are taking particular steps in a certain order.
One thing discovered after experimentation is that OSMnx embeds a good deal of geometry information on the attributes of graph edges and graph nodes, so my contract_twos function would need a good deal of modification to correctly join the geometries of two edges to a single edge. Instead, we will use a shortcut. OSMnx has a simplify_graph function, which eliminates all nodes of degree 2, but it can only be used once. (I pray that the maintainers of the OSMnx library might remove that restriction in later releases. ) Before, I used that function just after reading in the OpenStreetMap data, but now we will wait. The trade-off is that we start out with a graph with a huge number of nodes for our initial graphs. Our draw function that can handle OSMnx graphs has an option to show the nodes.
OSMnx version: 1.4.0
NetworkX version: 3.1
report(G)
79985 nodes
79983 edges
total length 1546.96 miles
number of zero-degree nodes : 11
number of odd intersections : 900
number of even intersections : 79085
number of 2-way intersections : 79027
number of trailhead/terminus : 489
draw(G)
If we turn on display_nodes, we see a jungle of them. The extra nodes slow down calculations, but not too much to bear. (Perhaps I need to display nodes in a higher contrast color than ‘pink’.
A few utility functions were created to help solve our maximum-length trail problem. Since most of you may not be interested in the code, I will first describe what each function does, and then append the source at the end for you to skip over.
First is report(graph, description=”), which reports of the number of edges and nodes of a graph, and some basic statistics. We will use our first example graph as an example. We make a small change to start with a Multigraph instead of a Graph, and to use ‘length’ instead of ‘weight’, to be more compatible with OSMnx later.
report(G)
16 nodes
21 edges
total length 0.02 miles
number of odd intersections : 10
number of even intersections : 6
number of 2-way intersections : 5
number of trailhead/terminus : 1
The function draw(graph, title=”) displays our simple graphs, and can handle Multigraphs.
draw(G)
Trailhead/terminus points are dead-ends that cannot be included in a grand circuit, so it is computationally easier to eliminate them early, with delete_ends(graph).