Select to view content in your preferred language

draw rectangle to clip layers using pythonaddin

1054
5
Jump to solution
11-28-2012 08:50 PM
IbraheemKhan1
Occasional Contributor
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:

[HTML]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[/HTML]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ArkadiuszMatoszka
Frequent Contributor
Hi,
Corrected code for onRectangle below.
functions from management toolbox returns nothing so assignment like:
ras1 = arcpy.Clip_management

What is more function arcpy.Clip_management takes one string as second argument, not 4 floats.

def onRectangle(self, rectangle_geometry):         extent = rectangle_geometry         arcpy.Clip_management('D:\\DATA\\temp\\trans.tif', "%f %f %f %f" % (extent.XMin, extent.YMin, extent.XMax, extent.YMax), 'ras1', "#", "#", "NONE")         arcpy.RefreshActiveView()

Cheers.
Arek

View solution in original post

0 Kudos
5 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,
Corrected code for onRectangle below.
functions from management toolbox returns nothing so assignment like:
ras1 = arcpy.Clip_management

What is more function arcpy.Clip_management takes one string as second argument, not 4 floats.

def onRectangle(self, rectangle_geometry):         extent = rectangle_geometry         arcpy.Clip_management('D:\\DATA\\temp\\trans.tif', "%f %f %f %f" % (extent.XMin, extent.YMin, extent.XMax, extent.YMax), 'ras1', "#", "#", "NONE")         arcpy.RefreshActiveView()

Cheers.
Arek
0 Kudos
IbraheemKhan1
Occasional Contributor
Thanks for suggestion but tool is unresponsive and nothing happens upon clicking it.
0 Kudos
ArkadiuszMatoszka
Frequent Contributor
Code was tested before I submitted it, and worked fine. Maybe problem lays somewhere else. Have you checked paths to files?
Does python window in arcmap shows any error message when you activate tool or using it?
Regards.
0 Kudos
markdenil
Frequent Contributor
In my (rather limited) experiance of Python Add-Ins, they simply don't always work.
The code will run as a toolbox tool...
It will run if fired off from the Python window...
But sometimes it just will not run consistantly from the blasted button.
0 Kudos
IbraheemKhan1
Occasional Contributor
There is no error message. When i click on tool it simply don't fire. I am not sure where problem lies.
0 Kudos