博客
关于我
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/

你可能感兴趣的文章
python | Python中使用@property装饰器
查看>>
python | Python中的functools模块高级应用
查看>>
python | Python中的itertools模块使用技巧
查看>>
python | Python中的事件驱动编程模型
查看>>
python | Python中的内存池与缓存机制
查看>>
python | Python中的弱引用与内存管理
查看>>
python | Python中的类多态:方法重写和动态绑定
查看>>
python | Python作用域链查找机制
查看>>
python | Python俄罗斯方块游戏详解
查看>>
python | Python动态代码执行:exec和compile函数
查看>>
Python读取文件数据进行数据图形化展示
查看>>
python | Python反向迭代:reversed实现机制
查看>>
python | Python开发必知的数据容器用法(建议收藏!)
查看>>
python读取文件夹下文件名称_如何在Python目录中获取文件名列表
查看>>
python | Python文本处理中的相似性识别应用
查看>>
python | Python模块缓存:sys.modules机制
查看>>
python | Python集成学习和随机森林算法
查看>>
python | Python高阶函数与函数式编程
查看>>
python | pytime,一个实用的 时间和日期处理 Python 库!
查看>>
python | pyupgrade,一个有趣的 Python 库!
查看>>