2

I am working on a script tool that will be run from a custom toolbox in ArcCatalog.

I have written everything with the inputs hardcoded into it for testing. Works great running from IDLE.

I had hoped that all I needed to do in the end was to remove the hardcoded paths and replace them for the input tool parameters:

workspace = arcpy.GetParameterAsText(0) ... save_flag = arcpy.GetParameterAsText(10)

But when running from the script tool GUI, I'm finding several places that it fails.

I have several places where I use raster math in place raster calculator as shown below , but it errors as "not licenced" (I am sure have SA activated earlier in the script) or as "invalid parameters".

First attempt (works in IDLE, fails in script tool)

eopslopes = arcpy.Raster(masked_dem3) - (float(SinkVal) / (arcpy.Raster(euc_eop) + 1))

and shown below in context

arcpy.FeatureToRaster_conversion(eop, "OBJECTID", r"in_memory\eop_ras", dem)
inSourceData = r"in_memory\eop_ras"
slopeDist = 15
SinkVal = 1
buffdist = 10
cellSize = dem
arcpy.gp.EucDistance_sa(inSourceData, r"in_memory\eucdist_eop", "", cellSize, "", "PLANAR")
euc_eop = r"in_memory\eucdist_eop"
eopslopes = arcpy.Raster(masked_dem3) - (float(SinkVal) / (arcpy.Raster(euc_eop) + 1))
eopslopes.save(r"in_memory\eopslopes")

the above yeilds this error when run in teh script tool GUI from Caralog

PYTHON ERRORS:
Traceback info:
    File "script.py", line 428, in <module>
    eopslopes = arcpy.Raster(masked_dem3) - (float(SinkVal) / (arcpy.Raster(euc_eop) + 1))

Error Info:
ERROR 000824: The tool is not licensed.

Other attempts attempt (fails):

I've tried several things, that all fail. here's an example of one of those failed changes I tried to save the input rasters as raster layers :

    arcpy.MakeRasterLayer_management(masked_dem3, "masked_dem3_lyr", where_clause="", envelope="", band_index="")
    arcpy.MakeRasterLayer_management(euc_eop, "euc_eop_lyr", where_clause="", envelope="", band_index="")
    expression = r"arcpy.Raster(masked_dem3_lyr) - (float(SinkVal) / (arcpy.Raster(euc_eop_lyr + 1))"
    arcpy.gp.RasterCalculator_sa(expression, "eopslopes")

What is the correct formatting for arcpy.gp.RasterCalculator_sa(expression, "outras")?

.

.

.

In response to Jae's answer which I guess directly addressed my final bolded question above:

OK so arcpy.gp.RasterCalculator_sa() is out...but more to the spirit of my question, what then should one use in this case? my first attempt above works in IDLE, what formatting needs to be done to have it work in a script tool?

  • 1
    Does your code check in and check out the spatial analyst extension? If not it should! See help on how to do that. – Hornbydd Dec 27 '19 at 0:33
  • I do have SA checked in for earlier steps not shown here that do work, which is why i thought it so odd to get the "not licensed" error. – Zipper1365 Dec 27 '19 at 4:09
4

From Raster Calculator help...

The Raster Calculator tool is intended for use in the application only as a GP tool dialog box or in ModelBuilder. It is not intended for use in scripting and is not available in the ArcPy Spatial Analyst module.

So, using the Math toolset, break your expression down in to its component parts, then save the result.

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

theDenominator = Plus(euc_eop, 1)

theDivide = Divide(float(SinkVal), theDenominator)

theResult = Minus(masked_dem3, theDivide)

theResult.save(......)

arcpy.CheckInExtension("Spatial")
|improve this answer|||||
  • 1
    The raster calculator expression you were intending on computing is a series of nested arithmetic operations. Just perform each operation sequentially, and save the final output raster. The result of each section of your expression will be a raster object, that can be used for the next part of your expression chain. – Jae Dec 27 '19 at 2:32
  • And as mentioned in an earlier comment make sure you import sa (spatial analyst) when you start your script, and use arcpy.CheckOutExtension(“Spatial”) to access the required license. – Jae Dec 27 '19 at 2:42
  • Thus is great guidance. I got called out of the office until next thursday so i won't be able to test till then but this seems really promising – Zipper1365 Dec 27 '19 at 4:12
  • Why bother with Minus, Plus etc if you can simply use + and - and make it a single line of code? – FelixIP Dec 27 '19 at 5:16
  • @FelixIP, it's just different coding styles, I kind of like the suggested approach as it is explicitly using raster tools, I think what you suggest could easily be misinterpreted as just some equation if say you were skipping through the code? – Hornbydd Dec 27 '19 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.