2

I have asked similar question at stackoverflow but posting here as well since it involves ArcGIS and python Add-In. Following code shows first step where i have to declare output folder as global so that later outputs can be saved in it as well. Right now I am getting an error at output folder string r'optfile/ras1'. Any help how to correctly store files in output folder and declare it as global would be appreciative.

import arcpy
import os
import pythonaddins

from datetime import datetime

now = datetime.now()
month = now.month
year = now.year

optfile = "C:/temp/"+str(year)+"_"+str(month)

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

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
    arcpy.Clip_management(r'D:/test', 
                          "%f %f %f %f" % (extent.XMin, extent.YMin, extent.XMax, extent.YMax), 
                          r'optfile/ras1', "#", "#", "NONE")
    arcpy.RefreshActiveView()
2
  • The cross-posting for this has been answered on StackOverflow: stackoverflow.com/questions/14536176/… so this question should be closed.
    – blah238
    Commented Jan 27, 2013 at 9:49
  • Not exactly. Use of global variables within python Add-In is still not working. I created a new function within Add-In class to declare string and required variables as global but no avail.
    – Ibe
    Commented Jan 27, 2013 at 18:24

1 Answer 1

4

In the line where you call arcpy.Clip_management you reference r'optfile/ras1' which is a string, you want it to be:

os.path.join(optfile,ras1)

so that you can make use of the variable.

3
  • It gave an error saying "ras1 is not defined". It worked with: optfile + '/ras1'
    – Ibe
    Commented Jan 26, 2013 at 18:42
  • 1
    @Ibe, you need to learn the difference between variables and string literals. It is a very important distinction in programming.
    – blah238
    Commented Jan 27, 2013 at 9:43
  • BTW, os.path.join(optfile,"ras1") should work presuming you want the output raster to always be named ras1.
    – blah238
    Commented Jan 27, 2013 at 9:45

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.