Copy several lines to the batch window of raster calculator

1400
5
Jump to solution
03-26-2012 03:50 AM
RasmusErlandsson
New Contributor II
Due to poor python skills i don't have the possibility to write scripts to automate my raster operations. As a solution to my problems I thought the batch mode of raster calculator would do the trick. I use Microsoft Excel to generate commands and pathways for the output files, resulting in long lists. But much to my dismay I can't find any way to enter them all at the same time in the batch window. I have been forced to copy every single line by it self, and paste in to each corresponding batch row.

I guess there must be a way to paste them in a more efficient way, but I can't find out how. If somebody could suggest me a solution I would be forever grateful!
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
Due to poor python skills i don't have the possibility to write scripts to automate my raster operations. As a solution to my problems I thought the batch mode of raster calculator would do the trick. I use Microsoft Excel to generate commands and pathways for the output files, resulting in long lists. But much to my dismay I can't find any way to enter them all at the same time in the batch window. I have been forced to copy every single line by it self, and paste in to each corresponding batch row.

I guess there must be a way to paste them in a more efficient way, but I can't find out how. If somebody could suggest me a solution I would be forever grateful!


In Arc 10.x, you should be able to paste your raster calculator expressions into the python command window, as long as you enter this line first:

from arcpy.sa import *

View solution in original post

0 Kudos
5 Replies
curtvprice
MVP Esteemed Contributor
Due to poor python skills i don't have the possibility to write scripts to automate my raster operations. As a solution to my problems I thought the batch mode of raster calculator would do the trick. I use Microsoft Excel to generate commands and pathways for the output files, resulting in long lists. But much to my dismay I can't find any way to enter them all at the same time in the batch window. I have been forced to copy every single line by it self, and paste in to each corresponding batch row.

I guess there must be a way to paste them in a more efficient way, but I can't find out how. If somebody could suggest me a solution I would be forever grateful!


In Arc 10.x, you should be able to paste your raster calculator expressions into the python command window, as long as you enter this line first:

from arcpy.sa import *
0 Kudos
RasmusErlandsson
New Contributor II
In Arc 10.x, you should be able to paste your raster calculator expressions into the python command window, as long as you enter this line first:

from arcpy.sa import *


That's terrific! Thank you alot! Do you know how to specify output location/filename in this way? Right now it exports to a standard folder with some generic name.
0 Kudos
TarunJaiswal
Esri Contributor
That's terrific! Thank you alot! Do you know how to specify output location/filename in this way? Right now it exports to a standard folder with some generic name.


Please take a look at the following help document:

A quick tour of using Map Algebra

Hope this helps.
0 Kudos
AhmedEl-Gabbas
New Contributor
Hello rasmerla, curtvprice & Tarun_Jaiswal

I almost have the same problem and I have posted it here. I have used concatenate function in excel to prepare the list of the calculator expressions [not sure if it is in the true format] and their output directories and file names as below.
"J:\sp1\sp1_Thresholded_0_3.img" + "J:\sp1\sp1_A2a_2020_Thresholded.img" ==> "J:\sp1\sp1_CCCMA_A2a_2020_GainLoss.img"

I tried to use ModelBuilder with raster calculator in the same way I use with other tools but it was unsuccessful. I also tried to used iterators but it was not successful with me as well (all operations were overwritten on just one file).

I do not have any background in Python but it does not seem too hard for me to do that task. Could you please help me with coding just one example mentioned above (using Python):

Thanks a lot,
Ahmed
0 Kudos
curtvprice
MVP Esteemed Contributor
I have used concatenate function in excel to prepare the list of the calculator expressions [not sure if it is in the true format] and their output directories and file names as below.
"J:\sp1\sp1_Thresholded_0_3.img" + "J:\sp1\sp1_A2a_2020_Thresholded.img" ==> "J:\sp1\sp1_CCCMA_A2a_2020_GainLoss.img"


As described in the help for the Raster Calculator tool, these are python expressions, so you need to be careful to escape your strings. The correct format for your expression in the Python window would be:

from arcpy.sa import *
rst = r"J:\sp1\sp1_Thresholded_0_3.img" + r"J:\sp1\sp1_A2a_2020_Thresholded.img";rst.save(r"J:\sp1\sp1_CCCMA_A2a_2020_GainLoss.img")


(Using the ";" to join multiple lines in python is generally frowned upon [it kind of takes away Python's elegance can introduce bugs] but in your case of creating command lines to paste in the python window it is a workable trick.)

Note the use of "r" for raw strings. This prevents recognized escape expressions from being used, for example, "\t" is recognized by Python as a tab character (without the prefix "r").

I still think [post=191665]using the ModelBuilder Iterate Rasters,[/post] with the Calculate Value tool used to set up your pathnames, is far preferable to generating arcpy code in Excel.
0 Kudos