List all FeatureClasses from a workspace and loop on the fields

1480
7
03-05-2012 06:40 PM
dgesridgesri
Occasional Contributor II
Greetings,

Lately I`ve assigned to a task that require me to list all the feature classes that are within a workspace.

So I need help here with this....I have a feature workspace and I`m trying to list all the feature classes inisde it, then Loop over the fields of each featureclass....Is there a sample code to do this? ...

Please advise....

Thanks in advance
0 Kudos
7 Replies
NeilClemmons
Regular Contributor III
The IWorkspace interface has a DatasetNames property that you can use to get the feature classes.  The IFeatureClass interface has a Fields property that you can use to get the fields.
0 Kudos
MukundaRao_R
New Contributor
Below is the code to get the feature class names form the workspace

// Get a name object for the first feature class in the workspace.
            IEnumDatasetName enumDatasetName = featureWorkspace.get_DatasetNames
                (esriDatasetType.esriDTAny);
            //enumDatasetName.Reset();
            IDatasetName datasetName = null;

            while ((datasetName = enumDatasetName.Next()) != null)
            {
                string featureClassname = datasetName.Name;
            }

Below code would be help full in looping through the fields

ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
                    ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("129398824");
                    ISpatialFilter spatialFilter = new SpatialFilterClass();
                    spatialFilter.Geometry = envelope;
                    spatialFilter.GeometryField = featureClass.ShapeFieldName;
                    spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                    spatialFilter.SubFields = "Point";
                 
                    IWorkspace workspaceCont = null;
ISelectionSet ifc = featureClass.Select(spatialFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, workspaceCont);
                    int fieldID;
                    IEnumIDs eIDs = ifc.IDs;
                    string fieldvalue= string.Empty;
                    while ((fieldID = eIDs.Next()) != null) // The fieldID corresponds to each feature and get_value method get the attributes of the fields
                    {
                        if (fieldID == -1)
                            break;
                        fieldvalue = featureClass.GetFeature(fieldID).get_Value(7).ToString();
                    }

Thanks
Mukund
0 Kudos
dgesridgesri
Occasional Contributor II
Thanks Guys for your value replies....

Please let me clarify my question in order to give you more details and let you help me more...

I have created a SDE workspace using the following code:

  
            Dim pPropertySet As IPropertySet
            pPropertySet = New PropertySet
            With pPropertySet
                .SetProperty("Server", Server)
                .SetProperty("Instance", Instance)
                .SetProperty("User", Username)
                .SetProperty("Password", Password)
                .SetProperty("Version", Version)
            End With
            Dim sdeWrkspcFactroy As IWorkspaceFactory
            sdeWrkspcFactroy = New SdeWorkspaceFactory
            pWS = sdeWrkspcFactroy.Open(pPropertySet, 0) 


Moreover, this Workspace is  containing more than 10 datasets and each Dataset may contain many FeatureClasses....
So I have a Workspace created through a connection (PropertySet) and this Workspace contains many DataSets and each Dataset contains many FeatureClasses...

@ Neil: I hope to get some sample code from your side

@ mukundbtech: I think that your code related to retrieve all Datasets in the Workspace and QI with FeatureClass type will not be a solution to me since there is a lot of FeatureClasses in each Dataset in the Workspace. In related to the second part of your code, I thank you very much but what I need and it is not mentioned in my first post which is really my fault,What I need is to list the fields of each FeatureClass that are assigned domains with the domains names..

appreciate your support...
0 Kudos
MukundaRao_R
New Contributor
here is the code to get fields of a featureclass and check for the assigned domain

IFields fields = featureClass.Fields;
    string domainName = string.Empty;
    for (int i = 0; i < fields.FieldCount; i++)
{
            if (fields.get_Field(i).DomainFixed)
                            domainName = fields.get_Field(i).Domain.Name;
}

Thanks
Mukund
0 Kudos
dgesridgesri
Occasional Contributor II
@mukundbtech: I tried your posted code and found that it is not returning all the used domains, but only the fixed ones....However I got the solution to return all the domains assigned to fields in all feature classes...But the issue now is the first one which is how to return all the feature classes in a workspace...
0 Kudos
NeilClemmons
Regular Contributor III
As I mentioned before, you can use IWorkspace::DatasetNames to get the feature classes from a workspace.  If your feature classes are inside feature datasets then use this method to get the feature datasets then loop through each dataset to get the feature classes.
0 Kudos
dgesridgesri
Occasional Contributor II
As I mentioned before, you can use IWorkspace::DatasetNames to get the feature classes from a workspace.  If your feature classes are inside feature datasets then use this method to get the feature datasets then loop through each dataset to get the feature classes.


Hello Neil,

I`m just wondering how I would do what you suggest, is there a way to get FeatureClasses inside a Dataset as you mentioned through code........because my FeatureClasses  are all inside many Datasets...So I have to get the Dataset first then get all the FeatureClasses inside it..Is there any sample VB.Net code to do that...
0 Kudos