How to save a raster with a script tool input?

374
6
Jump to solution
02-21-2019 01:52 PM
Highlighted
New Contributor III

I'm learning python and have been banging my head on this for hours now and can't figure it out. It's probably something really basic but here it goes...

I have a base elevation raster that I'm running some math on to get a basic ruggedness layer. The script works when I run it in the python window, but I'm trying to use it to create a script tool. Here is what I have simplified:

outWorkspace = arcpy.GetParameterAsText(0)
baseRaster = arcpy.GetParameterAsText(1)
scale = arcpy.GetParameterAsText(2)
outRaster = arcpy.GetParameterAsText(3)

env.workspace = outWorkspace

###lots of math on the baseRaster

FinalResult = #more math

finalResult.save()

When I run it like this, the tool runs but it doesn't appear to save my file in the workspace specified (or anywhere for that matter, I can't find the results of the tool). So I guess that's issue #1.

Second question is how do I script this so that the name of FinalResult is whatever the user puts in for outRaster, instead of FinalResult?

In case it is needed, my parameters that I have set up in my script tool are as follows:

0 = Workspace (data type), input

1 = Raster Dataset, Input

2 = Long, Input

3 = Raster Dataset, Output

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Regular Contributor

I would suggest the same thing as Curtis...

Get your output location as a parameter. Don't count on the arcpy.env.workspace setting and pass your full path...

something like:

final_raster.save(output_path)

Raster—ArcPy classes | ArcGIS Desktop 

One thing that I find helps a lot is to get everything working in a python IED with hardcoded parameters before turning it into a "script tool". That way you can take advantage of debuggers, etc. (helps cut down on the black box effect).

https://www.jetbrains.com/pycharm/download/#section=windows   

GitHub - pyscripter/pyscripter: Pyscripter is a feature-rich but lightweight Python IDE 

View solution in original post

6 Replies
Highlighted
MVP Esteemed Contributor

you need an output workspace (make it a folder not a gdb)

and an output filename (raster.tif  for example)

Concatenate the workspace and the output filename prior to saving

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Ryan:

I think your raster is saving but not at the location specified as outRaster.

If this is set up as a script tool, outRaster will be full path. So, this statement:

finalResult.save(outRaster)

should save the output in the location (and format) specified in the script tool dialog box.

Highlighted
New Contributor III

Thank you both for your reply. After another 45 min- 1 hour of banging my head this is where I'm at:

Dan:

I've changed my workspace (from the user input end of the script tool) to a folder rather than a gdb. I changed the outRaster to outRaster.tif (again in the user end).

I guess I'm unsure how to call my FinalResult and save it in the concatentated file path.

FinalResult.save(concatenatedVariable) ?

When I try this, I get a 000875 error saying the workspace is an invalid output workspace.

Curtis:

I've tried the line you added, but I get a 010240 error that it could not save the raster dataset with output format FGDBR.

When I run the script in the python window it adds my final raster to my map and it has a temporary location. If I just do

FinalRaster.save()

in the python window it saves in my workspace without any issues.

Thanks again to both of you for your input and patience with my very basic knowledge  

Reply
0 Kudos
Highlighted
MVP Regular Contributor

I would suggest the same thing as Curtis...

Get your output location as a parameter. Don't count on the arcpy.env.workspace setting and pass your full path...

something like:

final_raster.save(output_path)

Raster—ArcPy classes | ArcGIS Desktop 

One thing that I find helps a lot is to get everything working in a python IED with hardcoded parameters before turning it into a "script tool". That way you can take advantage of debuggers, etc. (helps cut down on the black box effect).

https://www.jetbrains.com/pycharm/download/#section=windows   

GitHub - pyscripter/pyscripter: Pyscripter is a feature-rich but lightweight Python IDE 

View solution in original post

Highlighted
New Contributor III

Getting rid of 

env.workspace = outWorkspace

and then doing

FinalRaster.save(outRaster)

ended up fixing all of my problems. I guess it was setting the workspace that was causing me issues.

Thanks!

Reply
0 Kudos
Highlighted
MVP Regular Contributor

Nice! Don't forget to mark a correct answer.

Reply
0 Kudos