I'm very new at python, and I don't understand how to use the defined parameters in the source code of the tool.
I'm trying to create a tool (with clip and contour tools) who can be used by any user (not specified folders or classes). My script is in attachment.
Can somebody take a look ? :S
Thanks
Solved! Go to Solution.
To get you started, have a look at the code below. Use this script by adding it as a script to a new toolbox.
Define the 5 parameters as explained on line 18 - 22
#-------------------------------------------------------------------------------
# Name: clip_contour.py
# Purpose: clip raster and create contours
#
# Author: Sandrine Carmet
#
# Created: 08/08/2014
#-------------------------------------------------------------------------------
class LicenseError(Exception):
pass
def main():
import arcpy
import os
# read the parameters
fc_extent = arcpy.GetParameterAsText(0) # define as featurelayer, filter polygon, direction input
ras_in = arcpy.GetParameterAsText(1) # define as rasterlayer, direction input
ras_clip = arcpy.GetParameterAsText(2) # define as raster dataset, direction output
fc_contour = arcpy.GetParameterAsText(3) # define as featureclass, direction output
interval = arcpy.GetParameter(4) # define as Double, set min and max range, provide default
# configuration of contours
contour_interval = interval # not 4...
base_contour = 0
z_factor = 1
try:
if arcpy.CheckExtension("3D") == "Available":
arcpy.CheckOutExtension("3D")
else:
# raise exception
raise LicenseError
# set in memory workspace
arcpy.env.workspace = "IN_MEMORY"
# Determine the extent of the fc
extent = arcpy.Describe(fc_extent).extent
ext_txt = "{0} {1} {2} {3}".format(extent.XMin, extent.YMin, extent.XMax, extent.YMax)
# Clip the raster
arcpy.Clip_management(ras_in, ext_txt, ras_clip, fc_extent, "#", "ClippingGeometry")
# Create the contours
arcpy.Contour_3d(ras_clip, fc_contour, contour_interval, base_contour, z_factor)
# return license
arcpy.CheckInExtension("3D")
# outputs are automatically added to the TOC...
except LicenseError:
print("3D Analyst license is unavailable")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
if __name__ == '__main__':
main()
Once you run the script successfully, you can go to the Results windows and see the syntax for executing the script.
# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "pols", "DEMsmpro"
arcpy.ClipRasterCreateContours("pols","DEMsmpro","D:/Xander/grd/test_clip","D:/Xander/tmp/test2.gdb/test_contours","50")
You could create another script that simply executes this script or change the script to work with a list of inputs.
Kind regards, Xander
InRaster = arcpy.GetParameterAsText(0)
Output = arcpy.GetParameterAsText(1)
humm this is not the answer that I was looking for...But thanks for answering me back
When I start my script, there is an Error:
"Traceback (most recent call last):
File "<string>", line 88, in execute
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 12021, in Clip
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (Clip)."
So how can I get the extent from my shapefile to make my clip tool work:
arcpy.Clip_management(RasterWorkspace + "\\" + RasterFile, "#", RasterWorkspace + "\\" + OutRasterFile, inputWorkspace + "\\" + ShapeFileName + ".shp", "0", "ClippingGeometry")
?
Hi Sol,
Yes I've already done that. But the problem is that I want to generalize my script, so that the user of my tool will be able to choose is own shapefile (for extent to the clip tool) and his raster file. But I can't find the properly form to insert my parameters in my tool.
Can you have a look to my script in attachment above, to see if you have any ideas ?
Thanks for helping.
Thank you very much for the advice Sol,
I'm going to see if they can help me in the python forum.
Sandrine
In addition to the export out of a model you could print the variables you put in the clip tool to see if there's something wrong:
arcpy.AddMessage(RasterWorkspace + "\\" + RasterFile) ......
The messages didn't show me something wrong.
It seems that the problem come from the shapefile (inputWorkspace + "\\" + ShapeFileName + ".shp") that I used as an extent for the Clip tool.
Maybe I should use an other function to obtain the extent values from the shapefile, and then put it directly in the clip tool. But I don't know how to do that... I'm going to continue my research.
As I can see it's all OK, so I don't know what could be wrong.
It's quite sophisticated what you are doing. Twice the use of parameters[0].valueAsText? One time .valueastext = not correct?
I would try to put the shape in the clip syntax hard coded, to see if it works?
I think I went to far with my script by trying things. I'm going to simplify it as possible.
For the clip, I'll try what you say to see if it works !
Thankss Johannes!
These links should help:
Create the custom script tool and define the input parameters via the Add Script Tool Wizard.
http://resources.arcgis.com/en/help/main/10.1/index.html#/Adding_a_script_tool/00150000001r000000/
Access the input parameters and plug them into your Clip_management code: