Python script to a script tool in ArcGIS

2150
8
Jump to solution
08-11-2020 03:46 PM
Deepa
by
New Contributor III

Hi everyone,

I am new to Python and my question may be very basic  

I would like to convert my python code to a script tool in ArcGIS. The code used is written below and it worked fine and got the output raster named Stak123. But when i make this code into a script tool, i want the output name to be decided by the user who runs the tool. To solve this I was thinking if i add an output folder (Out_Fold) as GetParameterAsText and then in the tool i just put any new name in the output folder for the output raster. But when i ran it  in ArcGIS as a tool I got the message it is completed but could not find the output raster. How to fix this?

import arcpy
import os
import sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
env.workspace = r"H:\Stak1108"
arcpy.env.cellSize = "MINOF"
# Assigned paramater as text for the tool
Elevation = arcpy.GetParameterAsText(0)
Out_Fold = arcpy.GetParameterAsText(4)
Raster1 = Raster("Elevation")
# The conditionals were applied in the input rasters
Stak123 = Con(Raster1 = 1,3)........
#Saving the output
Stak123.save(r"H:/Stak1108/Out_Fold/Stak123")

Thanks.

Deepa

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

Try changing the last line from

Try.save(r"H:\Stak1108\Fresh\Try")

Have it use the locations specified by "save_raster" with this:

Try.save(save_raster)

View solution in original post

8 Replies
DanPatterson
MVP Esteemed Contributor

The Out_fold should be defined as a 'folder' in the tool's parameters and make it required. The person can then navigate to the folder.

If the folder doesn't exist, you would have to create one in your code


... sort of retired...
Deepa
by
New Contributor III

Thanks for the reply  Yes i have added the folder as a parameter in the tool with direction defined as output and required. Also i have the folder called Out_Fold in the Stak1108 main folder. I am adding the error message here from the tool and the screenshot of my Add script tool. "Failed script Lab3S...Traceback (most recent call last):File "H:\Stak1108\Stack10.py", line 20, in <module>Stak123.save(r"H:/Stak1108/Out_Fold/Stak123")RuntimeError: ERROR 999998: Unexpected Error"Screenshot of tool details

0 Kudos
RandyBurton
MVP Alum

Try changing the direction to input.  The folder location is provided by the user, as input.  It is not generated by the script, which would be output.

Deepa
by
New Contributor III

Yes i changed the Output folder direction as input. But then i am unable to give the name of the output i want in the tool. I have attached the image here.Screenshot of Tool

0 Kudos
RandyBurton
MVP Alum

Perhaps a folder input field and a save raster (string) input field.

Folder selection

Completed form

A simple demo script:

import arcpy, os

folder = arcpy.GetParameterAsText(0) # Input, Data Type 'Folder'
raster = arcpy.GetParameterAsText(1) # Input, Data Type 'String'

arcpy.AddMessage("Folder: {}".format(folder))
arcpy.AddMessage("Raster: {}".format(raster))

# save location
save_raster = os.path.join(folder, raster)
arcpy.AddMessage("Save Location: {}".format(save_raster))

'''
Output:
Executing: GetFolder C:\Temp\Rasters Stak123
Start Time: Wed Aug 12 19:14:29 2020
Running script GetFolder...
Folder: C:\Temp\Rasters
Raster: Stak123
Save Location: C:\Temp\Rasters\Stak123
Completed script GetFolder...
Succeeded at Wed Aug 12 19:14:29 2020 (Elapsed Time: 0.00 seconds)
'''
Deepa
by
New Contributor III

Thanks, i tried like you explained. It runs and tool says it is completed successfully but i cant find the output. This is the code i used . All i see is the Try(output raster from the code saved in the folder called Fresh). I dont find the new name(Final) which i gave in the tool in a different output folder(Old).  Also i made the direction as input for the folder and added raster as a string in the tool parameters.

Screenshot of Tool output

import arcpy
import os
import sys
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
env.workspace = r"H:\Stak1108"
arcpy.env.cellSize = "MINOF"
arcpy.env.overwriteOutput = True
#The parameters for tool
...... = arcpy.GetParameterAsText(0)
.....
folder = arcpy.GetParameterAsText(4)
raster = arcpy.GetParameterAsText(5)
# The Add messages
arcpy.AddMessage("Folder: {}".format(folder))
arcpy.AddMessage("Raster: {}".format(raster))
save_raster = os.path.join(folder, raster)
arcpy.AddMessage("Save Location: {}".format(save_raster))
Raster1 = Raster(.....)
Try = Con(Raster4 == 1,5)......
Try.save(r"H:\Stak1108\Fresh\Try")
0 Kudos
RandyBurton
MVP Alum

Try changing the last line from

Try.save(r"H:\Stak1108\Fresh\Try")

Have it use the locations specified by "save_raster" with this:

Try.save(save_raster)
Deepa
by
New Contributor III

This worked like a charm  

Thanks for helping me from the scratch! 

0 Kudos