We will use the OSMnx library to import trails from OpenStreetMap to our graph. Installing the library typically requires more than the Python pip utility– use conda, and follow on-line guides– pretty much black magic to me. The folium library is used to display our maps with graphs.
import osmnx as ox import networkx as nx import folium ox.settings.log_console=True ox.settings.use_cache=True
Let us import a trail network.
# location where you want to find your route place = 'Gila National Forest, New Mexico, United States'
We will create a custom filter to only import trails. If we use the default network_type=’walk’ we also get dirt forest roads. Also, make retain_all=True instead of the default, or we will discard disconnected subgraphs, such as the entire Aldo Leopold Wilderness!
cf = '["highway"~"path|footway"]' graph = ox.graph_from_place(place, simplify=True,retain_all=True,custom_filter=cf) len = ox.stats.edge_length_total(graph) #in meters len = len * 0.0006213712 print(f'total length {len:.2f} miles') total length 2839.50 miles
(The total length of trails might be inflated by a factor of 2, because graph is a multidigraph, so edges are doubled.)
Continue reading “Trails and Graph Theory 4 : Import Maps”