Select to view content in your preferred language

addin tool not selecting by location

2081
12
Jump to solution
04-13-2018 08:09 AM
KevinBell1
Regular Contributor

I'm making a python addin select tool that allows the user to draw a rectangle and then select points within it to then do stuff.  For testing I'm only counting how many points are selected and writing to a log file, but it always returns 0... What am I doing wrong here?

def onRectangle(self, rectangle_geometry):
   ext = rectangle_geometry

   array = arcpy.Array()
   array.add(arcpy.Point(ext.XMin, ext.YMin))
   array.add(arcpy.Point(ext.XMin, ext.YMax))
   array.add(arcpy.Point(ext.XMax, ext.YMax))
   array.add(arcpy.Point(ext.XMax, ext.YMin))
   array.add(arcpy.Point(ext.XMin, ext.YMin))
   polygon = arcpy.Polygon(array)
   arcpy.CopyFeatures_management(polygon, "in_memory//Polygon_Extent")
   arcpy.MakeFeatureLayer_management("in_memory//Polygon_Extent", 'selectLyr')

   arcpy.MakeFeatureLayer_management('events', 'eventsLyr')

   arcpy.SelectLayerByLocation_management('eventsLyr', 'INTERSECT','selectLyr')
   n = arcpy.GetCount_management('eventsLyr')

   f = open(r'E:\ArcMap\BuildingServices\20180411_misassignedInspectors\addin\log.txt','a')
   f.write(str(n) + '\n')
   f.close()

   arcpy.Delete_management("in_memory//Polygon_Extent")
   arcpy.Delete_management('selectLyr')
   arcpy.Delete_management('eventsLyr')

Tags (2)
0 Kudos
12 Replies
RandyBurton
MVP Alum

As a test, do you get a count if you check just before the SelectByLocation?

arcpy.MakeFeatureLayer_management('events', 'eventsLyr')

# check here to see if layer has a count
n = arcpy.GetCount_management('eventsLyr')
print n

arcpy.SelectLayerByLocation_management('eventsLyr', 'INTERSECT','selectLyr')
KevinBell1
Regular Contributor

Yes!  I do get a count if I forego the selection.  If I do the select by location the count is zero, with out it 258 which is correct.

0 Kudos
KevinBell1
Regular Contributor

ESRI Tech support got me going!  

def onRectangle(self, rectangle_geometry):

   arcpy.MakeFeatureLayer_management('events', 'eventsLyr')

   arcpy.SelectLayerByLocation_management('eventsLyr', 'INTERSECT',rectangle_geometry.polygon)
   n = arcpy.GetCount_management('eventsLyr')
   f = open(r'E:\ArcMap\BuildingServices\20180411_misassignedInspectors\addin\log.txt','a')
   f.write(str(n) + '\n')
   f.close()

   arcpy.Delete_management('selectLyr')
   arcpy.Delete_management('eventsLyr')

0 Kudos