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
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.
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!
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.
Thanks Adam for the tips ! I'm going to take a look
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:
http://resources.arcgis.com/en/help/main/10.1/index.html#/Accessing_parameters_in_a_script_tool/001500000007000000/
Thank you very much for the advice Sol,
I'm going to see if they can help me in the python forum.
Sandrine
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.
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) ......
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.
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")
?
InRaster = arcpy.GetParameterAsText(0)
Output = arcpy.GetParameterAsText(1)
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.