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.
Thanks Adam for the tips ! I'm going to take a look
This is not helpful to the problem, but just curious why the choice for a python toolbox.
I just haven't found a reason to do this yet.
I am currently looking into your problem though.
I create this python toolbox to facilitate a big number of tasks by automating a sucession of tools.
But I find out the solution thanks to all for your advices.
I was just complicating myself with a tons of parameters.
Thanks everyone!
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
I agree that Python toolboxes (.pyt) are not always the best choice -- and definitely not for beginners. I too have not found a compelling case to go there yet.
The main advantages I see as this point are 1) better capability in parameter validation (especially with complex parameter inputs like value tables and 2) a fully-python solution for people that are very fluent in that environment. Some downsides include all the external files .pyt requires (each tool documentation is a separate XML file) and more difficult debugging (for most people).
I totally recommend you going back to the tbx way of doing it.