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')
Solved! Go to Solution.
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')
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.
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')