[PYTHON] First OSMnx ~ With shortest path search ~

There weren't many articles in Japanese about OSMnx, and it was too difficult for me as a weak English person to collect information ... So I wrote that it should be useful for other weak English people. Therefore, it is an introductory article on OSMnx for the weak English by the weak English.

1. 1. About OSMnx

What is OSMnx? I think there are many people who think that, so I will explain a little at the beginning. Do you know Open Street first?

What is OpenStreetMap?

Think of it as a map version of wikipedia. A map that anyone can write and use is OpenStreetMap. Information such as place names and roads is written on this map.

And OSMnx was developed to use that information.

What is OSMnx

A Python package for using OpenStreetMap map data. You can get map data, analyze it, and visualize it.

2. Steps to install OSMnx

The installation method written by the developer made it easy, so I will post it.

STEP1 First of all, let's add Anaconda. Please refer to Other site for how to install Anaconda.

Various sites also show how to do it with the pip command, but it is easy to get stuck with an error when installing Geopandas (experience story)

STEP2 Once you have Anaconda in place, just type these two commands. conda config --prepend channels conda-forge conda create -n ox --strict-channel-priority osmnx

Now you can use OSMnx on Anaconda's virtual environment ox.

When using OSMnx, when opening Anaconda Prompt activate ox Type the command to switch to the virtual environment ox.

3. 3. Shortest path search by OSMnx

The OSMnx installation method wasn't very tasty, so I'll add the shortest path search.

This time, we will acquire a road network centered on Tokyo Sky Tree. Looking at OpenStreetMap, the roads around Tokyo Sky Tree look like this. tokyoSkyTreeMap.png

And it is an image that acquired and displayed the road network with OSMnx. tokyoSkyTreeGraph.png

You can see that they match when you stack them. overlay.jpg

Let's take a closer look at the types of this road network and how to obtain it.

Road network type

There are roads for pedestrians and cars, right? So you can get various kinds of road networks.

*'drive'-Public roads where cars can pass *'drive_service'-Including public roads and service roads where cars can pass? (The translation here is unknown) *'walk'-Sidewalk *'bike'-Road where bicycles can pass *'all'-All non-private roads *'all_private'-All roads, including driveways

This time, we have acquired the road network of'drive'.

How to get a road network

When getting a road network

Specify either of them.

This time, the latitude and longitude are specified and the road network is acquired.

Get by latitude and longitude

This is the function that gets the road network from latitude and longitude.

ox.graph_from_point(center_point, dist=300, network_type='drive')



 * center_point --Tuple with latitude and longitude
 * dist-How many meters of data should be taken from the specified location
 * network_type-what type of road network to take

 There are other arguments, but omitted

## Shortest path search
 So far we have seen how to get a road network.
 The shortest path search can be easily calculated with this function as long as the road network is acquired.

#### **`ox.shortest_path(G, orig, dest, weight='length')`**
```shortest_path(G, orig, dest, weight='length')


 * G-Input graph
 * orig --Start node
 * dest --Target node
 * weight --Enter'length'to find the shortest path

 The whole code looks like this:


#### **`shortestPath.py`**
```python

import osmnx as ox

#Latitude and longitude of Tokyo Sky Tree
location_point = (35.7100069, 139.8108103)
#Graph of roadway within 300m centering on Tokyo Sky Tree
G = ox.graph_from_point(location_point, dist=300, network_type='drive')

#Display the acquired road network
ox.plot_graph(G, node_color='r')

#Calculate the shortest route from orig to dest of a node
orig = list(G)[0]
dest = list(G)[1]
route = ox.shortest_path(G, orig, dest, weight='length')
#Show the shortest route
ox.plot_graph_route(G, route, route_color='y', route_linewidth=6, node_size=0)

When this is executed, a graph of such a node-to-node shortest path is output. This time, the 0th and 1st nodes are selected appropriately.

tokyoSkyTreeRoot.png

It was confirmed that the route that seems to be the shortest route is output properly.

Reference material

Installation method written by the developer OpenStreetMap Developer's Github ・ A lot of sample programs have been uploaded. OSMnx module description

Recommended Posts

First OSMnx ~ With shortest path search ~
Implement the shortest path search for the maze
Find the shortest path with the Python Dijkstra's algorithm
Sequential search with Python
Binary search with python
Binary search with Python3
Database search with db.py
In-graph path search using Networkx
Bit full search with Go
First neuron simulation with NEURON + Python
Full bit search with Python
oslo.config config file search path
I can't search with # google-map. ..
Search engine work with python
Search twitter tweets with python
Streamline web search with python
Try to solve the shortest path with Python + NetworkX + social data