Hi,
I have written a simple tool that can be used to find intersections between two polygon featuresets.I think it would be nice to share it
-It will add a new column which shows the area of intersection,
-Main language is in Persian.You can change strings in your own language
```
// *******************************************************************************************************
// Product: InterSectionTool
// Description: Tool that Find polygons that has intersects in two featureclass
// Copyright & License: See www.DotSpatial.org.
// Contributor(s): Open source contributors may list themselves and their modifications here.
// Contribution of code constitutes transferral of copyright from authors to DotSpatial copyright holders.
//--------------------------------------------------------------------------------------------------------
// Name | Date | Comments
//--------------------|--------------------|--------------------------------------------------------------
// Majid Hojati | 9/15/2014 | First Release
// ********************************************************************************************************
using DotSpatial.Data;
using DotSpatial.Modeling.Forms;
using DotSpatial.Topology;
using System.Collections.Generic;
namespace LandManagment
{
/// <summary>
/// Clip With Polygon
/// </summary>
public class InterSectionTool : Tool
{
#region Constants and Fields
private Parameter[] _inputParam;
private Parameter[] _outputParam;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the ClipRasterWithPolygon class.
/// </summary>
public InterSectionTool()
{
this.Name = "ابزار یافتن همپوشانی بین دو لایه";
this.Category = "تحلیل های مکانی";
this.Description = "به کمک این ابزار میتوانید همپوشانی بین دو لایه انتخاب را پیدا کنید";
this.ToolTip = "یافتن همپوشانی بین دو لایه";
}
#endregion
#region Public Properties
/// <summary>
/// Gets or Sets the input paramater array
/// </summary>
public override Parameter[] InputParameters
{
get
{
return _inputParam;
}
}
/// <summary>
/// Gets or Sets the output paramater array
/// </summary>
public override Parameter[] OutputParameters
{
get
{
return _outputParam;
}
}
#endregion
#region Public Methods
/// <summary>
/// Once the Parameter have been configured the Execute command can be called, it returns true if succesful
/// </summary>
public override bool Execute(ICancelProgressHandler cancelProgressHandler)
{
IFeatureSet polygon0 = _inputParam[0].Value as IFeatureSet;
IFeatureSet polygon1 = _inputParam[1].Value as IFeatureSet;
IFeatureSet output = _outputParam[0].Value as IFeatureSet;
// Validates the input and output data
if (polygon0 == null || polygon1 == null || output == null)
{
return false;
}
output.CopyTableSchema(polygon0.DataTable);
output.DataTable.Columns.Add("میزان همپوشانی");
if (cancelProgressHandler != null)
cancelProgressHandler.Progress(null, 16, "آغاز به کار");
ProgressMeter pm = new ProgressMeter(cancelProgressHandler, "یافتن همپوشانی ها",polygon0.Features.Count);
pm.StepPercent = 1;
pm.StartValue = polygon0.Features.Count+1;
int c = 0;
for (int i = 0; i < polygon0.Features.Count; i++)
{
IFeature PolygonFeat1=polygon0.Features[i];
pm.StartValue = polygon0.Features.IndexOf(PolygonFeat1);
Extent tolerant = PolygonFeat1.Envelope.ToExtent();
List<IFeature> resultS =polygon1.Select(tolerant);
if (resultS != null && resultS.Count != 0)
{
foreach (IFeature PolygonFeat2 in polygon1.Features)
{
bool basicCheck;
basicCheck = PolygonFeat1.Envelope.Intersects(PolygonFeat2.Envelope);
if (basicCheck)
{
bool Check1 = PolygonFeat1.Intersects(PolygonFeat2);
if (Check1)
{
IFeature intersectedF = null;
try
{
intersectedF = PolygonFeat2.Intersection(PolygonFeat1);
output.AddFeature(PolygonFeat1);
output.Features[c].CopyAttributes(PolygonFeat1);
output.Features[c].DataRow[19] = intersectedF.Area();
c=c+1;
// output.DataTable.ImportRow(polygon0.DataTable.Rows[i]);
}
catch { return false; }
}
}
}
}
else
{
output.Save();
}
output.Save();
}
return true;
}
/// <summary>
/// The Parameter array should be populated with default values here
/// </summary>
public override void Initialize()
{
_inputParam = new Parameter[2];
_inputParam[0] = new PolygonFeatureSetParam("لایه در صفحه ای نخست") { HelpText = "این فایل به عنوان لایه صفحه ای اولیه مورد استفاده قرار میگیرد" };
_inputParam[1] = new PolygonFeatureSetParam("لایه در صفحه ای دوم")
{
HelpText = "این فایل به عنوان لایه صفحه ای اولیه مورد استفاده قرار میگیرد"
};
_outputParam = new Parameter[1];
_outputParam[0] = new PolygonFeatureSetParam("محل ذخیره فایل خروجی") { HelpText = "این فایل شامل مواردی است که دارای همپوشانی است"};
}
#endregion
}
}
```
Comments: ** Comment from web user: am2 **
This code is written to be used as a tool.Here is an example of using this tool
```
toolToExecute=new InterSectionTool();
Extent ex = new Extent(-180, -90, 180, 90);
List<DataSetArray> dataSets = new List<DataSetArray>();
if (legend1 != null)
{
for (int i = 0; i < legend1.RootNodes.Count; i++)
{
dataSets.AddRange(populateDataSets(legend1.RootNodes[i] as IGroup));
}
}
// DotSpatial.Tools.InverseDistanceWeighting IDW = new DotSpatial.Tools.InverseDistanceWeighting();
// it wasn't a category?
if (legend1 != null)
{
IMapFrame mf = legend1.RootNodes[0] as IMapFrame;
if (mf != null) ex = mf.ViewExtents;
}
ToolDialog td;
toolToExecute.Initialize();
td = new ToolDialog(toolToExecute, dataSets, ex);
td.RightToLeft = RightToLeft.Yes;
td.RightToLeftLayout = true;
td.Font = new Font("Tahoma", 8.25F);
DialogResult tdResult = td.ShowDialog(this);
while (tdResult == DialogResult.OK && td.ToolStatus != ToolStatus.Ok)
{
MessageBox.Show("لطفا تمامی فیلدهای مورد نیاز را تکمیل کنید");
tdResult = td.ShowDialog(this);
}
if (tdResult == DialogResult.OK && td.ToolStatus == ToolStatus.Ok)
{
//This fires when the user clicks the "OK" button on a tool dialog
//First we create the progress form
ToolProgress progForm = new ToolProgress(1);
progForm.RightToLeft = RightToLeft.Yes;
progForm.RightToLeftLayout = true;
progForm.Text = "فرآیند اجرای ابزار";
progForm.Font = new Font("Tahoma", 8.25F);
//We create a background worker thread to execute the tool
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += BwDoWork;
bw.RunWorkerCompleted += executionComplete;
object[] threadParameter = new object[2];
threadParameter[0] = toolToExecute;
threadParameter[1] = progForm;
// Show the progress dialog and kick off the Async thread
progForm.Show(this);
if (!bw.IsBusy)
{
bw.RunWorkerAsync(threadParameter);
}
}
here is background worker
private static void BwDoWork(object sender, DoWorkEventArgs e)
{
object[] threadParameter = e.Argument as object[];
if (threadParameter == null) return;
ITool toolToExecute = threadParameter[0] as ITool;
ToolProgress progForm = threadParameter[1] as ToolProgress;
if (progForm == null) return;
if (toolToExecute == null) return;
progForm.Progress(String.Empty, 0, "==================");
progForm.Progress(String.Empty, 0, String.Format("اجرا ابزار: {0}", toolToExecute.Name));
progForm.Progress(String.Empty, 0, "==================");
bool result = false;
try
{
result = toolToExecute.Execute(progForm);
}
catch (Exception ex)
{
progForm.Progress(String.Empty, 100, "خطا: " + ex);
}
e.Result = result;
progForm.ExecutionComplete();
progForm.Progress(String.Empty, 100, "==================");
progForm.Progress(String.Empty, 100, String.Format("پایان اجرای ابزار: {0}", toolToExecute.Name));
progForm.Progress(String.Empty, 100, "==================");
}
```