Introduction

The GeoBase routing engine provides convenient route strategy functionality via the RoutingStrategyFastest, RoutingStrategyShortest and RoutingStrategyForPedestrian classes.

These classes are used to adjust the strategy used by the GeoBase routing engine to create the fastest routes between stops (at the expense of route distance, if necessary; for example by favoring freeways over surface streets), the shortest routes between stops (at the expense of route duration, if necessary; for example favoring surface streets over freeways) and the best routes for pedestrians.

This tutorial describes common adjustments to the behavior of the routing engine using these three classes, with the aid of a simple GeoBase application.

Prerequisites

This tutorial assumes some familiarity with Microsoft Visual Studio. You will need a copy of Visual Studio 2005 or later and a moderately specified desktop computer. You will also need a licensed copy of the Telogis GeoBase SDK. A 30-day free-of-charge trial may be downloaded from the GeoBase developer portal: http://dev.telogis.com/. Two versions of the trial SDK are available, one loaded with US (West Coast) map data, the other version loaded with map data for the UK. The locations used in this tutorial assume that US data is being used.

This tutorial also assumes that you are already familiar with the basics of creating a GeoBase-specific Visual Studio Project and adding a map control to a GeoBase project.

The Application

Create a new Visual Studio Project (a Windows Forms Application) called 'Strategy'.

Add a reference to geobase.net.dll then, to simplify our code, add the following usings to the top of the project form (Form1.cs).

using Telogis.GeoBase;
using Telogis.GeoBase.Routing;

Return to the 'Design' view (Shift+F7) and add the following controls to the form:

  • • A GeoBase map control named mapMain
  • • A button named buttonFastest with the text 'Fastest Route'
  • • A button named buttonShortest with the text 'Shortest Route'
  • • A button named buttonPedestrians with the text 'Pedestrian Route'

When run, your new application should appear similar to the screenshot below:

Strategy Start

Next, add the following code to the top of the project such that the items are global (immediately above the Form1 constructor). Here we create a RenderList object to display the map and our routes, several RouteStop objects, and an empty Route object.


// Create rendererlist
RendererList renderList = new RendererList();

// Create route stops 
RouteStop a = new RouteStop(new LatLon(33.647536, -117.774116));
RouteStop b = new RouteStop(new LatLon(33.633131, -117.685001));
RouteStop c = new RouteStop(new LatLon(33.593368, -117.721010));
RouteStop d = new RouteStop(new LatLon(33.611624, -117.752008));
RouteStop e = new RouteStop(new LatLon(33.607467, -117.722967));

// Create a route
Route myRoute = new Route();

Add the following code to the Form1 constructor, beneath InitializeComponent(). This will add the RouteStops created above to our Route, and set the map's default renderer, center location and zoom level.

// Set the first stop of the route
myRoute.Start = a;

// Add more stops to the route
myRoute.AddStops(new RouteStop[] { b, c, d, e });

// Set the default renderer
mapMain.Renderer = renderList;

// Set the map center
mapMain.Center = new LatLon(33.6221, -117.7244);

// Set the map zoom
mapMain.Zoom = 8;

Now add a click event to each of the three buttons (three in total). These click events will be used to trigger routing strategy changes and recalculations, and a route highlight redraw using the modified routing strategy.

Update the buttonFastest_Click event to match the following:

		private void buttonFastest_Click(object sender, EventArgs e) {
			// Clear the render list
			renderList.Clear();

			// Set the route strategy to fastest then force a recalculation
			myRoute.Strategy = new RoutingStrategyFastest();
			myRoute.ForceRecalculate();

			// Get directions
			Directions fastdirs = myRoute.GetDirections();

			// Set the route highlight color
			fastdirs.RenderColor = Color.Blue;

			// Work out the time and distance of the new route
			TimeSpan time = fastdirs.GetTotalTime();
			double totalDistance = Math.Floor(fastdirs.GetTotalDistance(DistanceUnit.MILES));
			String msg = "Fastest route will take " + time.Hours + " hours, " + time.Minutes + " minutes to complete.";
			msg += "\nRoute length is around " + totalDistance + " miles.";

			// Add the route to the renderer
			renderList.Add(fastdirs);

			// Force redraw
			mapMain.Invalidate();

			MessageBox.Show(msg, "Route Details (Fastest)", 
				MessageBoxButtons.OK, 
				MessageBoxIcon.Information);
		}

Update the buttonShortest_Click event to match the following:

		private void buttonShortest_Click(object sender, EventArgs e) {
			// Clear the render list
			renderList.Clear();

			// Set the route strategy to shortest then force a recalculation
			myRoute.Strategy = new RoutingStrategyShortest();
			myRoute.ForceRecalculate();

			// Get directions
			Directions shortdirs = myRoute.GetDirections();

			// Set the route highlight color
			shortdirs.RenderColor = Color.Red;

			// Work out the time and distance of the new route
			TimeSpan time = shortdirs.GetTotalTime();
			double totalDistance = Math.Floor(shortdirs.GetTotalDistance(DistanceUnit.MILES));
			String msg = "The shortest route will take " + time.Hours + " hours, " + time.Minutes + " minutes to complete.";
			msg += "\nRoute length is around " + totalDistance + " miles.";

			// Add the route to the renderer
			renderList.Add(shortdirs);

			// Force redraw
			mapMain.Invalidate();

			MessageBox.Show(msg, "Route Details (Shortest)", 
				MessageBoxButtons.OK, 
				MessageBoxIcon.Information);
		}

