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
3
  • 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
    Commented Dec 17, 2012 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
    Commented Dec 18, 2012 at 7:02
  • I am not sure about it. Isn't Extent already an Envelop? Maybe you need to pass the extent only.
    – Emi
    Commented Jan 14, 2013 at 9:42

2 Answers 2

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
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..

2
  • Thanks for suggestion but tool is unresponsive and nothing happens upon clicking it.
    – Ibe
    Commented Nov 30, 2012 at 0:11
  • I got it working myself.
    – Ibe
    Commented Jan 24, 2013 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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