同学做毕设,要求我帮着写个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. 在AngularJS中同一个页面配置一个或者多个ng-app

    在AngularJS学习中,对于ng-app初始化一个AngularJS程序属性的使用需要注意,在一个页面中AngularJS自动加载第一个ng-app,其他ng-app会忽略, 如果需要加载其他ng ...

  2. mysql实战之 批量update

    mysql实战之批量update 现阶段我们的业务量很小,要对admin_user表中的relationship字段进行更新,指定id是409.已知409是公司内的一服务中心,需要把该服务中心放到区代 ...

  3. google浏览器打开报出文件不可读解决方案

    1.打开*.desktop文件 gedit ~/.local/share/applications/name.desktop 在文件中做改动: The .desktop file should loo ...

  4. 如何在win7上安装ant-design

    1.首先要安装务必确认 Node.js 已经升级到 v4.x 或以上. 2.打开cmd,输入"npm install antd-init -g",安装antd(可以自己先指定安装目 ...

  5. 前言,学习ios编程(坚持)

    其实,尝尝有人很疑惑,不知道自己要干嘛,看到很多的培训机构,不知道怎么选择但是又想进入软件行业.其实呢学习不一定要靠培训机构,一定要培训,特别是 当人家把自己吹的天花乱坠的时候,然并卵.出来之后,也只 ...

  6. 22.mongodb副本集集群

    软件版本64位:     $ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.0.tgz     mongo ...

  7. LTE 测试文档(翻译)

    Testing Documentation 翻译 (如有不当的地方,欢迎指正!)     1 概述   为了测试和验证 ns-3 LTE 模块,文档提供了几个 test suites (集成在 ns- ...

  8. leveldb 学习。

    1)大概浏览了leveldb文档的介绍.本想逐步看代码,想想还是自己先实现一个看看如何改进. 2)完成了一个非常丑陋的初版,但是还是比初初版有进步. 3)key value的数据库,不允许有key重复 ...

  9. TCP/IP协议学习(七) 基于C# Socket的Web服务器---动态通讯实现

    目录 (1).基于Ajax的前端实现 (2).Web服务器后端处理 一个完整的web服务器,不仅需要满足用户端对于图片.文档等资源的需求:还能够对于用户端的动态请求,返回指定程序生成的数据.支持动态请 ...

  10. springmvc 解决跨域CORS

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...