Package: routor
routorSimple routing engine for OpenStreetMaps with easy to customize profiles/weight-functions.RequirementsPython 3.6.1 or newerInstallationpipinstallroutorUsageCLIThe CLI offers multiple commands, useroutor --helpto find out more.Download mapDownloads a compatible map from OSM, eg.routordownload"Bristol, England"./bristol.graphmlBy default it only adds a handful of tags (nodes,edge) to the graph.
Use-nor-eto add other available tags (edge,node) as well.
Additionally, you can download multiple regions at once:routordownload-njunction-ntraffic_signals-esurface-elanes"Bristol, England""Somerset, England"./bristol_somerset.graphmlBy default, each downloaded map is enhanced withstreet_count- how many physical segments are connected to a nodebearing- angle of each edgespeed_kph- free-flow travel speed based onmaxspeed, fallback is set to30kph (seeosmnxfor more information)travel_time- Travel time based onspeed_kphandlengthIf you provide aGoogle API(using --api-key), the following additional attributes are available:elevation- elevation above sea levelgrade/grade_abs- grade of an endgeCalculate routeDetermine the optimal route between two points using the given weight function and print the route asJSONtostdout.routorroute--./bristol.graphml"51.47967237816338,-2.6174926757812496""51.45422084861252,-2.564105987548828""routor.weights.length"Web APIConfigurationThe configuration is either read from a.envfile or the environment.
Before you are able to run the server, you have to set the variables mentioned inroutor/api/config.py.Run the APIThe api is served usinguvicorn.
To start the server runuvicornroutor.api.main:appThe API will be available athttp://127.0.0.1:8000and the docs athttp://127.0.0.1:8000/docs.As libraryYou can also use the engine as a library.
To calculate a route from A to B you can dofrompathlibimportPathfromroutor.engineimportEnginefromroutorimportmodels,weights...map_path=Path(...)engine=Engine(map_path)origin=models.Location(latitude=51.47967237816338,longitude=-2.6174926757812496)destination=models.Location(latitude=51.45422084861252,longitude=-2.564105987548828)route=engine.route(origin,destination,weight_func=weights.length,travel_time_func=weights.travel_time)# shortest distanceAvailable weight-functions"length"/routor.weights.lengthCalculates the shortest path from A to B, only the length of an edge is taken into account."travel_time"/routor.weight.travel_timeCalculates the fastest route based ontravel time.Pluginsroutorimplements a simple plugin mechanism.
Simply create a new module with the prefixroutor_, make it available (install it,sys.pathhack or similar) and it will be automatically discovered and loaded.
Depending on how you structure your module/plugin, you have to do the registration of the additional functionality in eitherroutor_YOUR_MODULE/__init__.pyorroutor_YOUR_MODULE.py.Register a new weight functionExisting weight functions are defined inroutor/weights.pyand can be taken as reference.
To register a new function in your plugin, you have to implement something similar to# __init__.pyfromtypingimportOptionalfromroutor.weightsimportregisterfromroutorimportmodelsdefmy_weight_func(prev_edge:Optional[models.Edge],edge:models.Edge)->float:...return...register(my_weight_func,"weight_func")DevelopmentThis project usespoetryfor packaging and
managing all dependencies andpre-committo runflake8,isort,mypyandblack.Additionally,pdbppandbetter-exceptionsare installed to provide a better debugging experience.
To enablebetter-exceptionsyou have to runexport BETTER_EXCEPTIONS=1in your current session/terminal.Clone this repository and runpoetryinstall
poetryrunpre-commitinstallto create a virtual enviroment containing all dependencies.
Afterwards, You can run the test suite usingpoetryrunpytestThis repository follows theConventional Commitsstyle.Cookiecutter templateThis project was created usingcruftand thecookiecutter-pyprojecttemplate.
In order to update this repository to the latest template version runcruftupdatein the root of this repository.