2

I have a stand alone script that I am simply trying to get a record count from a layer in an mxd. I continue to get '-1' as the output with a layer that has 1200+ records. The data source is an SDE feature class within a feature dataset. When I try to print the data source I only get the name of the feature dataset and the feature class. I think the issue is because of this? I need to have the complete path seeing as this script will be run by other people on their own machines. Here is the portion of the code that prints the data source and tries to get the count:

import arcpy
mxd_path = r'C:\Projects\Test2.mxd'
mxd = arcpy.mapping.MapDocument(mxd_path)
df  = arcpy.mapping.ListDataFrames(mxd)[0]
layer = arcpy.mapping.ListLayers(mxd, 'Test Boundary', df)[0]
print layer.dataSource
print int(arcpy.GetCount_management(layer.getOutput(0))

The first print statement returns

'/LAND.Boundaries/LAND.TestBoundary`

The second print statement returns

-1

The path and names of the mxd and layer are correct and exist. I think this is really weird that it won't include the sde connection file path with the layer.dataSource? I haven't found other answers to this specific problem I saw similar threads here

https://geonet.esri.com/thread/48767

However this only indicates using the describe with layer.dataSource. if I try to run any arcpy/geoprocessing on the layer it says this is not a valid parameter. For example I can't create a feature layer with MakeFeatureLayer_management with the layer returned in the stand alone script. I can run it in the Python command window successfully but unfortunately need to solve it for the stand alone script per my situation. Anyone seen this same issue of not returning the full path of the layer's data source?

  • What prints if you do a print layer.workspacePath to test? – Midavalo Aug 19 '16 at 19:19
  • Also try arcpy.Describe(layer).catalogpath or import os and try os.path.abspath(layer). Does that provide anything? – Sethdd Aug 19 '16 at 19:47
  • If I do print layer.workspacePath I get an empty string returned. Just a blank line. – bighill Aug 22 '16 at 16:04
  • You have two issues here, and I'm not sure they are related. Your layer variable is already a layer, so simply print arcpy.GetCount_management(layer) should work. As to not getting the full data source path, I have layers in some mxds that behave the same way, but I'm not sure why. Try opening a new blank mxd and adding the layer giving you trouble (using the same connection, but don't drag it from the old mxd), and then run the script on that new mxd. Do you have the same issue, or is the full path returned? – Dan Jurgella Aug 22 '16 at 18:23
  • The more I look at the data source issue, I think it has to do with the version being changed after the layer was added. All my layers that behave this way were added, and then had their version changed for editing. – Dan Jurgella Aug 22 '16 at 18:45
2

I finally figured out what was causing the problem. If I create a new blank mxd, add the feature class, and save the mxd the stand alone script runs fine. The script recognizes the layer, will return the complete full path, run a describe, GetCount_managment, and other geoprocessing functions on that layer. I came to find out that the mxd the script reads from was originally saved while connected to ArcFM. ArcFM "remaps" the data source paths and essentially breaks the links as far as arcpy is concerned (e.g. layer.isBroken = True). This is why it would work in my new mxd, not connected to ArcFM, and not for the original mxd and script. By fixing the workspace path layer.findAndReplaceWorkspacePaths("", new_workspace_path) it will allow the stand alone script to function properly on the layer(s) in the mxd.

Thanks for the comments and info. I apologize I didn't know the ArcFM part initially. Hopefully this will help someone out if they come across this in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.