Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arcpy get database path of feature class in feature dataset

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.

like image 665
Genspec Avatar asked Mar 22 '15 06:03

Genspec


People also ask

What is the difference between a feature class and a feature data set?

A feature dataset is a collection of related feature classes that share a common coordinate system. Feature datasets are used to spatially or thematically integrate related feature classes.

Can feature classes in a feature dataset have different coordinate systems?

All feature classes in a feature dataset must share a common coordinate system, and the x,y coordinates of their features should fall within a common spatial extent.


2 Answers

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)
like image 171
JGP Avatar answered Nov 15 '22 04:11

JGP


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"
like image 25
Adam Avatar answered Nov 15 '22 02:11

Adam