Lastly, update the buttonPedestrians_Click event to match the following:

		private void buttonPedestrians_Click(object sender, EventArgs e) {
			// Clear the render list
			renderList.Clear();

			// Set the route strategy to pedestrian then force a recalculation
			myRoute.Strategy = new RoutingStrategyForPedestrian();
			myRoute.ForceRecalculate();

			// Get directions
			Directions walkersdirs = myRoute.GetDirections();

			// Set the route highlight color
			walkersdirs.RenderColor = Color.Green;

			// Work out the time and distance of the new route
			TimeSpan time = walkersdirs.GetTotalTime();
			double totalDistance = Math.Floor(walkersdirs.GetTotalDistance(DistanceUnit.MILES));
			String msg = "Pedestrian route will take " + time.Hours + " hours, " + time.Minutes + " minutes to complete.";
			msg += "\nRoute length is around " + totalDistance + " miles.";

			// Add the route to the renderer
			renderList.Add(walkersdirs);

			// Force redraw
			mapMain.Invalidate();

			MessageBox.Show(msg, "Route Details (Pedestrian)", 
				MessageBoxButtons.OK, 
				MessageBoxIcon.Information);
		}

Testing

Run the application. After clicking the 'Fastest Route', 'Shortest Route' and 'Pedestrian Route' buttons the map will be updated with new routes using the specified strategies. After each recalculation a new popup message will be displayed indicating the length of the new route (in miles) and an estimate of the time required to complete it.

Strategy Example Window

Complete Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telogis.GeoBase;
using Telogis.GeoBase.Routing;

namespace Strategy {
	public partial class Form1 : Form {
		// Create rendererlist
		RendererList renderList = new RendererList();

		// Create route stops 
		RouteStop a = new RouteStop(new LatLon(33.647536, -117.774116));
		RouteStop b = new RouteStop(new LatLon(33.633131, -117.685001));
		RouteStop c = new RouteStop(new LatLon(33.593368, -117.721010));
		RouteStop d = new RouteStop(new LatLon(33.611624, -117.752008));
		RouteStop e = new RouteStop(new LatLon(33.607467, -117.722967));

		// Create a route
		Route myRoute = new Route();

		public Form1() {
			InitializeComponent();

			// Set the first stop of the route
			myRoute.Start = a;

			// Add more stops to the route
			myRoute.AddStops(new RouteStop[] { b, c, d, e });

			// Set the default renderer
			mapMain.Renderer = renderList;

			// Set the map center
			mapMain.Center = new LatLon(33.6221, -117.7244);

			// Set the map zoom
			mapMain.Zoom = 8;
		}

		private void buttonFastest_Click(object sender, EventArgs e) {
			// Clear the render list
			renderList.Clear();

			// Set the route strategy to fastest then force a recalculation
			myRoute.Strategy = new RoutingStrategyFastest();
			myRoute.ForceRecalculate();

			// Get directions
			Directions fastdirs = myRoute.GetDirections();

			// Set the route highlight color
			fastdirs.RenderColor = Color.Blue;

			// Work out the time and distance of the new route
			TimeSpan time = fastdirs.GetTotalTime();
			double totalDistance = Math.Floor(fastdirs.GetTotalDistance(DistanceUnit.MILES));
			String msg = "Fastest route will take " + time.Hours + " hours, " + time.Minutes + " minutes to complete.";
			msg += "\nRoute length is around " + totalDistance + " miles.";

			// Add the route to the renderer
			renderList.Add(fastdirs);

			// Force redraw
			mapMain.Invalidate();

			MessageBox.Show(msg, "Route Details (Fastest)", 
				MessageBoxButtons.OK, 
				MessageBoxIcon.Information);
		}

		private void buttonShortest_Click(object sender, EventArgs e) {
			// Clear the render list
			renderList.Clear();

			// Set the route strategy to shortest then force a recalculation
			myRoute.Strategy = new RoutingStrategyShortest();
			myRoute.ForceRecalculate();

			// Get directions
			Directions shortdirs = myRoute.GetDirections();

			// Set the route highlight color
			shortdirs.RenderColor = Color.Red;

			// Work out the time and distance of the new route
			TimeSpan time = shortdirs.GetTotalTime();
			double totalDistance = Math.Floor(shortdirs.GetTotalDistance(DistanceUnit.MILES));
			String msg = "The shortest route will take " + time.Hours + " hours, " + time.Minutes + " minutes to complete.";
			msg += "\nRoute length is around " + totalDistance + " miles.";

			// Add the route to the renderer
			renderList.Add(shortdirs);

			// Force redraw
			mapMain.Invalidate();

			MessageBox.Show(msg, "Route Details (Shortest)", 
				MessageBoxButtons.OK, 
				MessageBoxIcon.Information);
		}

		private void buttonPedestrians_Click(object sender, EventArgs e) {
			// Clear the render list
			renderList.Clear();

			// Set the route strategy to pedestrian then force a recalculation
			myRoute.Strategy = new RoutingStrategyForPedestrian();
			myRoute.ForceRecalculate();

			// Get directions
			Directions walkersdirs = myRoute.GetDirections();

			// Set the route highlight color
			walkersdirs.RenderColor = Color.Green;

			// Work out the time and distance of the new route
			TimeSpan time = walkersdirs.GetTotalTime();
			double totalDistance = Math.Floor(walkersdirs.GetTotalDistance(DistanceUnit.MILES));
			String msg = "Pedestrian route will take " + time.Hours + " hours, " + time.Minutes + " minutes to complete.";
			msg += "\nRoute length is around " + totalDistance + " miles.";

			// Add the route to the renderer
			renderList.Add(walkersdirs);

			// Force redraw
			mapMain.Invalidate();

			MessageBox.Show(msg, "Route Details (Pedestrian)", 
				MessageBoxButtons.OK, 
				MessageBoxIcon.Information);
		}
	}
}

Published, Jul 7th 2016, 21:55

Tagged under: dotnet geobase routing navigation route optimization maps