ArcGIS Add-in插件开发从0到1及实际案例分享

时间:2023-03-09 09:31:04
ArcGIS Add-in插件开发从0到1及实际案例分享

同学做毕设,要求我帮着写个ArcGIS插件,实现功能为:遍历所有图斑,提取相邻图斑的公共边长及其他属性(包括相邻图斑的ID),链接到属性表中。搞定后在这里做个记录。本文分两大部分:

  • ArcGIS插件开发流程
  • 实际案例分享

一、ArcGIS插件开发流程

该部分不涉及具体业务,力求以最快速度了解ArcGIS Add-in插件从开发到使用的具体流程。

1.新建项目

ArcGIS Add-in插件开发从0到1及实际案例分享

ArcGIS Add-in插件开发从0到1及实际案例分享

ArcGIS Add-in插件开发从0到1及实际案例分享

2.编写业务代码

ArcGIS Add-in插件开发从0到1及实际案例分享

3.编译

ArcGIS Add-in插件开发从0到1及实际案例分享

4.安装插件

ArcGIS Add-in插件开发从0到1及实际案例分享    ArcGIS Add-in插件开发从0到1及实际案例分享

5.使用插件

ArcGIS Add-in插件开发从0到1及实际案例分享

ArcGIS Add-in插件开发从0到1及实际案例分享

ArcGIS Add-in插件开发从0到1及实际案例分享

ArcGIS Add-in插件开发从0到1及实际案例分享

二、实际案例分享

