[GIS] Referencing Enterprise Geodatabase feature classes from ArcPy with ArcGIS Pro

arcgis-proarcpyenterprise-geodatabase

When using ArcPy with the ArcGIS Desktop (10.4.1) architecture I am able to set a variable to a feature class and make a feature layer from it using the code below:

rock_unit_table = "Database Connections\\TEST PROD [personal].sde\\Test.TEST.DETAILED_SURFACE_COMP"
arcpy.MakeFeatureLayer_management(rock_unit_table, "rockUnit_lyr")

but when I try to do something analogous using ArcPy with ArcGIS Pro 1.3.1:

rock_unit_table = r"Databases\TEST PROD [personal]\Test.TEST.DETAILED_SURFACE_COMP"
arcpy.MakeFeatureLayer_management(rock_unit_table, "rockUnit_lyr")

I get an error (with some anonymizing applied):

Traceback (most recent call last): File "N:\path\Test.py", line 324,
in
arcpy.MakeFeatureLayer_management(rock_unit_table, "rockUnit_lyr") File "C:\Program
Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5607, in
MakeFeatureLayer
raise e File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5604, in
MakeFeatureLayer
retval = convertArcObjectToPythonObject(gp.MakeFeatureLayer_management(*gp_fixargs((in_features,
out_layer, where_clause, workspace, field_info), True))) File
"C:\Program
Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing_base.py", line
506, in
return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: Failed to execute. Parameters are not
valid. ERROR 000732: Input Features: Dataset Databases\TEST PROD
[personal]\Test.TEST.DETAILED_SURFACE_COMP does not
exist or is not supported Failed to execute (MakeFeatureLayer).

In ArcGIS Pro I can see the feature class under Databases in the Project pane so I think it is my syntax in ArcPy that is astray.

Can anybody suggest what may be astray with the syntax that I am trying to use?

Best Answer

It seems as you cannot use this shortcut any longer. When the Database Connections special folder is dragged into the Python window in ArcMap, you get:

r'C:\Users\%username%\AppData\Roaming\ESRI\Desktop10.4\ArcCatalog\'

This is because there is only one Database Connections per user, that is it is a universal source of all your enterprise database connections.

However, in ArcGIS Pro, Databases is specific to a project, that is every project will have its own Databases shortcut. When you drag the Databases into the Python window, you get:

"GDB"

There is no data path to the actual folder where the .sde files are stored. Thus, you won't be able to supply the Databases shortcut when running your arcpy scripts targeting ArcGIS Pro.

Trying to supply:

r"GDB\db.sde\esrigdb.DBO.DataLines" or r"Databases\db.sde\esrigdb.DBO.DataLines" just won't work.

Since you are always working with a specific ArcGIS Pro project, I'd get its homeFolder and then use os.path.join to get the database path:

proj = mp.ArcGISProject(r"C:\Users\%username%\Documents\ArcGIS\Projects\MyProject\MyProject.aprx")
print(proj.homeFolder)
C:\Users\%username%\Documents\ArcGIS\Projects\MyProject

The .sde connection files are stored in the project homeFolder.

arcpy.Describe(os.path.join(proj.homeFolder,'spatial_db.sde','esrigdb.DBO.DataLines')).shapeType
'Polyline'
Related Question