Getting FEATURE DATASET name - not FEATURE CLASS name - using Python

2570
11
09-10-2013 08:39 AM
LorindaGilbert
Occasional Contributor II
So,
Has anyone figured out how to get the Feature Dataset name using Python when your data is residing in FeatureDatasets within the database?  I see that the layer.datasetName is really the feature class name.  Unfortunately, we have several feature classes within feature datasets for network and topology purposes and now that we are changing servers, I need to have a script that will change the source data. 

Alas, as of right now, I can't do it via script unless someone has a work around out there. 

I've been banging my head against the wall for a week now, because like some others, when a term 'dataset' is used, you would expect it to be related to the same things that it is elsewhere.  I'd seriously consider an attempt to parse the name out, but we have several different former datasources referenced (even after changing them in the mxd - thanks for hardcoding ESRI) and the feature dataset names are many different lengths.

Thanks if anyone has a workaround to help with this.
Tags (2)
0 Kudos
11 Replies
MathewCoyle
Frequent Contributor
Something like this would get what you want I believe. Depends on what you are doing specifically if it is in a dataset or not.

import os
import arcpy

mxd = mxd = arcpy.mapping.MapDocument('current')
layer = arcpy.mapping.ListLayers(mxd, 'some_layer')[0]
describe = arcpy.Describe(os.path.dirname(layer.dataSource))
ws_type = describe.dataElementType
if ws_type == 'DEFeatureDataset':
    print('is in dataset')

elif ws_type == 'DEWorkspace':
    print('is not in dataset')

else:
    print('unknown datatype')
    pass
0 Kudos
MichaelVolz
Esteemed Contributor
Would ListDatasets help you out?

In 1 script where I need to access feature classes inside feature datasets, I loop through the feature datasets and then I loop through the feature classes inside the feature dataset that has the current focus.
0 Kudos
LorindaGilbert
Occasional Contributor II
Would ListDatasets help you out?

In 1 script where I need to access feature classes inside feature datasets, I loop through the feature datasets and then I loop through the feature classes inside the feature dataset that has the current focus.


I'm not sure, do you have a sample of the looping through the fds and the the fc?  Although I believe that the fc would be doing the for lyr in lyrList portion that I have already started.
Thanks,
0 Kudos
RDHarles
Occasional Contributor
Here's an example of looping through feature classes, tables & datasets in a file geodatabase...

import arcpy, os

log = open("list.txt","a")

arcpy.env.workspace = os.getcwd()

# For each ws, print the fc's and tbl's.
for ws in arcpy.ListWorkspaces("*", "FileGDB"):    
    arcpy.env.workspace = ws
    # feature classes
    for fc in arcpy.ListFeatureClasses():    
        print "fc: "+fc
        log.write("fc: "+fc+"\n")
    # tables
    for tbl in arcpy.ListTables():
        print "tbl: "+tbl
        log.write("tbl: "+tbl+"\n")
    # feature classes in datasets
    for ds in arcpy.ListDatasets():
        print "ds: "+ds
        log.write("ds: "+ds+"\n")
        for dfc in arcpy.ListFeatureClasses("*", "All", ds):    
            print "fc in ds: "+dfc
            log.write("fc in ds: "+dfc+"\n")


0 Kudos
MichaelVolz
Esteemed Contributor
rdharles:

In your sample code you use the method getcwd.  I am reading that this method returns the current working directory of a process.  So in this case, would the working directory be the folder location where the python script is being run from?  If this is not the case, what would the working directory be if I see no directory specified in your code?
0 Kudos
RDHarles
Occasional Contributor
rdharles:

In your sample code you use the method getcwd.  I am reading that this method returns the current working directory of a process.  So in this case, would the working directory be the folder location where the python script is being run from?  If this is not the case, what would the working directory be if I see no directory specified in your code?


Hi Michael,
I run all my python scripts from a windows command prompt, so yes, os.getcwd() spits back the current directory in which you are running the script from.  This way I don't have to hardcode any of my scripts, they just always work no matter where the data may sit on the file structure.
R.D.
0 Kudos
MichaelVolz
Esteemed Contributor
rdharles:

I am still confused with how you use arcpy.env.workspace = os.getcwd() along with ListWorkspaces.

According to the arcgis python documentation, ListWorkspaces lists all the workspaces within the set workspace.  If you set the workspace to be the folder location where the python script is being run, that also means that the file geodatabases you are looping through need to be stored in the same folder location.  That does not seem like how most organizations would structure their data storage locations.

Please correct me if I am interpreting the documentation incorrectly as I usually explicitly set the workspace from a config file.  The data storage location is in a different location than where the python scripts are stored and run from.
0 Kudos
RDHarles
Occasional Contributor
Let me give you a bit more detail and background...

1.) All the python scripts I write for our production are stored in a central location on our server (H:/pyscripts)
2.) All of our production people have that path set in their system environment PATH variable. (control panel > system > advances system settings > environment variables...)
3.) We also have a path set to where the the python installlation is.
4.) Now everything is easy, all anyone has to do it "cd" at the command prompt to the directory on the system where they want to process some data, and everything works by simply typing the script name.

In this example, I've gone to where my current gdb is (H:/jobs/j12345) and typed the name of the script (listStuff.py).  In this case, the xy.gdb only contains one fc, POIs.

Hope that make sense.  It's a highly efficient system that allows anyone in the organization to run python scripts easily, even if they have little understand of coding, python, system variables, ect.

[ATTACH=CONFIG]27347[/ATTACH]
0 Kudos
MichaelVolz
Esteemed Contributor
Thank you for the excellent clarification rdharles

3.) We also have a path set to where the the python installlation is

Do you have this set with the PYTHONPATH environmental variable?
0 Kudos