上面已经说了,案例来源于实际的需求,此处想必没有比直接上代码更实用更有feel了。实现功能为:遍历所有图斑,提取相邻图斑的公共边长及其他属性(包括相邻图斑的ID),并保存到文本文件中。注释已经写的很详细了,所以具体过程也不多说,有啥问题直接留言,我会看到的~

 using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs; namespace SharedSide
{
public class SharedSide : ESRI.ArcGIS.Desktop.AddIns.Button
{
private IApplication m_application;
private static IMap map; public SharedSide()
{ } protected override void OnClick()
{
m_application = ArcMap.Application;
map = (m_application.Document as IMxDocument).FocusMap; FormSelect formSelect = new FormSelect();
IEnumLayer layers = map.get_Layers(null, false);
layers.Reset();
ILayer layer = layers.Next();
while (layer != null)
{
formSelect.cmbLayers.Items.Add(layer.Name);
layer = layers.Next();
}
formSelect.ShowDialog(); if (formSelect.IsOK)
{
ILayer selectedLayer = GetLayerByName(formSelect.cmbLayers.Text);
IFeatureLayer pFeatureLayer = selectedLayer as IFeatureLayer;
int featureCount = pFeatureLayer.FeatureClass.FeatureCount(null);
IFeatureCursor featureCursor = pFeatureLayer.Search(null, false);
IFeature pFeature = null; // 最大相邻图斑数
int maxNumAdajacency = ;
// 具有相邻图斑的要素
List<IFeature> featuresHasAdjacency = new List<IFeature>();
// 对应featureHasAdjacency的相邻图斑
List<List<IFeature>> adjacentFeatures = new List<List<IFeature>>();
// 对应adjacentFeatures的公共边长度
List<List<double>> adjacentLengths = new List<List<double>>(); // 提取边相邻的相邻图斑并计算公共边长度
while ((pFeature = featureCursor.NextFeature()) != null)
{
// 与pFeature相邻的图斑
List<IFeature> adjacentFeature = AdjacentPolygons(pFeature, pFeatureLayer);
// pFeature与adjacentFeature的公共边长度
List<double> adjacentLength = new List<double>(); // 计算公共边长度并去掉只有公共点相邻的图斑
if (adjacentFeature.Count > )
{
for (int i = ; i < adjacentFeature.Count; i++)
{
double length = LengthOfSide((pFeature.Shape as IPolygon), (adjacentFeature[i].Shape as IPolygon));
if (length == )
{// 如果只有公共点相邻,则移除
adjacentFeature.Remove(adjacentFeature[i]);
}
} for (int i = ; i < adjacentFeature.Count; i++)
{
double length = LengthOfSide((pFeature.Shape as IPolygon), (adjacentFeature[i].Shape as IPolygon));
adjacentLength.Add(length);
}
} if (adjacentFeature.Count > )
{// 如果去掉只有公共点相邻的情况pFeature仍有图斑与之相邻
featuresHasAdjacency.Add(pFeature);
adjacentFeatures.Add(adjacentFeature);
adjacentLengths.Add(adjacentLength);
} // 3.查找最多相邻图斑数
if (adjacentFeature.Count > maxNumAdajacency)
{
maxNumAdajacency = adjacentFeature.Count;
}
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor); // 将相邻图斑的公共边长度及DLBM属性写入文本文件保存
string text = "OBJECTID";
for (int i = ; i < maxNumAdajacency; i++)
{
string str = (",相邻" + (i + ) + "-OBJECTID") + (",相邻" + (i + ) + "-公共边长") + (",相邻" + (i + ) + "-DLBM");
text += str;
} WriteData(formSelect.txtPath.Text, text); int n = featuresHasAdjacency.Count;
for (int i = ; i < n; i++)
{
int nIndex = featuresHasAdjacency[i].Table.FindField("OBJECTID");
string str = featuresHasAdjacency[i].get_Value(nIndex).ToString();
int m = adjacentFeatures[i].Count;
for (int j = ; j < m; j++)
{
str += "," + adjacentFeatures[i][j].get_Value(adjacentFeatures[i][j].Table.FindField("OBJECTID")).ToString();
str += "," + adjacentLengths[i][j].ToString();
str += "," + adjacentFeatures[i][j].get_Value(adjacentFeatures[i][j].Table.FindField("DLBM")).ToString();
}
WriteData(formSelect.txtPath.Text, str);
}
MessageBox.Show("计算完成!");
}
} protected override void OnUpdate()
{
Enabled = ArcMap.Application != null;
} // 通过图层名称查找指定图层
public static ILayer GetLayerByName(string lyrName)
{
ILayer findLayer = null;
IEnumLayer pEnumLayer = map.get_Layers();
pEnumLayer.Reset();
ILayer pLayer = pEnumLayer.Next();
while (pLayer != null)
{
if (pLayer.Name == lyrName)
{
findLayer = pLayer;
}
pLayer = pEnumLayer.Next();
}
return findLayer;
} // 判断线是否为面的边界
public static bool isBoundary(IPolyline iPolyline, IPolygon iPolygon)
{
bool isBoundary;
ITopologicalOperator topoOper = iPolygon as ITopologicalOperator;
IPolyline boundLine = topoOper.Boundary as IPolyline;
IRelationalOperator reltOper = iPolyline as IRelationalOperator;
isBoundary = reltOper.Overlaps(boundLine);
return isBoundary;
} // 查找当前图层中与某图斑相邻的其他图斑(包括有公共边的和公共点的)
public static List<IFeature> AdjacentPolygons(IFeature iFeature, IFeatureLayer featureLayer)
{
List<IFeature> listFeature = new List<IFeature>();
IRelationalOperator reltOperator = iFeature.Shape as IRelationalOperator;
int featureCount = featureLayer.FeatureClass.FeatureCount(null);
IFeatureCursor featureCursor = featureLayer.Search(null, false);
IFeature feature = null;
while ((feature = featureCursor.NextFeature()) != null)
{
if (feature.OID == iFeature.OID)
{
continue;
}
bool isAdjacent = reltOperator.Touches(feature.Shape);
if (isAdjacent)
{
listFeature.Add(feature);
}
}
return listFeature;
} // 计算两个图斑公共边的长度
public static double LengthOfSide(IPolygon iPolygon1, IPolygon iPolygon2)
{
IPolyline polyline;
ITopologicalOperator topoOper = iPolygon1 as ITopologicalOperator;
polyline = topoOper.Intersect(iPolygon2, esriGeometryDimension.esriGeometry1Dimension) as IPolyline;
return polyline.Length;
} // 写入数据到文件
private static void WriteData(string filePath, string text)
{
FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(text);
sw.Close();
fs.Close();
}
}
}