====== Source Code ServicesEditionSample Clipping CS 090929.zip ====== ====Program.cs==== using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Clipping { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } } } ====TestForm.cs==== using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.IO; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; namespace Clipping { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; ShapeFileFeatureLayer worldLayer = null; ShapeFileFeatureLayer evergreenLayer = null; public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { // Set the full extent and the background color mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-99.26,16.29,-26.37,-61.48), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); // Adds the static layers to the MapEngine //Adds the countries background layer. worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\Countries02.shp", ShapeFileReadWriteMode.ReadOnly); worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1; worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("WorldLayer", worldLayer); // Adds the evergreen layer to be clipped. evergreenLayer = new ShapeFileFeatureLayer(@"..\..\Data\evergreen.shp", ShapeFileReadWriteMode.ReadOnly); evergreenLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.LightGreen, GeoColor.StandardColors.DarkGreen); evergreenLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("EverGreenLayer", evergreenLayer); //Adds the InMemoryFeatureLayer with of the polygon of Brazil used for clipping. InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer(); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle (GeoColor.StandardColors.Transparent, GeoColor.StandardColors.Black, 1); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; worldLayer.Open(); Collection features = worldLayer.FeatureSource.GetFeaturesByColumnValue("cntry_name","Brazil"); worldLayer.Close(); inMemoryFeatureLayer.InternalFeatures.Add(features[0]); mapEngine.DynamicLayers.Add(inMemoryFeatureLayer); DrawImage(); } private void btnClip_Click(object sender, EventArgs e) { //deletes the files for the clipped shapefile if they already exist. if (File.Exists(@"..\..\Data\evergreenclip.shp")) { DeleteClipFiles(); } //Gets the MultipolygonShape of Brazil used for clipping. worldLayer.Open(); Collection features = worldLayer.FeatureSource.GetFeaturesByColumnValue("cntry_name", "Brazil"); worldLayer.Close(); MultipolygonShape multiPolygonShape = (MultipolygonShape)features[0].GetShape(); //Calls the ClipShapeFile function to get the clipped layer. ShapeFileFeatureLayer clipLayer = ClipShapeFile(evergreenLayer, @"..\..\Data\evergreenclip.shp", multiPolygonShape); //Adds the clipped shapefile to the mapEngine. mapEngine.StaticLayers.Remove("EverGreenLayer"); //Adds the new clipped layer of evergreeen. ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\evergreenclip.shp", BuildIndexMode.DoNotRebuild); clipLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.LightGreen, GeoColor.StandardColors.DarkGreen); clipLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("EverGreenClipLayer", clipLayer); DrawImage(); btnClip.Enabled = false; } private ShapeFileFeatureLayer ClipShapeFile(ShapeFileFeatureLayer shapeFileFeatureLayer, string outputShapeFileFeatureLayerPath, AreaBaseShape ClippingShape) { //Gets the columns from the polygon based shapefile. shapeFileFeatureLayer.Open(); Collection dbfColumns = ((ShapeFileFeatureSource)shapeFileFeatureLayer.FeatureSource).GetDbfColumns(); ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polygon, outputShapeFileFeatureLayerPath, dbfColumns); ShapeFileFeatureSource clippedShapeFileFeatureSource = new ShapeFileFeatureSource(outputShapeFileFeatureLayerPath, ShapeFileReadWriteMode.ReadWrite); //Gets the features with Spatial Query that will be used for the clipping. Collection features = shapeFileFeatureLayer.QueryTools.GetFeaturesIntersecting(ClippingShape, ReturningColumnsType.AllColumns); shapeFileFeatureLayer.Close(); //Loops thru all the features from the Spatial Query. clippedShapeFileFeatureSource.Open(); clippedShapeFileFeatureSource.BeginTransaction(); foreach (Feature feature in features) { //Gets the MultipolygonShape and clip it using GetIntersection geometric function. MultipolygonShape currentMultiPolygonShape = (MultipolygonShape)(feature.GetShape()); MultipolygonShape overlapMultiPolygonShape = currentMultiPolygonShape.GetIntersection(ClippingShape); //Adds the feature to the clipped layer with the clipped Multipolygon with its ColumnValues. Dictionary Columns = feature.ColumnValues; Feature clippedFeature = new Feature(overlapMultiPolygonShape, Columns); clippedShapeFileFeatureSource.AddFeature(clippedFeature); } clippedShapeFileFeatureSource.CommitTransaction(); clippedShapeFileFeatureSource.Close(); //Returns the clipped ShapeFileFeatureLayer. ShapeFileFeatureLayer clippedShapeFileFeatureLayer = new ShapeFileFeatureLayer(outputShapeFileFeatureLayerPath); return clippedShapeFileFeatureLayer; } private void DeleteClipFiles() { File.Delete(@"..\..\Data\evergreenclip.shp"); File.Delete(@"..\..\Data\evergreenclip.ids"); File.Delete(@"..\..\Data\evergreenclip.idx"); File.Delete(@"..\..\Data\evergreenclip.dbf"); File.Delete(@"..\..\Data\evergreenclip.shx"); } 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(); } } }