addin:  bad selection from tool

548
3
Jump to solution
06-23-2014 09:32 AM
KevinBell
Occasional Contributor III
I'm trying to get the id's of polygons that I select with a Python addin tool, but it is returning all the features, no just my selection.  Any ideas?

import arcpy import pythonaddins  global coords, mxd, df, ids coords = [] ids = []  mxdPath = r'E:\gis\Atlas\Atlas.mxd' mxd = arcpy.mapping.MapDocument(mxdPath)  class DrawPolygon(object):     """Implementation for searchAtlas_addin.drawPolygonTl (Tool)"""     def __init__(self):         self.enabled = True         self.shape = "Line"       def onLine(self, line_geometry):             array = arcpy.Array()             part = line_geometry.getPart(0)             for pt in part:                 array.add(pt)                             polygon = arcpy.Polygon(array)             getBlockIDs(polygon)   def getBlockIDs(inPolygon):     print 'getting block ids'     fc = r'E:\gis\Atlas\default.gdb\blocks'     arcpy.MakeFeatureLayer_management(fc, 'blocksLyr')     arcpy.MakeFeatureLayer_management(inPolygon, 'aoiLyr')     arcpy.SelectLayerByLocation_management('blocksLyr', 'INTERSECT', 'aoiLyr')          with arcpy.da.SearchCursor(fc, 'BlockNumber') as c:         for r in c:             ids.append(r[0])          print ids
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
You are only going through the feature class with the search cursor, not the feature layer.  You cannot have a selection on the feature class itself, but only a feature layer or table view.

with arcpy.da.SearchCursor(fc, 'BlockNumber') as c:         for r in c:             ids.append(r[0])


needs to be:
with arcpy.da.SearchCursor('blocksLyr', 'BlockNumber') as c:         for r in c:             ids.append(r[0])


Also, I would recommend setting up the feature layers differently and grab the output of the make feature layer tool:

block_lyr = arcpy.MakeFeatureLayer_management(fc, 'blocksLyr').getOutput(0) .... .... with arcpy.da.SearchCursor(block_lyr, 'BlockNumber') as c:         for r in c:             ids.append(r[0])

View solution in original post

0 Kudos
3 Replies
by Anonymous User
Not applicable
You are only going through the feature class with the search cursor, not the feature layer.  You cannot have a selection on the feature class itself, but only a feature layer or table view.

with arcpy.da.SearchCursor(fc, 'BlockNumber') as c:         for r in c:             ids.append(r[0])


needs to be:
with arcpy.da.SearchCursor('blocksLyr', 'BlockNumber') as c:         for r in c:             ids.append(r[0])


Also, I would recommend setting up the feature layers differently and grab the output of the make feature layer tool:

block_lyr = arcpy.MakeFeatureLayer_management(fc, 'blocksLyr').getOutput(0) .... .... with arcpy.da.SearchCursor(block_lyr, 'BlockNumber') as c:         for r in c:             ids.append(r[0])
0 Kudos
KevinBell
Occasional Contributor III
: \  Brain stall!  Thanks Caleb!
0 Kudos
by Anonymous User
Not applicable
: \  Brain stall!  Thanks Caleb!


Happens to me all the time!
0 Kudos