|
POST
|
Pickle is one way. I use JSON though as it's more "human readable". import os, arcpy, json
class Tool(object):
def __init__(self):
#Save settings in %APPDATA%/arcgistools/{module}.settings
setdir = os.path.join(os.environ['APPDATA'], 'arcgistools')
if not os.path.exists(setdir):
os.makedirs(setdir)
self._settings = os.path.join(setdir, __name__+'.settings')
self._toolname = type(self).__name__
def get_settings(self):
# Structure of settings dict is {toolname1: {paramname: paramvalue}, toolname2: {paramname: paramvalue}}
try:
return json.load(open(self._settings, 'r'))[self._toolname]
except (OSError, KeyError, ValueError,IOError):
return {}
def save_settings(self, settings):
# Don't overwrite other tools settings
try:
existing = json.load(open(self._settings, 'r'))
except (OSError, KeyError, ValueError, IOError):
existing = {}
existing[self._toolname]=settings
json.dump(existing, open(self._settings, 'w'))
class Test1(Tool):
def __init__(self):
Tool.__init__(self)
self.label = "Tool1"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
settings = self.get_settings()
#First parameter
param0 = arcpy.Parameter(
displayName="Input Feature Class",
name="in_fc", #I use param.name as dict key
datatype=["Feature Class"],
parameterType="Required",
direction="Input")
param0.value = settings.get(param0.name, '')
params = [param0]
return params
def execute(self, parameters, messages):
"""The source code of the tool."""
settings = {}
for param in parameters:
settings[param.name] = param.valueAsText
self.save_settings(settings)
return
class Test2(Tool):
def __init__(self):
Tool.__init__(self)
self.label = "Tool2"
# etc...
... View more
03-14-2015
06:13 PM
|
0
|
0
|
1446
|
|
POST
|
Did you try arcpy.GetParameter(1) instead of GetParameterAsText? GetParameter returns the parameter as an object instead of a string.
... View more
03-10-2015
12:30 PM
|
0
|
3
|
3735
|
|
POST
|
What do you mean "not working"? The following works for me: def onRectangle(self, rectangle_geometry):
arcpy.env.workspace = r"IN_MEMORY"
arcpy.env.overwriteOutput = True
lyrname = 'randompts'
randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,lyrname,'',rectangle_geometry).getOutput(0)
mxd=arcpy.mapping.MapDocument('CURRENT')
adf = mxd.activeDataFrame
try:
lyr=arcpy.mapping.ListLayers(mxd, lyrname, adf)[0]
except IndexError:
lyr=arcpy.MakeFeatureLayer_management(randompts, lyrname).getOutput(0)
arcpy.mapping.AddLayer(adf,lyr)
lyr=arcpy.mapping.ListLayers(mxd, lyrname, adf)[0]
arcpy.RefreshActiveView()
... View more
03-01-2015
02:30 PM
|
0
|
1
|
1607
|
|
POST
|
If the shapefile doesn't exist, the "randompts = arcpy.CreateRandomPoints_management" line will never get called, so when you "return randompts", the randompts variable has not been defined and you will get the UnboundLocalError. Do this instead: def onRectangle(self, rectangle_geometry):
extent = rectangle_geometry
arcpy.env.workspace = "c:\ArcpyBook\Ch11"
if arcpy.Exists("randompts.shp"):
arcpy.Delete_management("randompts.shp")
randompts = arcpy.CreateRandomPoints_management(arcpy.env.workspace,"randompts","randompts.shp","POINT",rectangle_geometry)
arcpy.RefreshActiveView()
return randompts
... View more
02-28-2015
11:04 PM
|
0
|
3
|
1607
|
|
IDEA
|
-->
So we can check if an extension has already been checked out manually and not check it back in at the end of the script. See https://geonet.esri.com/thread/121431 and http://gis.stackexchange.com/q/133906/2856 for more info.
... View more
02-10-2015
03:07 PM
|
3
|
2
|
725
|
|
POST
|
I'm writing a python add-in that uses a Spatial Analyst function and hence requires the Spatial Analyst extension to be enabled. I would like to be able to check out the extension only if it hasn't already been manually checked out by the user (i.e. via the "Customize->Extensions" menu). My code can check out the extension and then check it back in easily enough. However, this causes an issue if a user has already checked the extension out manually via the "Customize->Extensions" menu as they will suddenly start getting the "ERROR 000824: The tool is not licensed" error when trying to use other Spatial Analyst tools/functions (as my add-in has checked the extension back in). A further complication that will confuse the user is that when they open the "Customize->Extensions" menu to check if the SA extension is checked out, the tick will still be in the extensions check box as checking the extension in via code does not update the GUI. E.g see screenshot below There doesn't seem to be any way of testing whether the extension is already checked out out, i.e the return values for arcpy.CheckExtension are only Available, Unavailable, NotLicensed or Failed. My work around is to wrap the use of the spatial analyst tool in a try:except: clause (see below), but it's ugly and is not useful for larger scripts that use more SA tools (SA objects/functions especially) as I have to code the syntax of the tools twice. try:
result = Sample(rasters,features,table)
except ExecuteError as e:
if 'ERROR 000824' in e.message: # "ERROR 000824: The tool is not licensed"
if arcpy.CheckExtension('Spatial') == 'Available' and arcpy.CheckOutExtension('Spatial')=='CheckedOut':
result = Sample(rasters,features,table)
arcpy.CheckInExtension('Spatial')
else:
pythonaddins.MessageBox('Spatial Analyst license not available', 'Error', 0)
return
else:raise Q: Is there a better way of only checking an extension out if it is not already checked out...?
... View more
02-08-2015
05:51 PM
|
0
|
2
|
6404
|
|
POST
|
Can you be more specific about what's "going wrong" - what's your problem, do you get any error messages, etc...? The 2nd example in the Help is quite useful.
... View more
01-18-2015
05:18 PM
|
0
|
1
|
1118
|
|
POST
|
Justin Kraemer wrote: Hi Warren. I had posted another thread on this, and neglected to mention so on this one. My apologies for that. You can find the answered thread at ListFeatureClasses() or ListDatasets() fails since 10.2 upgrade, where you'll see it was an empty mosaic dataset that was found to be the troublemaker. And this is why cross-posting is poor form...
... View more
01-14-2015
04:51 PM
|
0
|
0
|
841
|