2

As part of the validation for parameters of a Python toolbox, I need to check if the text in parameter #1 is exists in a layer in a MXD file selected as parameter #2. Currently, I use this code in updateMessages:

mxd = arcpy.mapping.MapDocument(parameters[2].valueAsText)
layer = arcpy.mapping.ListLayers(mxd, "LayerName")[0]
condition = u"Field=" + parameters[1].text
with arcpy.da.SearchCursor(layer.dataSource, ["Field"], condition) as cursor:
    #Do some stuff...

The problem is that this takes very long time since the rather large MXD file has to be opened. It is not acceptable to have a two minute waiting time with ArcMap freezing every time the parameter validation is performed.

So my question is this: Is there any way to find the source of the layer, and search for a value in it, without having to open the MXD?

2
  • 1
    I had the same issue with an mxd having many data frames and data source connections to various drives, and had no luck to resolve this issue. In fact if you time it, most probably you will see that ListLayers is the problem.
    – fatih_dur
    Aug 8, 2015 at 2:40
  • @fatih_dur You are right that it is ListLayers that takes most of the time. I managed to reduce the time by reducing the number of layers (in my case, by merging rasters).
    – Anders
    Aug 10, 2015 at 7:21

1 Answer 1

1

Unless your layer definitions are static enough that you can keep them also saved as layer files, then I think you are stuck with creating a MapDocument object in order to locate its layer definitions.

If you are able to use a layer file instead of using a map file to get at the layer definitions then just use the specific Layer object instead of creating a MapDocument object, and it should be much quicker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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