Select to view content in your preferred language

Select features using onRectangle

1117
1
12-06-2012 04:38 AM
HarryMcKenzie
Deactivated User
I found an interesting function to use in an add-in called onRectangle. I would like to use this to build a feature set and get values from fields.
def __init__(self):
    self.enabled = True
    self.cursor = 3
    self.shape = 'Rectangle'
    
def onRectangle(self, rectangle_geometry):
    """Occurs when the rectangle is drawn and the mouse button is released.
    The rectangle is a extent object."""

    extent = rectangle_geometry

Now that I have an extent how do I use it to select a feature or features and then get the values from field1, field2, etc...
Tags (2)
0 Kudos
1 Reply
MikeHunter
Frequent Contributor
After you get your extent, to select the features in a layer named 'Stands' intersecting the extent rectangle, do this:

    mxd = arcpy.mapping.MapDocument('CURRENT')
    df = mxd.activeDataFrame
    ext = df.extent
    lyrs = arcpy.mapping.ListLayers(mxd, "Stands", df)
    if lyrs:
        lyr = lyrs[0]
    else:
        return

    a = arcpy.Array()
    a.add(ext.lowerLeft)
    a.add(ext.lowerRight)
    a.add(ext.upperRight)
    a.add(ext.upperLeft)
    a.add(ext.lowerLeft)
    thepoly = arcpy.Polygon(a)

    arcpy.SelectLayerByLocation_management('Stands', 'Intersect', thepoly, 0, 'New_Selection')



You can read more about SelectLayerByLocation in the online help.  But it sounds like you really don't want to select features as much as grab some attributes.  If that is the case, then I'd set up a search cursor, curse through the records, and if the shape of a feature intersects the poly we made above, grab the attributes of interest.

good luck,
Mike
0 Kudos