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.Text; using ThinkGeo.MapSuite.Core; namespace PolygonAcross180Meridian { //This class, inheriting from Projection, applies the very simple projection of offsetting all the points //360 degrees to the left. class OffsetProjection: Projection, IDisposable { protected override Vertex[] ConvertToExternalProjectionCore(double[] x, double[] y) { Vertex[] vertices = new Vertex[x.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vertex(x[i] - 360, y[i]); } return vertices; } protected override Vertex[] ConvertToInternalProjectionCore(double[] x, double[] y) { Vertex[] vertices = new Vertex[x.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vertex(x[i] + 360, y[i]); } return vertices; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { Close(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace PolygonAcross180Meridian { 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; namespace PolygonAcross180Meridian { 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(-198,115,198,-115), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); //Displays the World Map Kit as a background. ThinkGeo.MapSuite.Core.WorldMapKitLayer worldMapKitLayer = new ThinkGeo.MapSuite.Core.WorldMapKitLayer(); InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer(); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Red), GeoColor.StandardColors.Black); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; inMemoryFeatureLayer.Open(); inMemoryFeatureLayer.EditTools.BeginTransaction(); OffsetProjection offsetProjection = new OffsetProjection(); offsetProjection.Open(); //Original PolygonShape with the unchanged Longitude and Latitude values. PolygonShape polygonShape = new PolygonShape(); RingShape ringShape = new RingShape(); ringShape.Vertices.Add(new Vertex(-176.21, 43.51)); ringShape.Vertices.Add(new Vertex(-157.83, 20.81)); ringShape.Vertices.Add(new Vertex(-174.05, 4.59)); ringShape.Vertices.Add(new Vertex(171.35, 20.81)); ringShape.Vertices.Add(new Vertex(161.62, 12.16)); ringShape.Vertices.Add(new Vertex(156.21, 37.02)); ringShape.Vertices.Add(new Vertex(168.10, 53.24)); ringShape.Vertices.Add(new Vertex(-176.21, 43.51)); polygonShape.OuterRing = ringShape; inMemoryFeatureLayer.EditTools.Add(new Feature(SplitPolygonAt180Meridian(polygonShape))); inMemoryFeatureLayer.EditTools.CommitTransaction(); inMemoryFeatureLayer.Close(); mapEngine.StaticLayers.Add(worldMapKitLayer); mapEngine.DynamicLayers.Add(inMemoryFeatureLayer); DrawImage(); } private MultipolygonShape SplitPolygonAt180Meridian(PolygonShape polygonShape) { //This function gets the MultipolygonShape so that the shape is displayed correctly on decimal degrees map //split at the 180 degree of longitude. OffsetProjection offsetProjection = new OffsetProjection(); offsetProjection.Open(); //RectangleShape of the world used for the Intersection geometric function. RectangleShape worldExtent = new RectangleShape(-180, 90, 180, -90); RingShape westernRingShape = new RingShape(); RingShape easternRingShape = new RingShape(); foreach (Vertex vertex in polygonShape.OuterRing.Vertices) { if (vertex.X > 0) { westernRingShape.Vertices.Add(offsetProjection.ConvertToExternalProjection(vertex.X, vertex.Y)); easternRingShape.Vertices.Add(vertex); } else { westernRingShape.Vertices.Add(vertex); easternRingShape.Vertices.Add(offsetProjection.ConvertToInternalProjection(vertex.X, vertex.Y)); } } offsetProjection.Close(); MultipolygonShape multiPolygonShape = new MultipolygonShape(); PolygonShape westernPolygonShape = new PolygonShape(); PolygonShape easternPolygonShape = new PolygonShape(); westernPolygonShape.OuterRing = westernRingShape; easternPolygonShape.OuterRing = easternRingShape; MultipolygonShape westernMultiPolygonShape = worldExtent.GetIntersection(westernPolygonShape); MultipolygonShape easternMultiPolygonShape = worldExtent.GetIntersection(easternPolygonShape); foreach (PolygonShape polygonShape1 in westernMultiPolygonShape.Polygons) { multiPolygonShape.Polygons.Add(polygonShape1); } foreach (PolygonShape polygonShape1 in easternMultiPolygonShape.Polygons) { multiPolygonShape.Polygons.Add(polygonShape1); } return multiPolygonShape; } 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); } } }