OsmSharp/Tutorials/Routing

From OpenStreetMap Wiki
Jump to: navigation, search

This tutorial is about OsmSharp and can be used to calculate simple point-to-point routes and saving them als a GPX-file. All routing will have to be done using the Router class. It encapsulates all the routing functionality. A datasource can be created using a regular osm file.

Osmsharp point2point tutorial result.png

The code for tiny tutorial can be downloaded here.

  • Import Osm.Core, Osm.Data, Osm.Routing, Tools.Core and Tools.Math into a new project.
  • Use the following code to calculate one small point-to-point route.
// create a router from an osm file.
Router router = Router.CreateFrom(new FileInfo("demo.osm"));

// instantiate route end coordinates.
GeoCoordinate location_from = new GeoCoordinate(51.26565,4.7777);
GeoCoordinate location_to = new GeoCoordinate(51.2741,4.79795);

// resolve the end points and link them to the road network.
ResolvedPoint from = router.Resolve(location_from);
ResolvedPoint to = router.Resolve(location_to);

// calculate the route.
OsmSharpRoute route = router.Calculate(from, to);

// save the route as a GPX file.
route.SaveAsGpx(new FileInfo("demo.gpx"));