4

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
  • 1
    I'm confused - your question is tagged arcgis-10.0 but you mention pythonaddins and python27 which are both 10.1. Also, even assuming you are using 10.1, "The pythonaddins module can only be used within a Python add-in. It cannot be used in stand-alone scripts or geoprocessing script tools.", so can you confirm that you are NOT trying to do the latter? – PolyGeo Dec 17 '12 at 5:04
  • You are right, it should be 10.1. I am using it within Python add-in, not as stand-alone script. – Ibe Dec 18 '12 at 7:02
  • I am not sure about it. Isn't Extent already an Envelop? Maybe you need to pass the extent only. – Emi Jan 14 '13 at 9:42
2

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()
0

try to put your rectangle dimensions in strings as:

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

i hope it helps you..

  • Thanks for suggestion but tool is unresponsive and nothing happens upon clicking it. – Ibe Nov 30 '12 at 0:11
  • I got it working myself. – Ibe Jan 24 '13 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.