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.
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')
Is everything in the same coordinate system? since getcount honors the extent environment parameter, so it may not be selecting anything because of that.
You would also be wise to add the extent parameters and anything else useful to your output text file
This isn't even working:
def onRectangle(self, rectangle_geometry):
ext = rectangle_geometry
f = open('log.txt','a')
f.write(ext.XMin, ext.XMax, ext.YMin, ext.YMax)
f.close()
How can I ensure the coordinate systems are the same. That's got to be it, but I'm stuck.
The map where the tool is used is in state plane, and the event layer is projecting on the fly from wgs 84. When I'm creating the feature layer for the events there's no option to specify a different coordinate system.
Keven... Projecting on the fly does nothing but make things look nice on screen and give you a false sense that things are as they should be.
Make the event layer a featureclass, then Project it to the appropriate projection ... or ... unproject the thing that you are trying to use in the selection process so it matches the decimal degree data. Once everything is in the same coordinate system (either projected or geographic) proceed with spatial selections.
Coordinate systems is GCS_WGS_1984 in the map, and I added
sr = arcpy.SpatialReference(4326)
polygon = arcpy.Polygon(array, sr)
And now I see in the properties that the coord sys are identical but I still get no entry in my log. I do log data from this addin's combo box selection change, but not this. Here's what I'm running.
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))
sr = arcpy.SpatialReference(4326)
polygon = arcpy.Polygon(array, sr)
arcpy.CopyFeatures_management(polygon, "in_memory//Polygon_Extent")
arcpy.MakeFeatureLayer_management("in_memory//Polygon_Extent", 'selectLyr')
arcpy.MakeFeatureLayer_management('events', 'eventsLyr')
f = open(r'E:\ArcMap\BuildingServices\20180411_misassignedInspectors\addin\log.txt','a')
f.write('what the hell?')
f.close()
arcpy.Delete_management("in_memory//Polygon_Extent")
arcpy.Delete_management('selectLyr')
arcpy.Delete_management('eventsLyr')
Nothing is written to the log, but the polygon is created in the map, so the def is running. : (
Print the coordinates... again, you can define anything what you want, it doesn't make it so.
And you are appending to an existing file in your file.open, is that correct?
Changed f.write('what the hell?') to f.write(ext.XMin, ext.YMin) and nothing is written to the log file that is open to append.
Kevin- not sure if this'll help, but here's a snippet I use to create a single polygon based on data frame extent:
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
frameExtent = df.extent
XMAX = frameExtent.XMax
XMIN = frameExtent.XMin
YMAX = frameExtent.YMax
YMIN = frameExtent.YMin
#
pnt1 = arcpy.Point(XMIN, YMIN)
pnt2 = arcpy.Point(XMIN, YMAX)
pnt3 = arcpy.Point(XMAX, YMAX)
pnt4 = arcpy.Point(XMAX, YMIN)
#
array = arcpy.Array()
array.add(pnt1)
array.add(pnt2)
array.add(pnt3)
array.add(pnt4)
array.add(pnt1)
Mypolygon = arcpy.Polygon(array)
arcpy.CopyFeatures_management(Mypolygon,"in_memory\\ExtentPoly")
in_poly_layer = arcpy.mapping.ListLayers(mxd,"ExtentPoly",df)[0]
Ya, that code from JSchiener (sp?) was what I started with before violating the zen of python by nesting it rather than keeping it flat!