User Tools

Site Tools


source_code_serviceseditionsample_negativebuffering_cs_091120.zip

Source Code ServicesEditionSample NegativeBuffering CS 091120.zip

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace NegativeBuffering  
 {  
     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 ThinkGeo.MapSuite.Core;  
 
 namespace NegativeBuffering  
 {  
     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.7533,30.3191,-97.7261,30.3032), 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);  
 
             //PolygonShape to buffer.  
             PolygonShape polygonShape = new PolygonShape();  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7325,30.3164));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7359,30.3151));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7428,30.3053));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7382,30.303));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7374,30.3088));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7329,30.3115));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7325, 30.3164));  
 
             InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(80, GeoColor.StandardColors.Red), GeoColor.StandardColors.Black);  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             inMemoryFeatureLayer.InternalFeatures.Add("Area", new Feature(polygonShape));  
 
             InMemoryFeatureLayer BufferInMemoryFeatureLayer = new InMemoryFeatureLayer();  
             BufferInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Yellow), GeoColor.StandardColors.Black);  
             BufferInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
 
             //Calls the BufferPolygon function to get the buffered polygon with a negative distance.  
             MultipolygonShape bufferMultiPolygonShape = NegativeBufferPolygon(polygonShape, GeographyUnit.DecimalDegree, 50, DistanceUnit.Meter);  
 
             BufferInMemoryFeatureLayer.InternalFeatures.Add("BufferArea", new Feature(bufferMultiPolygonShape));  
 
             mapEngine.DynamicLayers.Add("AreaLayer", inMemoryFeatureLayer);  
            mapEngine.DynamicLayers.Add("BufferAreaLayer", BufferInMemoryFeatureLayer);  
 
             DrawImage();  
         }  
 
         //Negative Buffer is done in three steps.  
         private MultipolygonShape NegativeBufferPolygon(PolygonShape polygonShape,GeographyUnit polygonShapeUnit, double distance, DistanceUnit distanceUnit)  
         {  
             // 1) Gets the perimeter of the polygon as a LineShape.  
             LineShape perimeterLineShape = GetPerimeter(polygonShape);  
             // 2) Buffers the perimeter by the set distance.  
             MultipolygonShape bufferPerimeterMultiPolygonShape = perimeterLineShape.Buffer(distance, polygonShapeUnit, distanceUnit);  
             // 3) Performs the geometric operation of getting the Difference of the buffered perimeter and the polygon  
             //to get the final buffer result.  
             MultipolygonShape bufferMultiPolygonShape = polygonShape.GetDifference(bufferPerimeterMultiPolygonShape);  
 
             return bufferMultiPolygonShape;  
         }  
 
         private LineShape GetPerimeter(PolygonShape polygonShape)  
         {  
             LineShape perimeterLineShape = new LineShape();  
             foreach (Vertex vertex in polygonShape.OuterRing.Vertices)  
             {  
                perimeterLineShape.Vertices.Add(new Vertex(vertex.X, vertex.Y));  
             }  
             return perimeterLineShape;  
         }  
 
 
         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_negativebuffering_cs_091120.zip.txt · Last modified: 2015/09/08 05:35 by admin