7

I am trying to get the database path of a feature class that may or may not be in a feature dataset. I am using the os.path.dirname of the feature class. This will give me either the database path if the feature class is not in a feature dataset (great), but if the feature class is in a feature dataset, it will give me the path to the feature dataset.

This could be a file, personal or sde geodatabase. I was thinking of a split using the '.sde' but that won't work if it is a different type of geodatabase.

A sample of the path could be:

  • For inside a feature dataset: C:\GISData\Data.gdb\Property\Erf
  • For under the gdb root: C:\GISData\Data.gdb\Erf

In both these cases I would like to get C:\GISData\Data.gdb.

Thank you.

Genspec
  • 2,279
  • 2
  • 14
  • 10

2 Answers2

9

Check out this short blog posting which they use the following function:

def get_geodatabase_path(input_table):
  '''Return the Geodatabase path from the input table or feature class.
  :param input_table: path to the input table or feature class 
  '''
  workspace = os.path.dirname(input_table)
  if [any(ext) for ext in ('.gdb', '.mdb', '.sde') if ext in os.path.splitext(workspace)]:
    return workspace
  else:
    return os.path.dirname(workspace)
kenorb
  • 155,785
  • 88
  • 678
  • 743
JGP
  • 106
  • 1
  • Thanks very much. This helped a great deal. I managed to get something working through some `try` and `except` checking the `workspaceType` or `datasetType == "FeatureDataset"`, but this is definitely a cleaner way to do it. – Genspec Mar 23 '15 at 03:49
  • Also see http://gis.stackexchange.com/questions/112288/how-to-set-workspace-from-input-feature-class for handling Feature Datasets in path – matt wilkie Jan 20 '16 at 21:04
6

Another way which is not obvious from the documentation is to use the path (Read Only) property:

Describe object properties

import arcpy
desc = arcpy.Describe(r"C:\GISData\Data.gdb\Erf")
print desc.path

# prints: u"C:\\GISData\\Data.gdb"
Adam
  • 161
  • 1
  • 5
  • (I assumed you were using arcpy) – Adam Apr 28 '16 at 05:25
  • 6
    This does not get around the problem of feature datasets within a geodatabase as they are reported by `path`. It is only a substitute for the `os.path.dirname` function. – Andy Harfoot Dec 07 '17 at 12:30