ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace DifferentProjectionsWithRouting { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } } }
using System; using System.Collections.ObjectModel; using System.Drawing; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.Routing; namespace DifferentProjectionsWithRouting { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { // Set the extent and the background color mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-97.7566, 30.3048, -97.7206, 30.2764), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); WorldMapKitLayer worldMapKitLayer = new WorldMapKitLayer(); mapEngine.StaticLayers.Add(worldMapKitLayer); //Projection used to convert the result of routing to Geodetic to match World Map Kit. Proj4Projection proj4 = new Proj4Projection(); proj4.InternalProjectionParametersString = Proj4Projection.GetEsriParametersString(102339); //State Plane Texas Central proj4.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);// Geodetic WGS84 //Layer of the routing data in meters (State Plane) ShapeFileFeatureLayer Layer1 = new ShapeFileFeatureLayer(@"..\..\Data\stateplaneaustinstreets.shp"); //Converts the start and end point to State Plane to do the routing in the projection of the street data. proj4.Open(); Vertex startVertex = proj4.ConvertToInternalProjection(-97.7277, 30.3018); Vertex endVertex = proj4.ConvertToInternalProjection(-97.7507, 30.2821); //Gets the feature nearest to the start and end point. Layer1.Open(); Collection<Feature> startFeatures = Layer1.QueryTools.GetFeaturesNearestTo(new PointShape(startVertex), GeographyUnit.Meter, 1, ReturningColumnsType.NoColumns); Collection<Feature> endFeatures = Layer1.QueryTools.GetFeaturesNearestTo(new PointShape(endVertex), GeographyUnit.Meter, 1, ReturningColumnsType.NoColumns); Layer1.Close(); //Now we have our data ready (the street layer and the start/end points), so we can start doing the routing in the original projection (State Plane). //Notice that we are not using RoutingLayer which is no more than a convenient class to display routing result and start/end points. The whole routing logic //can be performed without using that class. RtgRoutingSource rtgRoutingSource = new RtgRoutingSource(@"..\..\Data\stateplaneaustinstreets.rtg"); RoutingEngine routingEngine = new RoutingEngine(rtgRoutingSource, Layer1.FeatureSource); //Make sure the geography Unit is set to Meter as the projection is State Plane meters. routingEngine.GeographyUnit = GeographyUnit.Meter; //Gets the routing result. RoutingResult routingResult = routingEngine.GetRoute(startFeatures[0].GetShape().GetCenterPoint(), endFeatures[0].GetShape().GetCenterPoint()); //We convert to Geodetic (decimal degrees) the results from the routing. LineShape georouteMultilineShape = (LineShape)proj4.ConvertToExternalProjection(routingResult.Route); PointShape geoStartPointShape = (PointShape)proj4.ConvertToExternalProjection(startFeatures[0].GetShape().GetCenterPoint()); PointShape geoEndPointShape = (PointShape)proj4.ConvertToExternalProjection(endFeatures[0].GetShape().GetCenterPoint()); proj4.Close(); //We use the InMemoryFeatureLayers to display the routing results and the start/end points in the projected Geodetic to match World Map Kit. InMemoryFeatureLayer routeInMemoryFeatureLayer = new InMemoryFeatureLayer(); routeInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Red), 5, true); routeInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; routeInMemoryFeatureLayer.Open(); routeInMemoryFeatureLayer.EditTools.BeginTransaction(); routeInMemoryFeatureLayer.EditTools.Add(new Feature(georouteMultilineShape)); routeInMemoryFeatureLayer.EditTools.CommitTransaction(); routeInMemoryFeatureLayer.Close(); InMemoryFeatureLayer pointInMemoryFeatureLayer = new InMemoryFeatureLayer(); pointInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.Orange, 12); pointInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; pointInMemoryFeatureLayer.Open(); pointInMemoryFeatureLayer.EditTools.BeginTransaction(); pointInMemoryFeatureLayer.EditTools.Add(new Feature(geoStartPointShape)); pointInMemoryFeatureLayer.EditTools.Add(new Feature(geoEndPointShape)); pointInMemoryFeatureLayer.EditTools.CommitTransaction(); pointInMemoryFeatureLayer.Close(); mapEngine.DynamicLayers.Add(routeInMemoryFeatureLayer); mapEngine.DynamicLayers.Add(pointInMemoryFeatureLayer); DrawImage(); } 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); } } }