博客
关于我
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读写json文件的简单实现
查看>>
Python 中的线程
查看>>
Python 中的继承机制是什么样的?
查看>>
python读写excel之xlrd&xlwt&xlutils组合
查看>>
Python 中的装饰器是什么?
查看>>
Python 中的装饰器是如何工作的,有哪些实际应用场景?
查看>>
python读excel
查看>>
Python 中读取 CSV 文件-ChatGPT4o作答
查看>>
Python 之 filecmp
查看>>
python请求html_使用Python请求获取HTML?
查看>>
Python 之匿名函数和偏函数
查看>>
python 之栈的实现
查看>>
python语音播放
查看>>
python语言:装饰器原理
查看>>
Python 交互式数据可视化详解
查看>>
python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
查看>>
Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
查看>>
Python 从数据库中存储和检索密码的最安全方法
查看>>
Python语言及其应用 - 知识点遍历
查看>>