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

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

一、ArcGIS插件开发流程

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

1.新建项目

2.编写业务代码

3.编译

4.安装插件

    

5.使用插件

二、实际案例分享

上面已经说了,案例来源于实际的需求,此处想必没有比直接上代码更实用更有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();
}
}
}

ArcGIS Add-in插件开发从0到1及实际案例分享的更多相关文章

  1. ArcGIS API for JavaScript 4.0(一)

    原文:ArcGIS API for JavaScript 4.0(一) 最近ArcGIS推出了ArcGIS API for JavaScript 4.0,支持无插件3D显示,而且比较Unity和Sky ...

  2. js面向对象插件的做法框架new goBuy('.cakeItem',{ add:'.add', reduce:'.reduce' },[1,0.7,0.6]);

    /*弹窗购买蛋糕*/;(function(){ var $DialogBg=$(".Dialogbg-Select"); var $Dialog=$(".Dialog-S ...

  3. arcengine帮助http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/what_s_new_for_developers_at_10_/0001000002zp000000/

    http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/what_s_new_for_develope ...

  4. arcgis中nodata设为0及其小技巧

    一.arcgis中nodata设为0 两个栅格进行叠加,有时会有一部分没有数据,即用identify点击该区域,Value为NoDat a,而不是像其他非空区域一样有值. 此时注意nodata区域要赋 ...

  5. 离线部署ArcGIS Runtime for Android100.5.0

    环境 系统:window 7 JDK:1.8.0_151 Maven:3.6.1 Android Studio:2.3 ArcGIS Runtime SDK for Android:100.5.0 1 ...

  6. ArcGIS Pro Add-In插件开发[ArcGIS Pro SDK for .NET]

    本文基于 Windows7 + VS2019 + .NET Framework 4.8 + ArcGIS Pro 2.5 开发和撰写. 目录 开发环境配置 获取ArcGIS Pro 安装VS2019 ...

  7. “指定的参数已超出有效值的范围”在【 parameterUpdate.Add(new OracleParameter("STATUS", 0));】报错

    改成:parameterUpdate.Add()); 就不报错,并不能知道为什么,有知道为什么的,评论告诉我. /// <summary> /// 插入数据 /// </summar ...

  8. 常见ArcGIS操作(以10.0为例)

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.建立缓冲区 先在图层属性表里面新建一个缓冲区半径字段,然后对该字段赋 ...

  9. 2.mongoDB add user in v3.0 问题的解决(Property 'addUser' of object admin is not a func)

    问题:创建mongodb帐户时,出错 > db.addUser('jyu', 'aerohive')  2015-08-05T20:03:02.767+0800 E QUERY    TypeE ...

随机推荐

  1. 数据结构 B-树和B+树的应用:数据搜索和数据库索引

    B-树 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每个结点至多有m 棵子树:⑵若根结点不是叶子结点 ...

  2. linux修改时间

    1.修改linux系统时间 [root@localhost ~]# date -s "2016-10-15 13:15:12" 2.将系统时间和网络服务器时间同步 [root@lo ...

  3. Windows2012 显示我的电脑

    从Windows2012开始,微软取消了服务器桌面个性化选项,如何重新调出配置界面,可以使用微软命令调出.具体方法如下: 1.同时按住键盘上的“Windows键”+“R”,调出运行窗口.在运行窗口输入 ...

  4. redis3.0.6安装(linux和windows)

    官网上描述安装方法如下:$ wget http://download.redis.io/releases/redis-3.0.6.tar.gz$ tar xzf redis-3.0.6.tar.gz$ ...

  5. 初窥Linux 之 我最常用的20条命令

    魏公 SecureCRTuname -avisftppartition,fsshell kshell,bshelluser,groupIPTables文件数,内核参数tail,less/var/log ...

  6. dom节点的操作

    dom节点的操作 -- jQuery 内部插入 1.(结尾)append 方法 . appendto方法(为了方便链式操作) (开头)prepend方法           $('#div1').ap ...

  7. asp.net天网代码

    自己整理的asp.net中国天气网的城市代码 完整下载 case "北京": Code = "101010100"; break; case "海淀& ...

  8. OpenCV从入门到放弃系列之——如何扫描图像、利用查找表和计时

    目的 如何遍历图像中的每一个像素? OpenCV的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 测试用例 颜色空间缩减.具体做法就是:将现有颜色空间值除以某个输入 ...

  9. 使用idea15搭建基于maven的springmvc-mybatis框架

    我这边使用的是intellij idea15 1.new maven webapp project 2.添加groupId和artifactId 3.选择maven路径和maven仓库路径 最后确定之 ...

  10. [原]Linux ssh远程连接断开问题处理办法

    我们在通过远程连接操作Linux server的时候,有可能过一段时间忘记操作,便会发生ssh断开的问题. 而如果是本地的server,比较好办,直连设备kill掉ssh,踢掉无效用户连接,再次链接即 ...