[GIS] How to draw rectangle to clip layers using python addin in ArcGIS 10.1

arcgis-10.1arcpypython-2.7python-addin

I want to clip rasters in data view using interactive rectangle (to define Area of Interest) and add them to current dataframe. Following is a piece of code which is not returning anything. Any suggestions to correct it would be appreciative:

import arcpy
import pythonaddins

def __init__(self):
    self.enabled = True
    self.cursor = 1
    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

    ras1 = arcpy.Clip_management(r"C:/temp/ras",
                                extent.XMin extent.YMin extent.XMax extent.YMax,
                                ras1, "#", "#", "NONE")
    arcpy.RefreshActiveView()
    return ras1

Best Answer

Here is code snippet for people who would like to use it:

import arcpy 
import pythonaddins

class DrawRectangle(object):
    """Implementation for rectangle_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 1
        self.shape = 'Rectangle'

    def onRectangle(self, rectangle_geometry):
        extent = rectangle_geometry
        arcpy.Clip_management(r"data/iras",
                              "%f %f %f %f" % (extent.XMin, extent.YMin, extent.XMax, extent.YMax),
                              "oras", "#", "#", "NONE")
        arcpy.RefreshActiveView()
Related Question