Select to view content in your preferred language

Test if SDE feature class is part of a feature dataset?

525
2
05-27-2011 01:02 PM
mvisekru
Occasional Contributor
Is there a way in ArcPy to test a featureclass in order to see if it is part of a featuredataset + get the name of the featuredataset?

Thanks!
Tags (2)
0 Kudos
2 Replies
AnthonyFarndon
Frequent Contributor
Not the most elegant solution which I have not tested, so hopefully someone has a better suggestion....

If you are calling this often in your code, you could create a dictionary of the geodatabase contents, and then run you tests against that

import arcpy
arcpy.env.workspace = "yourdegeodatabase.sde"

fcDict = {}

#loop through and fill dictionary
dsList = arcpy.ListDatasets()
for ds in dsList:
    fcList = arcpy.ListFeatureClasses("*","",ds)
    for fc in fcList:
        fcDict[fc] = ds

#test
myFC = arcpy.Describe("thefciwanttotest")
if myFC.name in fcDict:
    print fcDict[myFC.name]


If it is a once only call in your script for one featureclass, this is a little more streamlined, again not tested.
import arcpy
arcpy.env.workspace = "yourdegeodatabase.sde"

myFC = arcpy.Describe("thefciwanttotest")

#loop through
dsList = arcpy.ListDatasets()
for ds in dsList:
    fcList = arcpy.ListFeatureClasses(myFC.name,"",ds)
    for fc in fcList:
        print ds
0 Kudos
curtvprice
MVP Alum
Is there a way in ArcPy to test a featureclass in order to see if it is part of a featuredataset + get the name of the featuredataset?

Thanks!


Here's my suggestion:

import arcpy, os
def GetFDS(FC):
  """ Returns the feature dataset for a feature class
  or feature layer FC.

  If the feature class is not within a feature dataset,
  returns None."""
  # get the path to the feature class
  fcPath = arcpy.describe(FC).catalogPath
  # get the path to its container
  fcHome = os.path.dirname(fcPath)
  # is it a feature dataset??
  if arcpy.describe(fcHome).dataType == "FeatureDataset":
    return fcHome
  else:
    return None
0 Kudos