====== Source Code ServicesEditionSample RoutingExtension RoutingTolerance CS 100825.zip ====== ====Program.cs==== using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace RoutingTolerance { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } } } ====TestForm.cs==== using System; using System.Collections.ObjectModel; using System.Drawing; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.Routing; namespace RoutingTolerance { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; private ShapeFileFeatureLayer StreetLayer = null; private RoutingLayer routingLayer = null; private Collection pointShapes = new Collection(); private string text1, text2; public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { //Text for the instructions: text1 = "Click on the map to set the Start point."; text2 = "Click on the map to set the End point."; label1.Text = text1; // Defines layer to render the Austin streets StreetLayer = new ShapeFileFeatureLayer(@"..\..\Data\austinstreets.shp"); StreetLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.LocalRoad3; StreetLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("AustinStreets", StreetLayer); //Routing layer to render the route. routingLayer = new RoutingLayer(); mapEngine.DynamicLayers.Add("RoutingLayer", routingLayer); mapEngine.CurrentExtent = new RectangleShape(-97.7566, 30.3048, -97.7206, 30.2764); DrawImage(); } private void Map_MouseClick(object sender, MouseEventArgs e) { //Gets the world point for where the user clicked. PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, e.X, e.Y, Map.Width, Map.Height); //Checks if there are street features within the tolerance of the clicked point. StreetLayer.Open(); Collection features = StreetLayer.QueryTools.GetFeaturesWithinDistanceOf(pointShape, GeographyUnit.DecimalDegree, DistanceUnit.Meter, 25, ReturningColumnsType.NoColumns); StreetLayer.Close(); if (features.Count == 0) { //If no feature found within the tolerance, warns the user. string message; if (pointShapes.Count == 0) { message = "The Start point must be within 25 meters of a street"; } else { message = "The End point must be within 25 meters of a street"; } MessageBox.Show(message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { //If features are found, set the Start point at first click. At the second click, sets the end point and builds the route. if (pointShapes.Count < 1) { routingLayer.Routes.Clear(); pointShapes.Add(pointShape); routingLayer.StartPoint = pointShape; routingLayer.EndPoint = null; DrawImage(); label1.Text = text2; } else { //Finds the route between start point and end point. The rtg file needs to be build by Routing explorer if it has not been done already. RtgRoutingSource rtgRoutingSource = new RtgRoutingSource(@"..\..\Data\austinstreets.rtg"); RoutingEngine routingEngine = new RoutingEngine(rtgRoutingSource, StreetLayer.FeatureSource); pointShapes.Add(pointShape); LineShape routeLineShape = routingEngine.GetRoute(pointShapes[0], pointShapes[1]).Route; routingLayer.EndPoint = pointShape; routingLayer.Routes.Add(routeLineShape); pointShapes.Clear(); DrawImage(); label1.Text = text1; } } } private void DrawImage() { if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(Map.Width, Map.Height); mapEngine.OpenAllLayers(); mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree); mapEngine.DrawDynamicLayers(bitmap, GeographyUnit.DecimalDegree); mapEngine.CloseAllLayers(); Map.Image = bitmap; } private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e) { switch (e.Button.Tag.ToString()) { case "Zoom In": mapEngine.CurrentExtent.ScaleDown(50); break; case "Zoom Out": mapEngine.CurrentExtent.ScaleUp(50); break; case "Full Extent": mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height); break; case "Pan Left": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20); break; case "Pan Right": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20); break; case "Pan Up": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20); break; case "Pan Down": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20); break; default: break; } DrawImage(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void Map_MouseMove(object sender, MouseEventArgs e) { //Displays the X and Y in screen coordinates. statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y; //Gets the PointShape in world coordinates from screen coordinates. PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height); //Displays world coordinates. statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4); } } }