User Tools

Site Tools


source_code_serviceseditionsample_innerringcontainment_cs_100423.zip

Source Code ServicesEditionSample InnerRingContainment CS 100423.zip

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace InnerRingContainment  
 {  
     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.Windows.Forms;  
 using System.IO;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace InnerRingContainment  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
         enum ContainmentType { Outside, Fully_Inside, Inside_InnerRing };  
 
        public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             // Set the extent and the background color  
             mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-123.7339,38.9283,-123.6945,38.9064), 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();  
             mapEngine.StaticLayers.Add(worldMapKitLayer);  
 
             //InMemoryFeatureLayer for the polygon with two inner rings  
             InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(100, GeoColor.StandardColors.Red));  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
 
             mapEngine.DynamicLayers.Add(inMemoryFeatureLayer);  
 
             CreatePolygon();  
 
             DrawImage();  
         }  
 
         private void Map_MouseUp(object sender, MouseEventArgs e)  
         {  
             PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent,e.X,e.Y,Map.Width,Map.Height);  
             InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)mapEngine.DynamicLayers[0];  
             PolygonShape polygonShape = (PolygonShape)inMemoryFeatureLayer.InternalFeatures[0].GetShape();  
 
             ContainmentType containmentType = GetContainmentType(polygonShape, pointShape);  
 
             MessageBox.Show(containmentType.ToString());  
         }  
 
         private ContainmentType GetContainmentType(PolygonShape polygonShape, PointShape pointShape)  
         {  
             ContainmentType Result = ContainmentType.Outside;  
 
             if (polygonShape.Contains(pointShape) == true)  
             {  
                 Result = ContainmentType.Fully_Inside;  
             }  
             //If the result of Contains for the PolygonShape is false, the PointShape can be either completely outside or inside one of the inner rings.  
             else  
             {  
                 foreach (RingShape ringShape in polygonShape.InnerRings)  
                 {  
                     //Creates PolygonShape from the inner ring to check containment.  
                     PolygonShape innerPolygonShape = new PolygonShape(ringShape);  
                     if (innerPolygonShape.Contains(pointShape) == true)  
                     {  
                         Result = ContainmentType.Inside_InnerRing;  
                         break;  
                     }  
                 }  
             }  
             return Result;  
         }  
 
 
         private void CreatePolygon()  
         {  
             StreamReader[] streamReaders = new StreamReader[3];  
             streamReaders[0] = new StreamReader(@"..\..\Data\polygon1.txt");  
             streamReaders[1] = new StreamReader(@"..\..\Data\polygon3.txt");  
             streamReaders[2] = new StreamReader(@"..\..\Data\polygon2.txt");  
 
             InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)mapEngine.DynamicLayers[0];  
 
             inMemoryFeatureLayer.Open();  
             inMemoryFeatureLayer.EditTools.BeginTransaction();  
 
             //Builds the polygon based on the structure of the text files with X and Y comma separated.  
             PolygonShape polygonShape = new PolygonShape();  
             int i = 0;  
             while (i <= streamReaders.Length - 1)  
             {  
 
                 RingShape ringShape = new RingShape();  
                 string line = null;  
                 while (line != "")  
                 {  
                     line = streamReaders[i].ReadLine();  
                     if (line != null)  
                     {  
                         string[] strSplit = line.Split(',');  
                         ringShape.Vertices.Add(new Vertex(Convert.ToDouble(strSplit[0]), Convert.ToDouble(strSplit[1])));  
                     }  
                     else  
                     {  
                         break;  
                     }  
                 }  
                 //Remember to add the last vertex with the same X and Y as the first vertex to enclose completely the polygon.  
                 ringShape.Vertices.Add(new Vertex(ringShape.Vertices[0].X, ringShape.Vertices[0].Y));  
                 //The first ring is the outer ring the last two are the inner rings.  
                 if (i == 0)  
                 {  
                     polygonShape.OuterRing = ringShape;  
                 }  
                 else  
                 {  
                     polygonShape.InnerRings.Add(ringShape);  
                 }  
                 i = i + 1;  
             }  
             inMemoryFeatureLayer.EditTools.Add(new Feature(polygonShape));  
 
             inMemoryFeatureLayer.EditTools.CommitTransaction();  
             inMemoryFeatureLayer.Close();  
         }  
 
 
         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);  
         }  
 
 
     }  
 }  
 
source_code_serviceseditionsample_innerringcontainment_cs_100423.zip.txt · Last modified: 2015/09/08 05:33 by admin