Select to view content in your preferred language

Get workspace from layer object

1644
4
Jump to solution
11-17-2022 03:28 PM
BlakeTerhune
MVP Regular Contributor

I'm creating a Python toolbox in ArcGIS Pro that will start an edit session on a layer in the map. In order to create the edit session, a workspace is needed. How can I derive the workspace from a layer in the map to use for the edit session? Nothing in Layer properties or Describe seems to work.

Since the layer is in an enterprise geodatabase (Oracle), the only solution I see right now is to get the connection_info from the layer object's connectionProperties and create a new sde connection file in a temp folder. I'm hoping there's a better way.

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi Blake,

I'm wondering- did you try the arcpy.da.Describe() method?  It has catalogpath and path properties that you could use to parse out the path to the workspace.

 

lyrDesc = arcpy.da.Describe(r'layer')
workspace = '\\'.join(lyrDesc["path"].split('\\')[:-1])

View solution in original post

0 Kudos
4 Replies
ccowin_odfw
Frequent Contributor

Probably not the most elegant solution, but that is how I connect with my SDE. I just have a folder of all of them and use os.path.join(path_to_SDE_text_file, feature_class_name) in the init of the Tool class.

0 Kudos
DeanAnderson2
Frequent Contributor

I am testing branched version environment in an ArcPro 30 environment.  This is what I have been using to get a layers workspace from the layer's datasource. Essentially you grab everything from the layers datasource before the last "/" as the datasource also contains the feature class name.   Not sure if it will work for you but seems to work for me. 

 

AnnoLayer = "Anno0200Scale"

thisProject = arcpy.mp.ArcGISProject("CURRENT")
Map = thisProject.activeMap
AnnoLyr = Map.listLayers(AnnoLayer)[0]
FolderPath = thisProject.homeFolder

datasource = AnnoLyr.dataSource
lastslash = datasource.rfind("/")
workspace = datasource[:lastslash]
arcpy.AddMessage(workspace) 

by Anonymous User
Not applicable

Hi Blake,

I'm wondering- did you try the arcpy.da.Describe() method?  It has catalogpath and path properties that you could use to parse out the path to the workspace.

 

lyrDesc = arcpy.da.Describe(r'layer')
workspace = '\\'.join(lyrDesc["path"].split('\\')[:-1])
0 Kudos
BlakeTerhune
MVP Regular Contributor

Apparently I was doing something wrong, but indeed the catalogPath from describe on the layer does allow the edit session to start. Thanks @Anonymous User!

In my case, the data source of the layer is in a feature dataset, so here's the method I used to parse out the path to the sde connection file.

catalogPath = arcpy.Describe(theLayer).catalogPath
# Traverse up the path to get the sde connection file or error at the end of the path.
while not catalogPath.endswith('.sde'):
    if catalogPath == os.path.dirname(catalogPath):
        raise OSError("This catalogPath does not have an sde connection file.")
    catalogPath = os.path.dirname(catalogPath)

with arcpy.da.Editor(catalogPath) as edit:
    # editing logic here
0 Kudos