Layer.workspacePath returns wrong value

503
4
02-08-2013 03:52 AM
JoseSousa
Esri Contributor
Hi,

I am using ArcGIS for Desktop 10.1 SP1

I have created a database connection with name Test1 and stored in a directory as follows:
C:\Temp\Test1.sde

I right clicked the connection file in ArcCatalog --> General Tab --> Looked into the property name and noticed that it says C:\Temp\Test1.sde which is fine.

If I copy with ArcCatalog/Catalog Window the connection and place it in some other location I can confirm that these values are updated.

However, whenever you copy this using Windows Explorer these values don't change, probably as expected.

I have noticed though, that when you get the property layer.workspacePath that this value seems to be returning the property name instead of the actual location (fullname) where the file is now placed.

This is problematic because I am determining at runtime which are the different sources in the map looping through each layer and this creates an issue. Is there a workaround for this?

Thanks in advance,
Jose
Tags (2)
0 Kudos
4 Replies
JeffMoulds
Esri Contributor
Instead of looking at the workspacePath for sde layers, you can look at the database connection properties. That will give you useful, accurate information about the sde layers. For example:

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("SERVICEPROPERTIES"):
        servProp = lyr.serviceProperties
        print "Layer name:" + lyr.name
        print "-----------------------------------------------------"
        if lyr.serviceProperties["ServiceType"] == "SDE":
            print "Service Type: " + servProp.get('ServiceType', 'N/A')
            print "    Database:       " + servProp.get('Database', 'N/A')
            print "    Server:         " + servProp.get('Server', 'N/A')
            print "    Service:        " + servProp.get('Service', 'N/A')
            print "    Version:        " + servProp.get('Version', 'N/A')
            print "    User name:      " + servProp.get('UserName', 'N/A')
            print "    Authentication: " + servProp.get('AuthenticationMode', 'N/A')
            print "" 
0 Kudos
JoseSousa
Esri Contributor
Hi Jeff,

Thanks for your reply. That would be fine if I had any interest on getting the connection details of each layer.
What I need is to get the distinct workspaces on each data frame of the map. There should be a method on the DataFrame object to do this.

This is vital because the database connection is an input of several arcpy methods including registering the data sources with ArcGIS for Server, fixing data sources, changing the data sources between publisher and server, changing the version, and so on. Also note that ArcGIS for Server may need access to this file. That is why it is important that the location of the sde connection is determined dynamically as the location of this file changes often depending on the scenario.

Users can add/remove layers from different data sources and I need to determine at runtime the different *.sde connections on the data frame. These are used for lots of different tasks.

I wouldn't mind if I could get the connection details as you described before and re-create the sde connection from those details. However, those details don't include the database password as you know and I don't know that in advance as the script is database agnostic ...

According to the documentation the layer.workspacePath should provide the location of the connection file for supported layer types.

Hope this makes sense,
Jose
0 Kudos
JeffMoulds
Esri Contributor
When you say:
I have noticed though, that when you get the property layer.workspacePath that this value seems to be returning the property name instead of the actual location (fullname) where the file is now placed.
Are you referring to arcpy reporting the old .sde file location when its been copied via Windows Explorer?

If you have mxds where users have copied the .sde files via windows explorer, I dont know know a way to get at the new .sde file location as arcpy reports the original location. There can be other issues when trying to depend on the original .sde location - the mxd can be moved between computers or users, the .sde file can be renamed/moved/deleted, etc. In all these cases, the mxd will be fine, but the .sde file will be gone. The best thing would be to know the sde passwords for workflows that require a .sde connection, then build the connection on the fly with CreateArcSDEConnectionFile.
0 Kudos
JoseSousa
Esri Contributor
Hi Jeff,

Thanks again for your reply.
Yes. That was what I meant. I understand that getting the location of .sde file when the file is copied using Windows Explorer can be impossible to get.

That is why that property layer.workspacePath is useless and misleading.
Additionally, because the Map Document seems to be storing the connection details (since he can always open the connection) even when the sde file changes location I would suggest that Esri builds a method to provide the distinct connections on the map document.

As I mentioned before, I don't know (and don't want to) the passwords that the organization uses. That is on their control. What I need to get is the connection from the map document to access the data.

An example of use case would be registering all the data sources in the map document automatically with ArcGIS for Server, validating the data sources are registered properly with AGS for Server, etc. Note that when I validate the registered data sources ArcGIS for Server gives me the connection details and encrypts the password which is fine and the way to go.

You  can develop easily such a method, encrypt the password and return the connection back to the developer. Then any other method on arcpy that requires a connection would also accept an encrypted password.

Independently of the implementation this functionality is absolutely required otherwise it becomes difficult to completely automatize processes starting from any MXD and have them published in ArcGIS for Server.

Kind regards,
Jose Sousa
Esri NZ
0 Kudos