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.Windows.Forms; namespace ClippingOnPointLayer { 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.Drawing; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; using System.IO; using System.Collections.Generic; using System.Collections.ObjectModel; namespace ClippingOnPointLayer { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; ShapeFileFeatureLayer worldLayer = null; ShapeFileFeatureLayer popLayer = null; public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { // Sets the full extent and the background color mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-92.65,19.02,-76.75,6.9), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); //Adds the static layers to the MapEngine //Adds the country 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 pop layer. popLayer = new ShapeFileFeatureLayer(@"..\..\Data\popcentralamerica.shp", ShapeFileReadWriteMode.ReadOnly); popLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City7; popLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("PopCentralAmericaLayer", popLayer); //Adds the InMemoryFeatureLayer with of the polygon of Nicaragua used for clipping. InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer(); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle (GeoColor.StandardColors.Transparent, GeoColor.StandardColors.Brown, 2); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; worldLayer.Open(); Collection<Feature> features = worldLayer.FeatureSource.GetFeaturesByColumnValue("cntry_name", "Nicaragua"); 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\popcentralamericaclip.shp")) { DeleteClipFiles(); } //Gets the MultipolygonShape of Nicaragua used for clipping. worldLayer.Open(); Collection<Feature> features = worldLayer.FeatureSource.GetFeaturesByColumnValue("cntry_name", "Nicaragua"); worldLayer.Close(); MultipolygonShape multiPolygonShape = (MultipolygonShape)features[0].GetShape(); //Calls the ClipShapeFile function to get the clipped layer. ShapeFileFeatureLayer clipLayer = ClipPointShapeFile(popLayer, @"..\..\Data\popcentralamericaclip.shp", multiPolygonShape); //Adds the clipped shapefile to the mapEngine. mapEngine.StaticLayers.Remove("PopCentralAmericaLayer"); //Adds the new clipped layer of pop. ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\popcentralamericaclip.shp", BuildIndexMode.DoNotRebuild); clipLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City7; clipLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("PopCentralAmericaClipLayer", clipLayer); DrawImage(); btnClip.Enabled = false; } private ShapeFileFeatureLayer ClipPointShapeFile(ShapeFileFeatureLayer shapeFileFeatureLayer, string outputShapeFileFeatureLayerPath, AreaBaseShape ClippingShape) { //Gets the columns from the point based shapefile. shapeFileFeatureLayer.Open(); Collection<DbfColumn> dbfColumns = ((ShapeFileFeatureSource)shapeFileFeatureLayer.FeatureSource).GetDbfColumns(); ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Point, outputShapeFileFeatureLayerPath, dbfColumns); ShapeFileFeatureSource clippedShapeFileFeatureSource = new ShapeFileFeatureSource(outputShapeFileFeatureLayerPath, ShapeFileReadWriteMode.ReadWrite); //Gets the features with Spatial Query that will be used for the clipping. Collection<Feature> features = shapeFileFeatureLayer.QueryTools.GetFeaturesWithin(ClippingShape, ReturningColumnsType.AllColumns); shapeFileFeatureLayer.Close(); //Loops thru all the features resulting from the spatial query to add to the new shapefile. clippedShapeFileFeatureSource.Open(); clippedShapeFileFeatureSource.BeginTransaction(); foreach (Feature feature in features) { clippedShapeFileFeatureSource.AddFeature(feature); } clippedShapeFileFeatureSource.CommitTransaction(); clippedShapeFileFeatureSource.Close(); //Returns the clipped ShapeFileFeatureLayer. ShapeFileFeatureLayer clippedShapeFileFeatureLayer = new ShapeFileFeatureLayer(outputShapeFileFeatureLayerPath); return clippedShapeFileFeatureLayer; } private void DeleteClipFiles() { File.Delete(@"..\..\Data\popcentralamericaclip.shp"); File.Delete(@"..\..\Data\popcentralamericaclip.ids"); File.Delete(@"..\..\Data\popcentralamericaclip.idx"); File.Delete(@"..\..\Data\popcentralamericaclip.dbf"); File.Delete(@"..\..\Data\popcentralamericaclip.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(); } } }