Open Street Map
Open Street Map is map (data) as we know it from commercial solutions such as Google maps. In Open Street Map however, all the data is gathered by volunteers under an open source license approximately like Wikipedia creates articles, and the data is thus available for download at e.g. Geofabrik. (If you download data here, please respect their bandwidth, and do not download more/ more often than necessary.)
Obtaining features
If you wish to extract features from Open Street Map, e.g. things designated as either "shop" or "amenity", you can accomplish it using my VRP instance generator.
You can select from the categories shop and amenity, and for each of these, you can specify a type of data to search for. If nothing is specified, any entries will be listed. A query is done by holding down shift, and dragging the mouse over the required region of the map. After a short wait, the results will be displayed on the map, and will become browsable/deleteable in the list below the map. By clicking dump all current points will be written in a copyable format.
If you need to automate the process, the necessary http requests are dumped to the log each time you query.
I self host the backend covering only Europe (due to space issues), and guarantee no stability or performance, but if you interact with is manually you should mostly be fine. Again, please be considerate, and report any problems using an email adress at my domain. If your needs exceeds my capabilities, you are welcome to contact me.
Calculating distances
Getting distances between points is an oft requested feature, and is relatively easy to acheive. Solutions such as Graph Hopper and OSRM provides path finding engines, and what you use may be down to eprsonal preference. I prefer OSRM, partiallly because I mostly work in c++, and thus can integrate with it more easily. The following guide will assume you use OSRM. (And requires no c++ knowledge.)
Short summation of the procedure
For more details, read the sections.
- Install OSRM
- Download the
???.osm.pbf
file, where???
is the smallest area you could need from e.g. e.g. Geofabrik. - Run
osrm-extract ???.osm.pbf -p car.lua
where car.lua is the desired mode. - Run
osrm-partition ???.osrm
- Run
osrm-customize ???.osrm
- Run
osrm-routed --algorithm=MLD ???.osrm
(Only close with Ctrl+C when you are done calculating distances.) - Run calculate.py in a directory with shops.csv. Adapt the files as needed.
Obtaining the map data
Open Street Map data is available from a range of places e.g. Geofabrik. You should download the smalles region you need, e.g. Denmark instead of the whole of Europe.
Obtaining and using OSRM
If you use linux, OSRM might well be available in your favorite package manager typically as osrm-backend
, and can otherwise easily be build and installed using cmake. If you use Windows, you can download a precompiled version. No installation is needed, just navigate to the folder using a command prompt, and execute the commands you want to use.
First you will have to extract the map data to a .osrm file, as Open Street Map contains much more than what is needed for shotest path algorithms. The extraction is done using a command like osrm-extract berlin-latest.osm.pbf -p car.lua
. In this command the berlin-latest.osm.pbf
should be replaced with a path to the map you downloaded, and car.lua
is a file specifying what mode of transport is to be used, what roads can be used and at what speed. The defaults supplied with OSRM car.lua
, bicycle.lua
and foot.lua
are generally reasonable, but you can modify them as needed. I recommend extracting one mode, and one mode only in a single directory. (Symbolic links of the osm.pbf file can save you space if you need multiple modes.)
The next steps are partitioning and customizing. This is done using the commands osrm-partition berlin.osrm
and osrm-customize berlin.osrm
where berlin needs to be replaced with the name of your extracted file.
Next you start a routing server on your own computer using the command osrm-routed --algorithm=MLD berlin.osrm
. You can test it is running using the link http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true in a browser. You may want to select two lattitude, longitude pairs you know is within the region you downloaded.
If you want to stop the server, try Ctrl+C.
Using the server through python
Now your computer can act as a host to calculate distances. You may want to automate the process, and I can recommend a small python script.
To use it you will need the modules pandas
, csv
and requests
installed.
The following code will read a csv file of the format shops.csv and can be downloaded as calculate.py. Not that you may want to change the mode in the request from driving to e.g. foot.
import requests
import csv
import pandas
df=pandas.read_csv('shops.csv', sep=";",decimal=".", skipinitialspace=True)
print(df)
for i in range(len(df)):
coord1 = (df.iloc[i]["Longitude"], df.iloc[i]["Latitude"])
for j in range(len(df)):
coord2 = (df.iloc[j]["Longitude"], df.iloc[j]["Latitude"])
print(coord1, df.iloc[i]["Description"]," -> ", coord2, df.iloc[j]["Description"])
request = "http://127.0.0.1:5000/route/v1/driving/{:.8f},{:.8f};{:.8f},{:.8f}?overview=false".format(coord1[0],coord1[1],coord2[0], coord2[1])
r = requests.get(request)
print("\tdistance",r.json()["routes"][0]["distance"])
print("\tduration",r.json()["routes"][0]["duration"])