Search code examples
luaopenstreetmaposrm

How to use external data in an OSRM profile


It this Mapbox blog post, Lauren Budorick shares how they got working a routing engine with OSRM that uses elevation data in order to give cyclists better routes... AMAZING!

I also want to explore the potential of OSRM's routing when plugging in external (user-generated) data, but I'm still having a hard time grasping how OSRM's profiles work. I think I get the main idea, that every way (or node?) is piped into a few functions that, all toghether, scores how good that path is.

But that's it, there are plenty of missing parts in my head, like what do each of the functions Lauren uses in her profile do. If anyone could point me to some more detailed information on how all of this works, you'd make my next week much, much easier :)

Also, in Lauren's post, inside source_function she loads a ./srtm_bayarea.asc file. What does that .asc file looks like? How would one generate a file like that one from, let's say, data stored in a pgsql database? Can we use some other format, like GeoJSON?

Then, when in segment_function she uses things like source.lon and target.lat, are those refered to the raw data stored in the asc file? Or is that file processed into some standard that maps everything to comply it?

As you can see, I'm a complete newbie on routing and maybe GIS in general, but I'd love to learn more about this standards and tools that circle around the OSRM ecosystem. Can you share some tips with me?


Solution

  • I think I get the main idea, that every way (or node?) is piped into a few functions that, all toghether, scores how good that path is.

    Right, every way and every node are scored as they are read from an OSM dump to determine passability of a node and speed of a way (used as the scoring heuristic).

    A basic description of the data format can be found here. As it reads, data immediately available in ArcInfo ASCII grids includes SRTM data. Currently plaintext ASCII grids are the only supported format. There are several great Python tools for GIS developers that may help in converting other data types to ASCII grids - check out rasterio, for example. Here's an example of a really simple python script to convert NED IMGs to ASCII grids:

    import sys
    import rasterio as rio
    import numpy as np
    
    args = sys.argv[1:]
    
    with rio.drivers():
        with rio.open(args[0]) as src:
            elev = src.read()[0]
            profile = src.profile
    
        def shortify(x):
            if x == profile['nodata']:
                return -9999
            elif x == np.finfo(x).tiny:
                return 0
            else:
                return int(round(x))
    
        out_elev = [map(shortify, row) for row in elev]
    
        with open(args[0] + '.asc', 'a') as dst:
            np.savetxt(dst, np.array(out_elev),fmt="%s",delimiter=" ")
    

    source.lon and target.lat e.g: source and target are nodes provided as arguments by the extraction process. Their coordinates are used to look up data at each location during extraction.

    Make sure to read thoroughly through the relevant wiki page (already linked).

    Feel free alternately to open a Github issue in https://github.com/Project-OSRM/osrm-backend/issues with OSRM questions.