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.Collections.ObjectModel; using System.Linq; using System.Text; using ThinkGeo.MapSuite.Core; namespace CenteringAndRotatingOnMovingFeature { public enum Position { UpperLeft, UpperRight, LowerLeft, LowerRight }; //This class accepts two images. One image for the needle (north arrow) and another optional image for //the frame of the compass. Typically, we want to have an image for the frame representing the angles (in degrees or other units) //representing to what angle the map is rotated. We could have a regular frame as in a traditional compass. //The image for the frame stays fixed as the map rotates whereas the image for the needle rotates with the map to always point //to the north. Keep in mind that the frame image needs to have to transparency so that it does not occult the needle image. class CompassAdornmentLayer : AdornmentLayer { GeoImage needleImage = null; GeoImage frameImage = null; Position position; int sizePercentage; float rotateAngle; //Use that constructor with only one image for the needle (will work as a simple north arrow) public CompassAdornmentLayer(GeoImage needleImage, Position position, int sizePercentage) { this.needleImage = needleImage; this.position = position; this.sizePercentage = sizePercentage; } //Use that constructor with both images for needle and frame of the compass. public CompassAdornmentLayer(GeoImage needleImage, GeoImage frameImage, Position position, int sizePercentage) { this.needleImage = needleImage; this.frameImage = frameImage; this.position = position; this.sizePercentage = sizePercentage; } public GeoImage NeedleImage { get { return needleImage; } set { needleImage = value; } } public GeoImage FrameImage { get { return frameImage; } set { frameImage = value; } } //We needs to update that property as the map rotates to have the needle showing the north accurately. public float RotateAngle { get { return rotateAngle; } set { rotateAngle = value; } } public Position Position { get { return position; } set { position = value;} } public int SizePercentage { get { return sizePercentage; } set { sizePercentage = value; } } protected override void DrawCore(GeoCanvas canvas, Collection<SimpleCandidate> labelsInAllLayers) { //Draws the scaled images of needle and frame images according to the percentage size. For example with a PercentageSize of 80, //the compass size will be 80% of the size of needle image. //Sets the position of the compass. int margin = 10; int Xloc = 0; int Yloc = 0; int imageWidth = (sizePercentage * needleImage.GetWidth()) / 100; int imageHeight = (sizePercentage * needleImage.GetHeight()) / 100; switch (position) { case Position.UpperLeft: Xloc = (imageWidth /2)+ margin; Yloc = (imageHeight / 2) + margin; break; case Position.UpperRight: Xloc = ((int)canvas.Width -(imageWidth /2)) - margin; Yloc = (imageHeight / 2) + margin; break; case Position.LowerLeft: Xloc = (imageWidth / 2) + margin; Yloc = ((int)canvas.Height - (imageHeight / 2)) - margin; break; case Position.LowerRight: Xloc = ((int)canvas.Width - (imageWidth / 2)) - margin; Yloc = ((int)canvas.Height - (imageHeight / 2)) - margin; break; } //Draws the needle image at the size according to the percentage size and rotate the image according to RotateAngle property. canvas.DrawScreenImage(needleImage, Xloc, Yloc, imageWidth, imageHeight, DrawingLevel.LevelFour, 0, 0, rotateAngle); //If the frame image has been set, draws image at the size according to the percentage size with no rotation. if (frameImage != null) { canvas.DrawScreenImage(frameImage, Xloc, Yloc, imageWidth, imageHeight, DrawingLevel.LevelFour, 0, 0, 0); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace CenteringAndRotatingOnMovingFeature { 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 System.IO; using System.Data; using ThinkGeo.MapSuite.Core; namespace CenteringAndRotatingOnMovingFeature { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; private StreamReader mGPSData = new StreamReader(@"..\..\data\GPSinfo1.txt"); private Timer timer; private double previousLong; private double previousLat; private RotationProjection rotateProjection = new RotationProjection(); public TestForm() { InitializeComponent(); timer = new Timer(); } private void TestForm_Load(object sender, EventArgs e) { //Sets timers properties. timer.Interval = 1000; timer.Tick += new EventHandler(timer_Tick); mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-66.8581, 10.4995, -66.8456, 10.4915), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.FromArgb(255, 233, 232, 214)); //Loads all the backgrounds layers for street, street label, park and park label. LoadBackgroundLayers(); //Makes sure all the layers have the Projection property set to RotationProjection. foreach (FeatureLayer Layer in mapEngine.StaticLayers) { Layer.FeatureSource.Projection = rotateProjection; } //InMemoryFeatureLayer for vehicle. InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer(); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.PointType = PointType.Bitmap; inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image = new GeoImage(@"..\..\data\sedan.png"); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.RotationAngle = 45; inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; inMemoryFeatureLayer.InternalFeatures.Add("Car", new Feature(new PointShape())); inMemoryFeatureLayer.FeatureSource.Projection = rotateProjection; //Adds the InMemoryFeatureLayer for the car. mapEngine.DynamicLayers.Add("CarLayer", inMemoryFeatureLayer); //Gets the image for the needle of the compass that will rotate with the map. GeoImage needleImage = new GeoImage(@"..\..\Data\compass1_needle_letters_disc.png"); //Gets the image for the frame of the compass that will stay fixed. GeoImage frameImage = new GeoImage(@"..\..\Data\compass1_circle_digits.png"); //Passes the images, the position on the map and the size of the compass in percentage to the size of the needle image. CompassAdornmentLayer compassAdornmentLayer = new CompassAdornmentLayer(needleImage, frameImage, Position.UpperLeft, 100); //Adds CompassAdornmentLayer for the compass to the AdormentLayers collection of mapEngine. mapEngine.AdornmentLayers.Add("MyCompassAdornmentLayer", compassAdornmentLayer); DrawImage(); timer.Start(); } void timer_Tick(object sender, EventArgs e) { //Gets the GPS info from the textfile. DataTable carData = GetCarData(); double angle; InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)mapEngine.DynamicLayers["CarLayer"]; PointShape pointShape = inMemoryFeatureLayer.InternalFeatures[0].GetShape() as PointShape; // Get the Row of Data we are working with. DataRow carDataRow = carData.Rows[0]; double Lat = Convert.ToDouble(carDataRow["LAT"]); double Long = Convert.ToDouble(carDataRow["LONG"]); if (previousLong == 0) { previousLong = Long; previousLat = Lat; } double Xdiff = previousLong - Long; double Ydiff = previousLat - Lat; //Gets the angle based on the current GPS position and the previous one to get the direction of the vehicle. angle = GetAngleFromTwoVertices(new Vertex(previousLong, previousLat), new Vertex(Long, Lat)); //Sets the angle of the car icon so that it is facing up. inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.RotationAngle = 90; //Converts to external (rotated) projection the longitude and latitude to update the pointshape location of the car. PointShape rotatedPointShape = (PointShape)rotateProjection.ConvertToExternalProjection(new PointShape(Long,Lat)); pointShape.X = rotatedPointShape.X; pointShape.Y = rotatedPointShape.Y; pointShape.Id = "Car"; inMemoryFeatureLayer.Open(); inMemoryFeatureLayer.EditTools.BeginTransaction(); inMemoryFeatureLayer.EditTools.Update(pointShape); inMemoryFeatureLayer.EditTools.CommitTransaction(); inMemoryFeatureLayer.Close(); previousLong = Long; previousLat = Lat; //First, centers the map to the location of the moving vehicle mapEngine.CenterAt(new PointShape(pointShape.X, pointShape.Y), Map.Width, Map.Height); //Second, rotates the map to the angle of the direction of the moving vehicle. rotateProjection.Angle = (float) angle; mapEngine.CurrentExtent = rotateProjection.GetUpdatedExtent(mapEngine.CurrentExtent); //Updates RotateAngle property of CompassAdornmentLayer (Compass) according to the new value of //Angle property of RotationProjection. CompassAdornmentLayer compassAdornmentLayer = (CompassAdornmentLayer)mapEngine.AdornmentLayers[0]; compassAdornmentLayer.RotateAngle = (float)rotateProjection.Angle; DrawImage(); } private DataTable GetCarData() { DataTable datatable = new DataTable(); datatable.Columns.Add("LAT"); datatable.Columns.Add("LONG"); string strLattitude = ""; string strLongitude = ""; // Read the next line from the text file with GPS data in it. string strCurrentText = mGPSData.ReadLine(); if (strCurrentText == "") { mGPSData.BaseStream.Seek(0, SeekOrigin.Begin); strCurrentText = mGPSData.ReadLine(); } while (strCurrentText != null) { // Every other line is a "/" and we want to skip those. if (strCurrentText.Trim() != "/") { string[] strSplit = strCurrentText.Split(','); // (':'); strLongitude = strSplit[0]; strLattitude = strSplit[1]; break; } strCurrentText = mGPSData.ReadLine(); } object[] objs = new object[2] { strLattitude, strLongitude }; datatable.Rows.Add(objs); return datatable; } //We assume that the angle is based on a third point that is on top of b on the same x axis. private double GetAngleFromTwoVertices(Vertex b, Vertex c) { double alpha = 0; double tangentAlpha = (c.Y - b.Y) / (c.X - b.X); double Peta = Math.Atan(tangentAlpha); if (c.X > b.X) { alpha = 90 - (Peta * (180 / Math.PI)); } else if (c.X < b.X) { alpha = 270 - (Peta * (180 / Math.PI)); } else { if (c.Y > b.Y) alpha = 0; if (c.Y < b.Y) alpha = 180; } return alpha; } private void LoadBackgroundLayers() { ShapeFileFeatureLayer ParksShapeLayer = new ShapeFileFeatureLayer(@"..\..\Data\parks.shp"); ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\parks.shp", BuildIndexMode.DoNotRebuild); ParksShapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ParksShapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Park1; ShapeFileFeatureLayer ParksLabelLayer = new ShapeFileFeatureLayer(@"..\..\Data\parks.shp"); ParksLabelLayer.RequireIndex = false; ParksLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("Name", "Arial", 10, DrawingFontStyles.Bold, GeoColor.StandardColors.SeaGreen, GeoColor.StandardColors.White, 2); ParksLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ShapeFileFeatureLayer StreetsShapeLayer = new ShapeFileFeatureLayer(@"..\..\Data\streets.shp"); ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\streets.shp", BuildIndexMode.DoNotRebuild); StreetsShapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ValueStyle valueStyle = new ValueStyle(); valueStyle.ColumnName = "Type"; valueStyle.ValueItems.Add(new ValueItem("A", LineStyles.LocalRoad2)); valueStyle.ValueItems.Add(new ValueItem("M", LineStyles.MajorRoad1)); StreetsShapeLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle); StreetsShapeLayer.FeatureSource.Projection = rotateProjection; ShapeFileFeatureLayer StreetsLabelLayer = new ShapeFileFeatureLayer(@"..\..\Data\streets.shp"); StreetsLabelLayer.RequireIndex = false; TextStyle textStyle = new TextStyle(); textStyle.TextColumnName = "St_name"; textStyle.TextSolidBrush = new GeoSolidBrush(GeoColor.StandardColors.Black); textStyle.Font = new GeoFont("Arial", 7); textStyle.TextLineSegmentRatio = 10; textStyle.GridSize = 100; textStyle.SplineType = SplineType.StandardSplining; textStyle.DuplicateRule = LabelDuplicateRule.OneDuplicateLabelPerQuadrant; StreetsLabelLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(textStyle); StreetsLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.GridSize = 5; StreetsLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; StreetsLabelLayer.FeatureSource.Projection = rotateProjection; mapEngine.StaticLayers.Add("ParksShapeLayer", ParksShapeLayer); mapEngine.StaticLayers.Add("StreetsShapeLayer", StreetsShapeLayer); mapEngine.StaticLayers.Add("ParksLabelLayer", ParksLabelLayer); mapEngine.StaticLayers.Add("StreetsLabelLayer", StreetsLabelLayer); } 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.DrawAdornmentLayers(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); Vertex vertex = rotateProjection.ConvertToInternalProjection(pointShape.X, pointShape.Y); //Displays world coordinates. statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(vertex.X, 4) + " Y:" + Math.Round(vertex.Y, 4); } } }