using System;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
namespace IntersectingTwoLayers
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
ShapeFileFeatureLayer countyLayer = null;
ShapeFileFeatureLayer stratLayer = null;
ShapeFileFeatureLayer intersectLayer = 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(1181989,837689,2113317,224351), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.StandardColors.LightGoldenrodYellow);
// Add the static layers to the MapEngine
stratLayer = new ShapeFileFeatureLayer(@"..\..\Data\stratigraphy.shp", ShapeFileReadWriteMode.ReadOnly);
stratLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.LightGreen, GeoColor.StandardColors.Black);
stratLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("StratigraphyLayer", stratLayer);
countyLayer = new ShapeFileFeatureLayer(@"..\..\Data\panhandle_counties.shp", ShapeFileReadWriteMode.ReadOnly);
countyLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Transparent, GeoColor.StandardColors.Brown, 2, LineDashStyle.Solid, 0, 0);
countyLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("CountiesLayer", countyLayer);
DrawImage();
}
private void btnIntersect_Click(object sender, EventArgs e)
{
//deletes the files for the intersected shapefile if they already exist.
if (File.Exists(@"..\..\Data\stratigraphyintersect.shp"))
{
DeleteIntersectFiles();
}
//Calls the IntersectShapefiles function to get the intersected layer.
ShapeFileFeatureLayer intersectLayer = IntersectShapeFiles(stratLayer, countyLayer, @"..\..\Data\stratigraphyintersect.shp");
//Adds the shapefiles to the mapEngine.
mapEngine.StaticLayers.Remove("CountiesLayer");
mapEngine.StaticLayers.Remove("StratigraphyLayer");
//Adds the new intersected layer frm stratigraphy and counties.
ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\stratigraphyintersect.shp", BuildIndexMode.DoNotRebuild);
intersectLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.LightGreen, GeoColor.StandardColors.DarkGreen);
intersectLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("IntersectStratigraphyLayer", intersectLayer);
DrawImage();
btnIntersect.Enabled = false;
}
private ShapeFileFeatureLayer IntersectShapeFiles(ShapeFileFeatureLayer InputShapeFileFeatureLayer, ShapeFileFeatureLayer IntersectingShapeFileFeatureLayer, string outputShapeFileFeatureLayerPath)
{
//Gets the columns from the Input shapefile (stratigraphy) and the intersecting shapefile (counties).
InputShapeFileFeatureLayer.Open();
IntersectingShapeFileFeatureLayer.Open();
Collection<DbfColumn> inputDbfColumns = ((ShapeFileFeatureSource)InputShapeFileFeatureLayer.FeatureSource).GetDbfColumns();
Collection<DbfColumn> intersectingDbfColumns = ((ShapeFileFeatureSource)IntersectingShapeFileFeatureLayer.FeatureSource).GetDbfColumns();
//Adds all the columns from both shapefiles for the resulting shapefile.
Collection<DbfColumn> totalColumns = new Collection<DbfColumn>();
foreach (DbfColumn dbfColumn in inputDbfColumns)
{
totalColumns.Add(dbfColumn);
}
foreach (DbfColumn dbfColumn in intersectingDbfColumns)
{
totalColumns.Add(dbfColumn);
}
//Creates the new shapefile that is the result of the intersection of the input shapefile and the intersecting shapefile.
ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polygon, outputShapeFileFeatureLayerPath, totalColumns);
//Creates the new ShapeFileFeatureSource for the intersected data.
ShapeFileFeatureSource intersectShapeFileFeatureSource = new ShapeFileFeatureSource(outputShapeFileFeatureLayerPath, ShapeFileReadWriteMode.ReadWrite);
intersectShapeFileFeatureSource.Open();
intersectShapeFileFeatureSource.BeginTransaction();
//Loops thru the features of the intersecting shapefiles.
Collection<Feature> intersectingFeatures = IntersectingShapeFileFeatureLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
foreach (Feature intersectingFeature in intersectingFeatures)
{
//Gets the features of the input shapefile to be intersected.
MultipolygonShape intersectingMultiPolygonShape = (MultipolygonShape) intersectingFeature.GetShape();
Collection<Feature> intersectedFeatures = InputShapeFileFeatureLayer.QueryTools.GetFeaturesIntersecting(intersectingMultiPolygonShape, ReturningColumnsType.AllColumns);
//Loops thru the features of the input shapefile to be intersected.
foreach (Feature intersectedFeature in intersectedFeatures)
{
//Gets the MultipolygonShape as the result of the intersection.
MultipolygonShape intersectedMultipolygonShape = (MultipolygonShape) intersectedFeature.GetShape();
MultipolygonShape overlapMultipolygonShape = intersectingMultiPolygonShape.GetIntersection(intersectedMultipolygonShape);
//Gets the column values from both the input and the intersecting shapefile.
Dictionary<string, string> Columns = new Dictionary<string, string>();
foreach (DbfColumn dbfColumn in inputDbfColumns)
{
Columns.Add(dbfColumn.ColumnName, intersectedFeature.ColumnValues[dbfColumn.ColumnName]);
}
foreach (DbfColumn dbfColumn in intersectingDbfColumns)
{
Columns.Add(dbfColumn.ColumnName, intersectingFeature.ColumnValues[dbfColumn.ColumnName]);
}
//Adds the new feature with the intersected MultipolygonShape and the columns from both the input and the intersecting shapefiles.
Feature newFeature = new Feature(overlapMultipolygonShape, Columns);
intersectShapeFileFeatureSource.AddFeature(newFeature);
}
}
intersectShapeFileFeatureSource.CommitTransaction();
intersectShapeFileFeatureSource.Close();
InputShapeFileFeatureLayer.Close();
IntersectingShapeFileFeatureLayer.Close();
//Returns the intersected ShapeFileFeatureLayer.
ShapeFileFeatureLayer clippedShapeFileFeatureLayer = new ShapeFileFeatureLayer(outputShapeFileFeatureLayerPath);
return clippedShapeFileFeatureLayer;
}
private void DeleteIntersectFiles()
{
File.Delete(@"..\..\Data\stratigraphyintersect.shp");
File.Delete(@"..\..\Data\stratigraphyintersect.ids");
File.Delete(@"..\..\Data\stratigraphyintersect.idx");
File.Delete(@"..\..\Data\stratigraphyintersect.dbf");
File.Delete(@"..\..\Data\stratigraphyintersect.shx");
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.Feet);
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();
}
}
}