3

I'm working on Python add-in that changes geometry of an object (polygon orthogonalization). The tool itself works properly when I give it an input line of layer name (or just drag layer from table of contents to script window).

Now I need to deal with single selected feature from unknown layer. For example I have 10 layers in the current mxd, edit session started for 5 of them and I need the one which feature is selected from.

How to get a layer name of current selected feature?

UPD. So what i have now. When tool works from toolbox it have an input parameter layer which is further converted to path:

layer = arcpy.GetParameterAsText(0)
dsc = arcpy.Describe(layer)
layerName = dsc.baseName
fd = dsc.path
layerPath = os.path.join(fd, layerName)

This works good when I execute this tool from toolbox for all the layer contents. This tool can also work with only selected features and I already tested it by replacing all the input with a strict line layer = "building" and adding this layer to TOC.

Now I'm trying to edit this tool to work as add-in with button.

What I need now is to know how to get a layer name when I simply select one polygone from some layer.

7

The pythonaddins module has a function to do just this, GetSelectedTOCLayerOrDataFrame():

>>> import pythonaddins
>>> sel = pythonaddins.GetSelectedTOCLayerOrDataFrame()
>>> print sel
Exported_Data
>>> 

enter image description here

  • Thank you! But it's not what I need to do. GetSelectedTOCLayerOrDataFrame returns a name of the highlighted layer from TOC. In my case I have a selected polygon from some polygon layer and I need to know the name of this layer. – Serge Norin Sep 20 '16 at 7:14
3

You may be able to modify this to suit your requirements. This loops through all layers in your MXD and checks if there's a selection. If it finds a layer with a single selected feature it will return the name of the layer.

mxd = arcpy.mapping.MapDocument("CURRENT") 
df = mxd.activeDataFrame()
layers = arcpy.mapping.ListLayers(mxd, "", df)

for layer in layers:
    if len(layer.getSelectionSet()) == 1:
        print layer.name

The Layer method getSelectionSet() returns a list of OIDs of selected records in the specified layer. Getting the length (len()) of that list will tell you how many features are selected.

2

Regarding to my old Answer: You can extend it to get what you are looking for:

mxd = arcpy.mapping.MapDocument("Current")

df = arcpy.mapping.ListDataFrames(mxd)

lyr = arcpy.mapping.ListLayers(df[0]) #if you have the requested layers in first dataframe
for i in lyr:

      ext1 = i.getExtent()
      x1max = ext1.XMax
      ext2 = i.getSelectedExtent()
      x2max = ext2.XMax

      if x1max==x2max:
         print "this layer has no selection : ", i.name
      else:
         print "this layer has selextion : ", i.name

The logic is simple take the extend of layer and compare it with selected extend, if it has changes the layer has selection.

Im one million percent sure that this logic works on terminal and on scripts, maybe it doesn't work on your addin.

  • Thank you! I'll try it. I thought about counting selected items, but it looks like serious harming overall processing time (layer could consist of more than 100k polygons). Getting extent should be faster. – Serge Norin Sep 20 '16 at 9:34
  • try to implement it on addins. I have never used addins, because i have found everything i have needed in scripts. if it works the only problem is when the layer has all rows selected, in this case the selected feature has the same extend of whole layer. – Florjan Sep 20 '16 at 9:57
  • Which is your "old answer"? It seems unnecessary to have two especially when one seems incomplete. – PolyGeo Sep 21 '16 at 9:41
  • @Florjan Also this method will fail if I select the rightmost polygon :) – Serge Norin Sep 21 '16 at 9:55
-1
mxd = arcpy.mapping.MapDocument("Current")

df = arcpy.mapping.ListDataFrames(mxd)

lyr = arcpy.mapping.ListLayers(df[0]) #if you have the requested layers in first dataframe
for i in lyr:

  ext1 = i.getExtent()
  x1max = ext1.XMax
  ext2 = i.getSelectedExtent()
  x2max = ext2.XMax

  if x1max==x2max:
     print "this layer has no selection : ", i.name
  else:
     print "this layer has selextion : ", i.name

Answer work for Addin too. It can be applied from 10.2 onwards. But getSelectionSet also does the same from 10.3 onwards.

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.