Table of Contents

Source Code ServicesEditionSample IntersectingMultiPolygon CS 100112.zip

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace SelfIntersectingMultiPolygonShape  
 {  
     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());  
         }  
     }  
 }  
 

TestForm.cs

 using System;  
 using System.Collections.ObjectModel;  
 using System.Drawing;  
 using System.IO;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace SelfIntersectingMultiPolygonShape  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
 
         private StreamReader polygonWKT1 = new StreamReader(@"..\..\data\Polygon1.txt");  
         private StreamReader polygonWKT2 = new StreamReader(@"..\..\data\Polygon2.txt");  
         private StreamReader polygonWKT3 = new StreamReader(@"..\..\data\Polygon3.txt");  
         private StreamReader polygonWKT4 = new StreamReader(@"..\..\data\Polygon4.txt");  
         private StreamReader polygonWKT5 = new StreamReader(@"..\..\data\Polygon5.txt");  
         private StreamReader polygonWKT6 = new StreamReader(@"..\..\data\Polygon6.txt");  
 
         public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             // Set the extent and the background color  
             mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(12.20,48.03,17.46,45.07), Map.Width, Map.Height);  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             //Creates the MultipolygonShape with overlapping PolygonShapes.  
             PolygonShape polygonShape1 = new PolygonShape(polygonWKT1.ReadLine());  
             PolygonShape polygonShape2 = new PolygonShape(polygonWKT2.ReadLine());  
             PolygonShape polygonShape3 = new PolygonShape(polygonWKT3.ReadLine());  
             PolygonShape polygonShape4 = new PolygonShape(polygonWKT4.ReadLine());  
             PolygonShape polygonShape5 = new PolygonShape(polygonWKT5.ReadLine());  
             PolygonShape polygonShape6 = new PolygonShape(polygonWKT6.ReadLine());  
 
             MultipolygonShape multiPolygonShape = new MultipolygonShape();  
             multiPolygonShape.Polygons.Add(polygonShape1);  
             multiPolygonShape.Polygons.Add(polygonShape2);  
             multiPolygonShape.Polygons.Add(polygonShape3);  
             multiPolygonShape.Polygons.Add(polygonShape4);  
             multiPolygonShape.Polygons.Add(polygonShape5);  
             multiPolygonShape.Polygons.Add(polygonShape6);  
 
             InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Green), GeoColor.StandardColors.Black);  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
 
             inMemoryFeatureLayer.InternalFeatures.Add(new Feature(multiPolygonShape));  
 
             mapEngine.StaticLayers.Add("Layer", inMemoryFeatureLayer);  
 
             DrawImage();  
         }  
 
         //Here we create a collection of PolygonShapes from a MultipolygonShape if it has PolygonShapes that intersect or overlap each other.  
         //This is one approach to avoid having an invalid MultipolygonShape with overlapping parts.  
         private void btnPolygonCollection_Click(object sender, EventArgs e)  
         {  
             InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)mapEngine.StaticLayers["Layer"];  
             MultipolygonShape multiPolygonShape = (MultipolygonShape)inMemoryFeatureLayer.InternalFeatures[0].GetShape();  
 
             if (IsMultiPolygonShapeSelfIntersected(multiPolygonShape) == true)  
             {  
                 Collection<PolygonShape> polygonShapes = GetPolygonShapeCollection(multiPolygonShape);  
                 inMemoryFeatureLayer.InternalFeatures.Clear();  
 
                 foreach (PolygonShape polygonShape in polygonShapes)  
                 {  
                     inMemoryFeatureLayer.InternalFeatures.Add(new Feature(polygonShape));  
                 }  
 
                 DrawImage();  
                 btnMultiPolygonShape.Enabled = false;  
             }  
         }  
 
         //Here we create a new MultipolygonShape from the invalid MultipolygonShape with overlapping (intersecting) PolygonShapes.  
         //The overlapping parts are merged together (Union geometry operation).  
         //This is another approach to avoid having an invalid MultipolygonShape with overlapping parts.  
         private void btnMultiPolygonShape_Click(object sender, EventArgs e)  
         {  
             InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)mapEngine.StaticLayers["Layer"];  
             MultipolygonShape multiPolygonShape = (MultipolygonShape)inMemoryFeatureLayer.InternalFeatures[0].GetShape();  
 
             if (IsMultiPolygonShapeSelfIntersected(multiPolygonShape) == true)  
             {  
                 MultipolygonShape correctedPolygonshape = GetCorrectedMultiPolygonShape(multiPolygonShape);  
 
                 inMemoryFeatureLayer.InternalFeatures.Clear();  
                 inMemoryFeatureLayer.InternalFeatures.Add(new Feature(correctedPolygonshape));  
 
                 DrawImage();  
 
                 btnPolygonCollection.Enabled = false;  
             }  
         }  
 
         //Function to get a collection of PolygonShape from a MultipolygonShape.  
         private Collection<PolygonShape> GetPolygonShapeCollection(MultipolygonShape multiPolygonShape)  
         {  
             Collection<PolygonShape> polygonShapes = new Collection<PolygonShape>();  
 
             foreach (PolygonShape polygonShape in multiPolygonShape.Polygons)  
             {  
                 polygonShapes.Add(polygonShape);  
             }  
 
             return polygonShapes;  
         }  
 
         //Function that gets a new MultipolygonShape from merging (unioning) the intersecting parts.  
         private MultipolygonShape GetCorrectedMultiPolygonShape(MultipolygonShape multiPolygonShape)  
         {  
             Collection<AreaBaseShape> AreaBaseShapes = new Collection<AreaBaseShape>();  
 
             foreach (PolygonShape polygonShape in multiPolygonShape.Polygons)  
             {  
                 AreaBaseShapes.Add(polygonShape);  
             }  
 
             MultipolygonShape correctedMultiPolygonShape = PolygonShape.Union(AreaBaseShapes);  
             return correctedMultiPolygonShape;  
         }  
 
         //Determines if any  PolygonShape making up the MultiPolygonShape overlap (intersect) each other.  
         private bool IsMultiPolygonShapeSelfIntersected(MultipolygonShape multiPolygonShape)  
         {  
             bool result = false;  
             int i, j;  
             for (i = 0; i <= multiPolygonShape.Polygons.Count - 1; i += 1)  
             {  
                 PolygonShape polygonShape = multiPolygonShape.Polygons[i];  
 
                 for (j = i + 1; j <= multiPolygonShape.Polygons.Count - 1; j += 1)  
                 {  
                     PolygonShape polygonShape2 = multiPolygonShape.Polygons[j];  
                     if (polygonShape.Intersects(polygonShape2))  
                     {  
                         result = true;  
                         break;  
                     }  
                 }  
                 if (result == true) break;  
             }  
             return result;  
         }  
 
 
         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);  
         }  
     }  
 }