Raster Calculator in Arcmap Desktop Python Script

1646
11
12-17-2019 10:59 PM
MudassarAli2
New Contributor

Dear All

I have a model builder in ARCMap desktop 10.5 which is executing fine. I have converted it to python script to make a batch script. But at the last step, where I am using raster calculator in model builder, getting an error. The line is

arcpy.gp.RasterCalculator_sa(constant_raster * float(value), rastercalc)

(where value is a variable and constant_raster is a raster dataset)

From the link Raster Calculator—Help | ArcGIS for Desktop  I came to know that RasterCalculator only function in model builder.

Is it any other wayaround that I could implement the raster calculator in my script?

Your response will be highly appreciated.

Thanks

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

I am sure ArcMap had arithmetic operators equivalent to 'multiply'

*—Spatial Analyst module | ArcGIS Desktop 

check your version of the help for Spatial Analyst and 'Operators' for basic math functionality

0 Kudos
MudassarAli2
New Contributor

Thanks for the reply. 

I have read the article but can not found solution. I have a raster dataset which I want to multiply by a constant value, how could i achieve this?

0 Kudos
DanPatterson_Retired
MVP Emeritus

part way down the help link...

Assume your raster is called "elevation" and you want to multiply it by a number (say 0.3048).  You do the multiplication, then save it and give it a name.  If you want to save it to a *.tif, you would add that extension to the output raster name

outTimes = Raster("elevation") * 0.3048

outTimes.save("C:/sapyexamples/output/outtimes")

0 Kudos
MudassarAli2
New Contributor

I have executed the following 

rastercalc=arcpy.sa.Raster(constant_raster_)*0.3048

arcpy.gp.ExtractByMask_sa(rastercalc,water_body_feature_class,WaterMask)

WaterMask.save("H:\\output.tif")

But its giving me an error "AttributeError: 'str' object has no attribute 'save' "

Whats the issue is this?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Extract by Mask—Help | ArcGIS Desktop 

no clue what water_body_feature_class is

no clue what WaterMask is

and why you have 3 parameters and there are only 2 listed in the help for ArcGIS Pro.

Check the code samples towards the end of the help topic.

also, saving to the root of a folder isn't a wise idea, you are generally better off saving to a folder therein

0 Kudos
MudassarAli2
New Contributor

I have successfully created the following code which is running fine on my system (Arcmap 10.5)

https://onlinegdb.com/SkvkqiWeU

This code is running fine on my system but giving following error on the other system,

Executing: script "G:\image\s.img" "G:\river_splitted" "G:\output"
Start Time: Tue Jan 07 09:40:43 2020
Running script script...
Failed script script...

Traceback (most recent call last):
File "E:\script.py", line 53, in <module>
arcpy.gp.ExtractMultiValuesToPoints_sa(point_feature, DEM + " elevation", "NONE")
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

Failed to execute (script).
Failed at Tue Jan 07 09:40:45 2020 (Elapsed Time: 2.11 seconds)

0 Kudos
DanPatterson_Retired
MVP Emeritus

has the original question been answered? 

Questions have been posed to you for follow up and you haven't responded and nothing seems to have helped.

If you script now works on your system and it doesn't on a new machine, then the issue lies in the differences.

You might begin by why you are writing to the root director of a drive... or worse, a thumb drive.

Perhaps someone else will pick up this thread and provide further assistance.

0 Kudos
MudassarAli2
New Contributor

This is the complete script which I have written whose one part I had questioned above. Secondly I am not writing to the root folder, I have just deleted the complete path from the error log. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

That examine the simplified path, that may be where the problem lies

0 Kudos