博客
关于我
ArcGIS Engine常用开发代码整理
阅读量:820 次
发布时间:2019-03-25

本文共 4168 字,大约阅读时间需要 13 分钟。

创建ArcGIS工作空间和处理地理数据的完整流程

在ArcGIS开发中,了解如何创建工作空间、管理地理数据以及实施常见操作是核心技能。以下是从基础到高级操作的完整指南。

1. 创建ArcGIS工作空间工厂——EDN

在编写代码时,首先需要创建工作空间工厂以管理数据源。以下代码示例展示了如何使用AccessWorkspaceFactory来创建一个新的工作空间。

public void IWorkspaceFactory_Create_Example_Access()  
{
IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
IWorkspaceName workspaceName = workspaceFactory.Create("C:\\temp\\", "MyNewpGDB.mdb", null, 0);
IWorkspace pGDB_workspace = (IWorkspace)workspaceName.Open();
Console.WriteLine("Current path of the {0} is {1}", pGDB_workspace.Type, pGDB_workspace.PathName);
}

2. 遍历所有图层

知道如何高效地遍历地图中的所有图层是 GIS 开发中的常见任务。以下方法展示了如何通过枚举层来筛选特定类型的UID。

IMap pMap = axMapControl1.Map;  
IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
pEnumLayer.Reset();
ILayer pLayer = pEnumLayer.Next();
while (pLayer != null)
{
pLayer = pEnumLayer.Next();
}

3. 判断图层类型

在ArcGIS中,判断图层类型有助于定制操作。以下代码可以根据图层的GUID确定其类型。

public I3DProperties get3DProps(IFeatureLayer pFeatLayer)  
{
I3DProperties p3DProps = null;
ILayer pLayer = pFeatLayer as ILayer;
ILayerExtensions lyrExt = pLayer as ILayerExtensions;
for (int i = 0; i < lyrExt.ExtensionCount; i++)
{
if (lyrExt.get_Extension(i) is I3DProperties)
{
p3DProps = lyrExt.get_Extension(i) as I3DProperties;
}
}
return p3DProps;
}

4. 基于图层名称获取当前图层

有时,根据图层名称找到特定图层是必要的。以下代码实现了两种方法:通过名称匹配和通过选中元素获取。

Private Function GetCurLayer() As ILayer  
Dim i As Integer
Set GetCurLayer = Nothing
For i = 0 To ff_m_Map.LayerCount - 1
If UCase$(ff_m_Map.Layer(i).Name) = UCase$(ff_m_strCurLayername) Then
Set GetCurLayer = ff_m_Map.Layer(i)
Exit For
End If
Next i
End Function

5. 打开TIN数据集

处理TIN数据集需要使用专门的接口。以下代码展示了如何打开并访问TIN数据集。

public ILayer openTinLayer(string fullPath)  
{
ITinWorkspace pTinWorkspace;
IWorkspace pWS;
IWorkspaceFactory pWSFact = new TinWorkspaceFactoryClass();
string pathToWorkspace = System.IO.Path.GetDirectoryName(fullPath);
string tinName = System.IO.Path.GetFileName(fullPath);
pWS = pWSFact.OpenFromFile(pathToWorkspace, 0);
ITinWorkspace pTinWorkspace = pWS as ITinWorkspace;
if (pTinWorkspace.get_IsTin(tinName))
{
ITin pTin = pTinWorkspace.OpenTin(tinName);
pTinLayer.Dataset = pTin;
pTinLayer.ClearRenderers();
return pTinLayer as ILayer;
}
else
{
MessageBox.Show("该目录不包含Tin文件");
return null;
}
}

6. 构建IPolyline或IPolygon

将点集合转换为线或面是 GIS 开发中的常见操作。以下代码展示了如何创建并添加点到IPolyline或IPolygon中。

IPolyline m_ProfilePolyline = new PolylineClass();  
IPointCollection m_PtCol = m_ProfilePolyline as IPointCollection;
IPoint pPoint1 = new PointClass();
pPoint1.X = x; pPoint1.Y = y; m_PtCol.AddPoint(pPoint1, ref missing, ref missing);
IPoint pPoint2 = new PointClass();
pPoint2.X = x; pPoint2.Y = y; m_PtCol.AddPoint(pPoint2, ref missing, ref missing);

7. 平缓处理(Buffering)

在ArcGIS中,平缓处理是构造缓冲区的一种常用操作。以下代码展示了如何将线向两侧平移以构造矩形或面。

private IPolygon FlatBuffer(IPolyline myLine, double bufferDis)  
{
object o = System.Type.Missing;
IConstructCurve mycurve = new PolylineClass();
mycurve.ConstructOffset(myLine, bufferDis, ref o, ref o);
IPointCollection pCol = mycurve as IPointCollection;
IConstructCurve mycurve2 = new PolylineClass();
mycurve2.ConstructOffset(myLine, -1 * bufferDis, ref o, ref o);
IPolyline addline = mycurve2 as IPolyline;
addline.ReverseOrientation();
IPointCollection pCol2 = addline as IPointCollection;
IPointCollection myPCol = new PolygonClass();
myPCol.AddPointCollection(pCol);
IPolygon myPolygon = myPCol as IPolygon;
myPolygon.SimplifyPreserveFromTo();
return myPolygon;
}

8. 遍历要素类中的所有字段

获取要素类中的所有字段是开发过程中常需操作。以下代码展示了如何筛选非几何字段并添加到控件中。

IField pField = null;  
IFields pFields = pFeatureClass.Fields;
for (int i = 0; i < pFields.FieldCount - 1; i++)
{
pField = pFields.get_Field(i);
if (pField.Type != esriFieldType.esriFieldTypeGeometry)
{
dgvCombo.Items.Add(pField.Name);
}
}

参考文献

转载地址:http://mdayk.baihongyu.com/

你可能感兴趣的文章
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>