Select to view content in your preferred language

Identify feature class within feature dataset

6271
13
10-01-2015 03:23 PM
MikeLouwrens
Frequent Contributor

I have a list of feature class names I want to feed into a script, and one of the things I need the script to do is determine whether each feature class is held within a feature dataset (as then the script will treat the FC differently).

Using Describe I can identify feature classes and feature datasets, however I cannot determine whether a FC is within a FD.  I can list children of a FD, but not parent of FC.

Is there another way I can easily determine whether a specified FC is held in a FD?

The list of FCs is output from a third-party table, so doesn't reference feature datasets.  The FCs are contained in a database that has many feature datasets and hundreds of feature classes.  While I know I could Describe all the FDs in my database and compare the children to my list, I imagine this would take a long time, when my list only contains about 60 FDs and only about 15 of them are in FDs.

EDIT: If possible I would like to determine the name of the Feature Dataset, not just if there is one.  Data is in a SDE Geodatabase (SQL Server)

Thanks,
Mike.

Message was edited by: Mike Louwrens

Tags (2)
0 Kudos
13 Replies
MikeLouwrens
Frequent Contributor

Attached is all my code so far - have only attempted to find the Feature Dataset for the feature class so far, so not much to it.  Using the help from Describe object properties—Help | ArcGIS for Desktop (and some of the other Describe Properties that appear to fit)

import arcpy, os

SDEDatabase = "Database Connections\\GISDatabase.sde"

## SWServiceLine is a Feature Class contained in a Feature Dataset called "StormWater",
## however my list of Feature Classes does not (and can not) identify if a particular 
## Feature Class is within a Feature Dataset and the name of the Feature Dataset if relevant
fc = SDEDatabase + "\\SWServiceLine"
tester = arcpy.Describe(fc)

## Describe options from ArcGIS desktop help.  Potentially there is one that
## tells me the Feature Dataset that contains the Feature Class, but
## I haven't found one yet
if hasattr(tester, "name"):
  print "Name:               " + tester.name
if hasattr(tester, "dataElementType"):
  print "dataElementType:    " + tester.dataElementType
if hasattr(tester, "catalogPath"):
  print "CatalogPath:        " + tester.catalogPath
if hasattr(tester, "Path"):
  print "Path:               " + tester.path
if hasattr(tester, "datasetType"):
  print "DatasetType:        " + tester.datasetType
if hasattr(tester, "baseName"):
  print "BaseName:           " + tester.baseName

## As suggested by Wes Miller
h, n = os.path.split(fc)
desc = arcpy.Describe(h)
#print desc.datasetType      ## this produces an error
print desc.name

I've been through a lot of the describe properties, but so far I haven't found any that tell me a) if the feature class is in a feature dataset or b) the name of the feature dataset.

Cheers,

Mike.

0 Kudos
WesMiller
Deactivated User
import arcpy,os
fc = "your file"
h,n = os.path.split(fc)
desc = arcpy.Describe(h)
print desc.datasetType
print desc.name
Luke_Pinner
MVP Regular Contributor

I had a quick look at this and I *think* Darren Wiens​ is right, arcpy.da.Walk (or a combination of arcpy.ListDatasets() and arcpy.ListFeatureClasses() etc.) is the only way to get the Feature Dataset name if you only know the Feature Class name at runtime:

Something like:

import arcpy,os
gdb = r"C:\Temp\Default.gdb"
fcs = ["TESTFC",   # standalone FC
       "TESTFDFC"] # FC in a feature dataset

def get_paths(path):
    """Use this if you have multiple FCs to check"""
    paths = {}
    for dirname,subdirs,files in arcpy.da.Walk(path):
        paths.update(zip(files, [os.path.basename(dirname.replace(path,""))]*len(files)))
    return paths

def get_fd(fc):
    """Use this if you only have one FC to check"""
    path = os.path.normpath(os.path.dirname(fc))
    if  path == ".":
        path = arcpy.env.workspace
    for dirname,subdirs,files in arcpy.da.Walk(path):
        if fc in files:
            return os.path.basename(dirname.replace(path,""))

arcpy.env.workspace = gdb
paths = get_paths(gdb)
for fc in fcs:
    print os.path.join(paths[fc], fc)
    print os.path.join(get_fd(fc), fc)

TESTFC

TESTFC

TESTFD\TESTFDFC

TESTFD\TESTFDFC

KevinMorrissey
Deactivated User

at least within the sql tables, the gdb_items indicates the Name, and the Path which contains the fds address if it applies...

gdb_items.jpg

The gdb_itemrelationships would be more detailed, but as I understand, this might be enough, perhaps?

This was a helpful link in identifying this...

Feature datasets in a geodatabase in SQL Server—Help | ArcGIS for Desktop