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 Dissolve { 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.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Drawing; using System.IO; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; namespace Dissolve { 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 full extent and the background color mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-128,52,-63,20), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); // Add the states shapefile to the MapEngine LoadStateShapeFile(); DrawImage(); } private void btnDissolve_Click(object sender, EventArgs e) { //deletes the files for the dissolved shapefile if they already exist. if (File.Exists(@"..\..\Data\USregions.shp")) { DeleteDissolveFiles(); } //Creates the dissolved shapefile to the mapEngine. ShapeFileFeatureLayer dissolveLayer = DissolveShapeFile((ShapeFileFeatureLayer)mapEngine.StaticLayers[0], "sub_region", @"..\..\Data\USRegions.shp"); //Adds the dissolved shapefile to the mapEngine. mapEngine.StaticLayers.Clear(); ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\USRegions.shp", BuildIndexMode.DoNotRebuild); dissolveLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1; dissolveLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("USregionLayer", dissolveLayer); DrawImage(); btnDissolve.Enabled = false; } private ShapeFileFeatureLayer DissolveShapeFile(ShapeFileFeatureLayer shapeFileFeatureLayer, string column, string DissolvedShapeFilePath) { shapeFileFeatureLayer.Open(); //Gets all the distinct values for the column the dissolve is going to be based on. DataTable tempDataTable = shapeFileFeatureLayer.FeatureSource.ExecuteQuery("Select distinct " + column + " from " + Path.GetFileName(shapeFileFeatureLayer.ShapePathFileName)); //Creates the dissolve shapefile with the collum the dissolve is going to be based on. DbfColumn NameColumn = new DbfColumn(column, DbfColumnType.String, 30, 0); ShapeFileFeatureSource.CreateShapeFile(shapeFileFeatureLayer.GetShapeFileType(), DissolvedShapeFilePath, new DbfColumn[] { NameColumn }); ShapeFileFeatureSource dissolveFeatureSource = new ShapeFileFeatureSource(DissolvedShapeFilePath, ShapeFileReadWriteMode.ReadWrite); dissolveFeatureSource.Open(); dissolveFeatureSource.BeginTransaction(); foreach (DataRow dataRow in tempDataTable.Rows) { Collection<AreaBaseShape> polygonShapes = new Collection<AreaBaseShape>(); Collection<Feature> features = shapeFileFeatureLayer.FeatureSource.GetFeaturesByColumnValue(column, System.Convert.ToString(dataRow[0])); //Loops thru all the features with the same column value and add their shape to the polygonShapes collection. foreach (Feature feature in features) { AreaBaseShape polygonShape = (AreaBaseShape)feature.GetShape(); polygonShapes.Add(polygonShape); } //Uses the PolygonShape.Union static method to union all the shapes in one step. MultipolygonShape multiPolygonShape = PolygonShape.Union(polygonShapes); //Adds the resulting feature from the dissolve operation to the dissolve shapefile. Dictionary<string, string> Columns = new Dictionary<string, string>(); Columns.Add(column, System.Convert.ToString(dataRow[0])); Feature newFeature = new Feature(multiPolygonShape, Columns); dissolveFeatureSource.AddFeature(newFeature); } //Commits and closes the open resources. dissolveFeatureSource.CommitTransaction(); dissolveFeatureSource.Close(); shapeFileFeatureLayer.Close(); //Creates the dissolved shapefile to return. ShapeFileFeatureLayer dissolveShapeFile = new ShapeFileFeatureLayer(DissolvedShapeFilePath, ShapeFileReadWriteMode.ReadOnly); return dissolveShapeFile; } private void DeleteDissolveFiles() { File.Delete(@"..\..\Data\USregions.shp"); File.Delete(@"..\..\Data\USregions.ids"); File.Delete(@"..\..\Data\USregions.idx"); File.Delete(@"..\..\Data\USregions.dbf"); File.Delete(@"..\..\Data\USregions.shx"); } private void LoadStateShapeFile() { ShapeFileFeatureLayer stateLayer = new ShapeFileFeatureLayer(@"..\..\Data\States.shp", ShapeFileReadWriteMode.ReadOnly); stateLayer.Name = "States"; ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\States.shp", BuildIndexMode.DoNotRebuild); stateLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1; stateLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("StateLayer", stateLayer); } private void DrawImage() { if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(Map.Width, Map.Height); mapEngine.OpenAllLayers(); mapEngine.DrawStaticLayers(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(); } } }