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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityTwo 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.
fc = r"Connexions aux bases de données\MySDEDatabaseConnection.sde\SDE.MyFeatureDataset\SDE.MyFeatureClass"
Mar 10, 2017 at 9:46
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')
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