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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Michael Todd
  • 1,976
  • 4
  • 20
  • 31

3 Answers3

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.

blah238
  • 35,793
  • 7
  • 94
  • 195
  • 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"

    – GeoStoneMarten Mar 10 '17 at 09: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 '18 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')

Jason Scheirer
  • 18,002
  • 2
  • 53
  • 72
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
GeoStoneMarten
  • 1,165
  • 10
  • 26