12

I am trying to use Arcpy to run the CopyFeatures_management script so that I can copy a featurelayer in SDE.

What do I use for the input (and output, for that matter, since I'll be copying the layer back to SDE) to access the layer?

3 Answers 3

16

Two ways that I can think of, both involve having a Database Connection already set up in ArcCatalog. If the Database Connection file does not already exist, you can use CreateArcSDEConnectionFile_management in your script to create it.

1) Set the current workspace to the database connection, and then refer to the feature class by name.

arcpy.env.workspace = r"Database Connections\MySDEDatabaseConnection.sde"
fc = "SDE.myFeatureClass"

If the feature class is in a feature dataset, tack on the the feature dataset name to the workspace like so:

arcpy.env.workspace = r"Database Connections\MySDEDatabaseConnection.sde\SDE.MyFeatureDataset"

2) Supply the full path to the feature class including the database connection:

fc = r"Database Connections\MySDEDatabaseConnection.sde\SDE.MyFeatureDataset\SDE.MyFeatureClass"

Some tools require the first method, others require the second.

Also "Database Connections" is actually just a shortcut to %APPDATA%\ESRI\Desktop10.0\ArcCatalog (for ArcGIS 10 on Windows XP). You can just as easily supply the full path to .sde files that are stored in that folder or other folders.

2
  • Warning in others languages you will change "Database Connections" by words in accordance to your software language used on the system. In mine, (because I am french) the connection is : fc = r"Connexions aux bases de données\MySDEDatabaseConnection.sde\SDE.MyFeatureDataset\SDE.MyFeatureClass" Mar 10, 2017 at 9:46
  • ok, what if I need to use layer 1 from database 1 and clip it to layer 2 thats in database 2. how do I handle the env.workspace if there are two separate workspaces?
    – NULL.Dude
    Mar 20, 2018 at 16:12
11

You'll use the path to the SDE file plus the feature class name, so something like

CopyFeatures_management(r'c:\connections\my.sde\fc1', r'c:\connections\my.sde\newfc')

2
  • 5
    And the 'r' in front of the text does the same thing in Python as '@' in C#, i.e. treats the string as a literal so the '\' aren't misconstrued as control characters? Jan 26, 2011 at 23:45
  • 2
    Correct. Marks string as literal without control characters. Jan 27, 2011 at 0:00
2

In accordance with my previous comment I have an other proposition to acces safely to feature dataset and featureclass

# catalog local and arcgis version
arcgis_version = arcpy.GetInstallInfo()['Version'].split(
    ".")  # liste v_majeur,v_mineur
catalog_path = "{}\\ESRI\\Desktop{}\\ArcCatalog".format(
    os.getenv('APPDATA'), ".".join(
        arcpy.GetInstallInfo()['Version'].split(".")[:2])) # Work with Arcgis >= 10.3
conn = {}
conn["out_folder_path"] = catalog_path
conn["out_name"] = "server_x_db_user.sde"
conn["database_platform"] = "SQL_SERVER"
conn["instance"] = "server_x"
conn["account_authentication"] = "DATABASE_AUTH"
conn["database"] = "bdd"
conn["username"] = "db_user"
conn["password"] = "MydbPasS@"
conn["save_user_pass"] = "SAVE_USERNAME"

arcpy.CreateDatabaseConnection_management(**conn)
#result
# >>> <Result 'C:\\Users\\me\\AppData\\Roaming\\ESRI\\Desktop10.4\\ArcCatalog\\server_x_db_user.sde'>
desc = arcpy.Describe(os.path.join(conn["out_folder_path"],conn["out_name"]) 
# you can also pass by arcpy.Result object
arcpy.env.workspace = os.path.join(desc.path, desc.name)
#safe env for arcCatalog sde folder

print arcpy.env.workspace 
# >>> u'Connexions aux bases de donn\xe9es\\server_x_db_user.sde'

for ds in arcpy.ListDatasets(feature_type='feature') + ['']:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        print fc
        # Remove empty dataset to get valid path
        path = os.path.join(
            *[v for v in [arcpy.env.workspace, ds, fc] if v])
        print path

result FC:

bdd.user_db.bndy_lv_municipal_sector
bdd.user_db.bndy_admin_lv_municipal
bdd.user_db.water_pg
bdd.user_db.water_pl

result access with path:

Connexions aux bases de données\server_x_db_user.sde\bdd.user_db.bndy_lv_municipal_sector
Connexions aux bases de données\server_x_db_user.sde\bdd.user_db.bndy_admin_lv_municipal
Connexions aux bases de données\server_x_db_user.sde\bdd.user_db.water_pg
Connexions aux bases de données\server_x_db_user.sde\bdd.user_db.water_pl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.