Select to view content in your preferred language

Arcpy.mapping.AddLayer selection Funk (serious ESRI devs only)

5272
44
Jump to solution
02-05-2018 11:23 AM
JosephArmbruster
New Contributor III
Hey everyone,
It's been ages since i've had to resort to the forums, but the time has come...  This is issue is occurring with:
10.3.0.4322
AND
10.5.1.7333
Problem: When a layer is added to the TOC, just as you see in the snippet below, feature selections will no longer be respected (or at least detected) by geoprocessing tools.
Description: The symptom of the problem is that obtaining the feature selection count of a geoprocessing layer in a python toolbox is inconsistent.  I've pinned it down to some issue with the way Arc (or ArcPy) is handling layers by Name.  Here are some snippets:
# make this an absolute path to some feature class in a geodatabase, you only need a minimum of two features to reproduce the issue, i'm using polygons.

path = #path to your polygon feature class with at least two features in it.
mxd = arcpy.mapping.MapDocument('CURRENT')
new_layer_name = os.path.basename(path)
arcpy.MakeFeatureLayer_management(path, new_layer_name)
new_layer = arcpy.mapping.Layer(new_layer_name)
arcpy.mapping.AddLayer(mxd.activeDataFrame, new_layer_name, 'BOTTOM')
del new_layer
del mxd
Use the code above in a geoprocessing tool to add a layer to the map.

Now, use the code below in a separate geoprocessing tool, to report the number of features selected in the map.
desc = arcpy.Describe(input_feature_class)
if desc.FIDSet:
 messages.addMessage('selection made')
 # you'll never get here with the layer above!!
else:
 messages.addMessage('no selection made')
 # you'll always get here even with a selection 😞
# count will always be the entire feature class count, since no selection was registered
# to the layer....
feature_count = int(arcpy.GetCount_management(input_feature_class).getOutput(0)

Notes:
- If you use the code above to add the feature class to the map.  Remove it from the toc (right click, remove) and add it using arc catalog (drag-drop), the feature selection will STILL not work.  Hence why I suggest it has something to do with the internal name resolution.
- If you start a new ArcMap instance, add the layer by drag-dropping it from Arc Catalog, the selection WILL work.  If you remove this layer and add it using the code above, all subsequent selections will NOT be respected.

I look forward to hearing from ESRI support.
Let me know if you have any questions,
Joseph Armbruster
0Lat Inc, Orlando Florida.
0 Kudos
44 Replies
XanderBakker
Esri Esteemed Contributor

if I add the layer using this code (which is exactly yours apart from line 1 pointing to my featureclass):

path = r'C:\GeoNet\Selection\test.gdb\myLines'
mxd = arcpy.mapping.MapDocument('CURRENT')
new_layer_name = os.path.basename(path)
arcpy.MakeFeatureLayer_management(path, new_layer_name)
new_layer = arcpy.mapping.Layer(new_layer_name)
arcpy.mapping.AddLayer(mxd.activeDataFrame, new_layer_name, 'BOTTOM')
del new_layer
del mxd

This beautiful error is produced:

Runtime error
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\mapping.py", line 49, in AddLayer
assert isinstance(add_layer, Layer)
AssertionError

And the layer is added at the top:

So, your code is not working correctly...

JosephArmbruster
New Contributor III

Bah, copy/pasta typo... That line should be:

arcpy.mapping.AddLayer(mxd.activeDataFrame, new_layer, 'BOTTOM')

0 Kudos
XanderBakker
Esri Esteemed Contributor

Never seen a "typo" occur from copy and past. Might want to report it to Microsoft. 

JosephArmbruster
New Contributor III

Haaaaaar 🙂  That was a self inflicted post-copy-pasta wound...  But still a distraction from the real issue!!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Some last comments before I leave this thread to what it is:

  • You requested that only "Serious ESRI devs" should respond. Rebecca, Joshua an James are all MVP' with a large amount of experience with ArcGIS and Python
  • Rebecca asked you to provided the code using the syntax highlighter, which you haven't done, making it more difficult to detect problems in the code
  • You provided partial code using variable names that don't match the data type and aid misinterpretation of the code. Which led to the valid suggestion of Joshua that you cannot get the FIDSet of a featureclass.
  • I assume that you have been marking several answers as "Not helpful". If so, this is not inviting the users to keep helping you. 
  • If you are so sure that it is a software bug,  you should report it to support. 

Good luck on solving it.

JosephArmbruster
New Contributor III

MVP and GISP mean almost nothing to me personally.  There are people who make careers out of accumulating thumbs ups, which is what it is...  I've been doing this for years and typically do not have unlimited time to invest in forum interaction (reference the old, original esri forums...)

There are two people who attempted to provided material value so-far on this issue, you and Joshua, and for that Thanks!  Even with the silly typo, it's not difficult to reproduce this issue.  If you change that text to new_layer, you'll see 🙂

Yes, I have opened an issue with ESRI support and a working .pyt with both sides of the tool.  If there was an attach file option here, i'd use it!!

0 Kudos
DanPatterson_Retired
MVP Emeritus

here you go

JosephArmbruster
New Contributor III

Test code attached.

0 Kudos
JamesCrandall
MVP Frequent Contributor

If possible, implement arcpy.GetCount on the layer in the TOC rather than the desc.FIDSet.

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "SomeLayerName", df)[0]
getCountMethod = int(arcpy.GetCount_management(lyr).getOutput(0))
arcpy.AddMessage("getCountMethod: {}".format(getCountMethod))

That seems to work just fine, even after the layer was added programmatically from another GP tool.

0 Kudos
JosephArmbruster
New Contributor III

James,

I use the feature layer that was provided as an input to the tool within my function.  I shouldn't have to List and search for it again, if it's given to me

Also, I am not using the FIDSet for anything besides a little debug output.  You could remove the lines from desc = arcpy.Describe(input_feature_class), all the way down to the 'no selection made' message if you want... same issue will surface.

The problem has something to do with the way the layer was added... because any subsequent add of a layer with the same Name will experience the issue.

Joe

0 Kudos