vba ArcObjects - 枚举地理数据库中的要素类和数据集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2509567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArcObjects - enumerating feature classes and datasets within a geodatabase
提问by Tom
I'm trying to enumerate the contents (feature classes and feature datasets, not interested in tables, etc) of a file geodatabase using vba/arcobjects.
我正在尝试使用 vba/arcobjects 枚举文件地理数据库的内容(要素类和要素数据集,对表不感兴趣等)。
I have the file GDB set as an IGxDatabase object, but can't find a way of getting further in. I've had a look at the geodatabase object model and tried using IFeatureClass and IFeatureDataset but neither seem to return useful results.
我将文件 GDB 设置为 IGxDatabase 对象,但找不到进一步深入的方法。我查看了地理数据库对象模型并尝试使用 IFeatureClass 和 IFeatureDataset,但似乎都没有返回有用的结果。
Thanks in advance for any assistance
提前感谢您的任何帮助
回答by Kirk Kuykendall
It is much faster to enumerate the names contained in a geodatabase instead of the things that the names can open. The tricky part is looping through names in a featuredataset. While IFeatureWorkspace.Open can be used to open a featureclass without first opening the featuredataset that contains it, getting at featureclassnames within a featuredataset requires opening the featuredataset.
枚举包含在地理数据库中的名称而不是名称可以打开的内容要快得多。棘手的部分是遍历特征数据集中的名称。虽然 IFeatureWorkspace.Open 可用于打开要素类而无需先打开包含它的要素数据集,但获取要素数据集中的要素类名称需要打开要素数据集。
If you know for sure you'll need to open each featureclass, then I suppose it wouldn't hurt to use IWorkspace.Datasets, IEnumDataset, and IDataset instead of IWorkspaceDatasetNames, IEnumDatasetname and IDatasetname.
如果您确定需要打开每个要素类,那么我想使用 IWorkspace.Datasets、IEnumDataset 和 IDataset 代替 IWorkspaceDatasetNames、IEnumDatasetname 和 IDatasetname 不会有什么坏处。
Option Explicit
Sub TestGetContents()
Dim pGxApp As IGxApplication
Set pGxApp = Application
If Not TypeOf pGxApp.SelectedObject Is IGxDatabase Then
Debug.Print "select a geodb first"
Exit Sub
End If
Dim c As Collection
Set c = GetContents(pGxApp.SelectedObject)
Dim l As Long
For l = 1 To c.Count
Dim pName As IName
Set pName = c.Item(l)
If TypeOf pName Is IFeatureClassName Then
Dim pFC As IFeatureClass
Set pFC = pName.Open
Debug.Print pFC.AliasName, pFC.FeatureCount(Nothing)
ElseIf TypeOf pName Is IFeatureDatasetName Then
Dim pDSName As IDatasetName
Set pDSName = pName
Debug.Print pDSName.name, "(featuredataset)"
End If
Next l
End Sub
Function GetContents(pGxDB As IGxDatabase) As Collection
Dim c As New Collection
Dim pEnumDSName As IEnumDatasetName
Set pEnumDSName = pGxDB.Workspace.DatasetNames(esriDTAny)
Dim pDSName As IDatasetName
Set pDSName = pEnumDSName.Next
Do Until pDSName Is Nothing
If TypeOf pDSName Is IFeatureClassName Then
c.Add pDSName
ElseIf TypeOf pDSName Is IFeatureDatasetName Then
c.Add pDSName
AddSubNames pDSName, c
End If
Set pDSName = pEnumDSName.Next
Loop
Set GetContents = c
End Function
Sub AddSubNames(pDSName1 As IDatasetName, c As Collection)
Dim pEnumDSName As IEnumDatasetName
Set pEnumDSName = pDSName1.SubsetNames
pEnumDSName.Reset
Dim pDSName2 As IDatasetName
Set pDSName2 = pEnumDSName.Next
Do Until pDSName2 Is Nothing
If TypeOf pDSName2 Is IFeatureClassName Then
c.Add pDSName2
End If
Set pDSName2 = pEnumDSName.Next
Loop
End Sub
回答by Devdatta Tengshe
you can use the ListFeatureClasses Method on the Geoprocessor (the following shows how this can be done in C#)
您可以在地理处理器上使用 ListFeatureClasses 方法(以下显示了如何在 C# 中完成此操作)
IGeoProcessor gp = new GeoProcessorClass();
gp.SetEnvironmentValue("workspace", @"C:\temp");
IGpEnumList gpEnumList = gp.ListFeatureClasses("*", "Polygon", "");
string fc = gpEnumList.Next();
while (fc != "")
{
//Do whatever
}