how to connect to a feature class within a feature dataset using Python

1228
7
Jump to solution
05-02-2012 02:33 PM
MatthewWalker1
New Contributor III
I am trying to modify a feature class that is within a feature dataset using python, but I keep getting the error that the feature class does not exist.  I have read many posts that state you do not have to list in the workspace the feature dataset, but the only feature classes I can see when running a arcpy.ListFeatureClasses are the ones that are outside of the feature datasets.  Here is the script I am trying to run:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"
arcpy.AddField_management("Exposures_Platte", "STATE", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

The error I get is this:

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000732: Input Table: Dataset Exposures_Platte does not exist or is not supported

What am I doing wrong here?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MatthewWalker1
New Contributor III
I GOT IT!  Not sure why it took me so long or how picky python is, but this code worked:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"
arcpy.AddField_management("BASE.BASE.Exposures_Platte", "STATE", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

The issue was I needed to fully qualify the table name, even though I had to physically login to that database as the schema owner.  I thought that login would have resolved the name schema, but I guess not.
I did have issues with a schema lock, although I was the only one logged in, but I just killed Catalog and reopened it, logged in as the schema owner, and copied the code in, and it worked!  Thanks everyone for looking into this issue!

As a side issue to this situation, instead of adding new fields to an existing feature class within that feature dataset, now I am trying to copy that existing feature class and paste a new feature class (this one a linear feature instead of a point feature) into that same feature dataset, using this code:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"

#Use existing Span (point) feature (Exposures_Platte) as template to create new linear Span feature class
arcpy.CreateFeatureclass_management("database connections/connection to KM-BASE.sde", "BASE.BASE.KMC.Exposures_Platte_Line", "POLYLINE", "BASE.BASE.Exposures_Platte", "ENABLED", "ENABLED")

and ArcCatalog crashes after about a minute of thinking, no error thrown.  Any ideas on this issue?


Solution!:
I needed to resolve the spaces that were in the connection paths by placing an 'r' (MUST be lower case!) in front of each path.  This code worked (I made a variable, 'Path', for the above path name to make deployment of this code more efficient):

arcpy.CreateFeatureclass_management(Path, r"Exposures_Platte_Line_Test2", "POLYLINE", Path +"\BASE.BASE.Exposures_Platte")  

View solution in original post

0 Kudos
7 Replies
MathewCoyle
Frequent Contributor
You are correct, you should not need to change your workspace to the dataset. Are you sure that is the name of the feature class? SDE feature classes usually have the username appending to the beginning of the feature class EG SDE.FEATURECLASS.
0 Kudos
MatthewWalker1
New Contributor III
You are correct, you should not need to change your workspace to the dataset. Are you sure that is the name of the feature class? SDE feature classes usually have the username appending to the beginning of the feature class EG SDE.FEATURECLASS.


The full name of the feature class is BASE.BASE.Exposures_Platte, but I had the 'Save password' checkbox unchecked on the connection string so as to force login as that schema owner.  I had also tried to include the schema name in with the tablename, and that also failed, stating it was an invalid database name.
0 Kudos
JohnYaist1
Esri Contributor
Hi Matt,

The ListFeatureClasses function allows for limiting the results by feature dataset.

...
arcpy.env.workspace = r"Database Connections\<YOUR CONNECTION>.sde"
fcList = arcpy.ListFeatureClasses("","","<Feature Dataset Name>")
print len(fcList)
...



This returned the number of feature classes in my feature dataset when I ran it. I used the fully qualified Feature Dataset Name as well.
Hope this helps.

John




Help Topic:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001n000000
0 Kudos
MatthewWalker1
New Contributor III
Hi Matt,

The ListFeatureClasses function allows for limiting the results by feature dataset.

...
arcpy.env.workspace = r"Database Connections\<YOUR CONNECTION>.sde"
fcList = arcpy.ListFeatureClasses("","","<Feature Dataset Name>")
print len(fcList)
...



This returned the number of feature classes in my feature dataset when I ran it. I used the fully qualified Feature Dataset Name as well.
Hope this helps.

John




Help Topic:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001n000000


Thanks for the assistance on this.  I was finally able to at least see that dataset and the feature classes within it, by using your example:
arcpy.env.workspace = r"Database Connections\connection to KM-BASE.sde"
fcList = arcpy.ListFeatureClasses("","","BASE.BASE.KMC")
print len(fcList)

but how to I integrate that dataset naming convention into my addfield_management code?  I tried adding 'BASE.BASE.KMC' to either my workspace path or to the table to be modified, and neither worked.
0 Kudos
MathewCoyle
Frequent Contributor
Kind of messy, but here is how I have accessed SDE feature classes in a dataset before. It is exporting all the feature class in each dataset in the target SDE.

dsList = arcpy.ListDatasets("TFM.TFM_*", "Feature")
for ds in dsList:
    fcList = arcpy.ListFeatureClasses("","",ds)
    ds = str(ds).partition(".")[2]
    print "Starting "+ds
    arcpy.CreateFeatureDataset_management(output, ds, sr)  
    for feature in fcList:
        featout = str(feature).partition(".")[2]
        try:
            if not arcpy.Exists(output+ds+"\\"+featout):

                arcpy.FeatureClassToFeatureClass_conversion(feature, output+ds, featout)
0 Kudos
MatthewWalker1
New Contributor III
Kind of messy, but here is how I have accessed SDE feature classes in a dataset before. It is exporting all the feature class in each dataset in the target SDE.

dsList = arcpy.ListDatasets("TFM.TFM_*", "Feature")
for ds in dsList:
    fcList = arcpy.ListFeatureClasses("","",ds)
    ds = str(ds).partition(".")[2]
    print "Starting "+ds
    arcpy.CreateFeatureDataset_management(output, ds, sr)  
    for feature in fcList:
        featout = str(feature).partition(".")[2]
        try:
            if not arcpy.Exists(output+ds+"\\"+featout):

                arcpy.FeatureClassToFeatureClass_conversion(feature, output+ds, featout)



I GOT IT!  Not sure why it took me so long or how picky python is, but this code worked:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"
arcpy.AddField_management("BASE.BASE.Exposures_Platte", "STATE", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

The issue was I needed to fully qualify the table name, even though I had to physically login to that database as the schema owner.  I thought that login would have resolved the name schema, but I guess not.
I did have issues with a schema lock, although I was the only one logged in, but I just killed Catalog and reopened it, logged in as the schema owner, and copied the code in, and it worked!  Thanks everyone for looking into this issue!

As a side issue to this situation, instead of adding new fields to an existing feature class within that feature dataset, now I am trying to copy that existing feature class and paste a new feature class (this one a linear feature instead of a point feature) into that same feature dataset, using this code:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"

#Use existing Span (point) feature (Exposures_Platte) as template to create new linear Span feature class
arcpy.CreateFeatureclass_management("database connections/connection to KM-BASE.sde", "BASE.BASE.KMC.Exposures_Platte_Line", "POLYLINE", "BASE.BASE.Exposures_Platte", "ENABLED", "ENABLED")

and ArcCatalog crashes after about a minute of thinking, no error thrown.  Any ideas on this issue?
0 Kudos
MatthewWalker1
New Contributor III
I GOT IT!  Not sure why it took me so long or how picky python is, but this code worked:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"
arcpy.AddField_management("BASE.BASE.Exposures_Platte", "STATE", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

The issue was I needed to fully qualify the table name, even though I had to physically login to that database as the schema owner.  I thought that login would have resolved the name schema, but I guess not.
I did have issues with a schema lock, although I was the only one logged in, but I just killed Catalog and reopened it, logged in as the schema owner, and copied the code in, and it worked!  Thanks everyone for looking into this issue!

As a side issue to this situation, instead of adding new fields to an existing feature class within that feature dataset, now I am trying to copy that existing feature class and paste a new feature class (this one a linear feature instead of a point feature) into that same feature dataset, using this code:

import arcpy
from arcpy import env
env.workspace = r"database connections\connection to KM-BASE.sde"

#Use existing Span (point) feature (Exposures_Platte) as template to create new linear Span feature class
arcpy.CreateFeatureclass_management("database connections/connection to KM-BASE.sde", "BASE.BASE.KMC.Exposures_Platte_Line", "POLYLINE", "BASE.BASE.Exposures_Platte", "ENABLED", "ENABLED")

and ArcCatalog crashes after about a minute of thinking, no error thrown.  Any ideas on this issue?


Solution!:
I needed to resolve the spaces that were in the connection paths by placing an 'r' (MUST be lower case!) in front of each path.  This code worked (I made a variable, 'Path', for the above path name to make deployment of this code more efficient):

arcpy.CreateFeatureclass_management(Path, r"Exposures_Platte_Line_Test2", "POLYLINE", Path +"\BASE.BASE.Exposures_Platte")  
0 Kudos