同学做毕设,要求我帮着写个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. 大理石在哪里UVa 10474

    我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){    int N,a[ ...

  2. 个人作业-Week3

    个人作业-Week3 1. 软件工程师的成长 同学们在上这门课的时候,还是大三,你的困难和迷茫,别人一定有过.请看看别人怎么学习的,有些是科班,有些是野路子,有些成功,有些失败. 请读完下面所有博客( ...

  3. R语言多项式回归

    含有x和y这两个变量的线性回归是所有回归分析中最常见的一种:而且,在描述它们关系的时候,也是最有效.最容易假设的一种模型.然而,有些时候,它的实际情况下某些潜在的关系是非常复杂的,不是二元分析所能解决 ...

  4. HDU 1525 Euclid's Game 博弈

    Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. install intel c/c++ compiler

    通过在Intel官网上申请试用版本Intel® Parallel Studio XE Cluster Edition for Linux,会让你提交邮箱等信息,完成后会很快回复邮件,邮件会给出下载地址 ...

  6. 广播Broadcast Receiver

    广播的类型: 1.标准广播(Normal broadcat) 完全异步执行的广播,所有广播接收器同一时间接收广播消息. 效率高,但无法被截断. 2.有序广播 同步执行的广播,同一时刻只能有一个广播接收 ...

  7. Java生成带小图标的二维码-google zxing 工具类

    近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core- ...

  8. html select的事件 方法 属性

    事件 onactivate 当对象设置为活动元素时触发. onafterupdate 当成功更新数据源对象中的关联对象后在数据绑定对象上触发. onbeforeactivate 对象要被设置为当前元素 ...

  9. (原创)QuartusII设置虚拟引脚(Virtual Pin)

    方法一: 在Quartus II中Assignments->Assignment Editor, 在Category栏选择logic options, 到列表中To列下添加要设置的引脚接口,如果 ...

  10. 流媒体测试笔记记录之————解决问题video.js 播放m3u8格式的文件,根据官方的文档添加videojs-contrib-hls也不行的原因解决了

    详细代码Github:https://github.com/Tinywan/PHPSharedLibrary/tree/master/Tpl/Html5/VideoJS 想播放hls协议的就是m3u8 ...