Multiplication raster by table

4053
17
Jump to solution
10-20-2013 12:32 PM
MarcinNowak
New Contributor
Hello,

I'm looking for the easiest way to multiply raster by the table records. Here's what I mean in details:

I have:
1) raster (DEM) - further called as "input_DEM"
2) specified numeric values stored in a table in 1 column - further called as "value_from_n_row", where 'n' is the row number

I need to create a few new rasters by this way:
"input_DEM" * "value_from_1_row" = "output_raster_1"
"input_DEM" * "value_from_2_row" = "output_raster_2"
"input_DEM" * "value_from_3_row" = "output_raster_3"
etc.

If there's a way to do this automatically? Sorry if my question is trivial, but I didn't found any solution yet.

Thanks for any support! 🙂
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor
Hello,

I'm looking for the easiest way to multiply raster by the table records. Here's what I mean in details:

I have:
1) raster (DEM) - further called as "input_DEM"
2) specified numeric values stored in a table in 1 column - further called as "value_from_n_row", where 'n' is the row number

I need to create a few new rasters by this way:
"input_DEM" * "value_from_1_row" = "output_raster_1"
"input_DEM" * "value_from_2_row" = "output_raster_2"
"input_DEM" * "value_from_3_row" = "output_raster_3"
etc.

If there's a way to do this automatically? Sorry if my question is trivial, but I didn't found any solution yet.

Thanks for any support! 🙂


Hi Marcin,

There is standard tool that does the job. It is however pretty straight forward to do this in Python. See the code snippet below:

import arcpy import os  dem = arcpy.Raster('Input_DEM') tbl = 'YourTableName' fldName = 'YourFieldName' outFolder = r'c:\folder\subfolder'  fields = [fldName] cnt=0 with arcpy.da.SearchCursor(fc, fields) as cursor:     for row in cursor:         value_from_row = row[0]         cnt+=1         outName = outFolder + os.sep + "dem_{0}".format(cnt)         result = dem * value_from_row         result.save(outName)  del row del dem del tbl


In this code the following assumptions are made (basically to keep the code simple):

  • environment settings are specified correctly (workspace, cell size, extent, ...)

  • spatial analyst extension is enabled

  • the table ("YourTableName")  and DEM ("Input_DEM") are in the TOC

  • the values are stored in field "YourFieldName"

Change the "YourTableName" and the name of the DEM "Input_DEM" to the actual name of the objects in the TOC. Also change the "YourFieldName" to the actual name of the field in your table holding the values you want to use. Also specify a valid output folder (or file geodatabase) to store the resulting rasters.

What happens in the code is that for each value in your table a new raster is created by multiplying the DEM with that value. The resulting raster is stored in your output folder with the name "dem_#" (where "#" is a consecutive number).

The code can be executed from the Python window. You can read more about this in the Help topic "What is the Python window?".

The code makes use of arcpy data access module which is available from version 10.1. You can also use a "normal" search cursor to loop through the values in the table. This a also available in version 10.0, but the syntax is different.

Kind regards,

Xander

View solution in original post

0 Kudos
17 Replies
XanderBakker
Esri Esteemed Contributor
Hello,

I'm looking for the easiest way to multiply raster by the table records. Here's what I mean in details:

I have:
1) raster (DEM) - further called as "input_DEM"
2) specified numeric values stored in a table in 1 column - further called as "value_from_n_row", where 'n' is the row number

I need to create a few new rasters by this way:
"input_DEM" * "value_from_1_row" = "output_raster_1"
"input_DEM" * "value_from_2_row" = "output_raster_2"
"input_DEM" * "value_from_3_row" = "output_raster_3"
etc.

If there's a way to do this automatically? Sorry if my question is trivial, but I didn't found any solution yet.

Thanks for any support! 🙂


Hi Marcin,

There is standard tool that does the job. It is however pretty straight forward to do this in Python. See the code snippet below:

import arcpy import os  dem = arcpy.Raster('Input_DEM') tbl = 'YourTableName' fldName = 'YourFieldName' outFolder = r'c:\folder\subfolder'  fields = [fldName] cnt=0 with arcpy.da.SearchCursor(fc, fields) as cursor:     for row in cursor:         value_from_row = row[0]         cnt+=1         outName = outFolder + os.sep + "dem_{0}".format(cnt)         result = dem * value_from_row         result.save(outName)  del row del dem del tbl


In this code the following assumptions are made (basically to keep the code simple):

  • environment settings are specified correctly (workspace, cell size, extent, ...)

  • spatial analyst extension is enabled

  • the table ("YourTableName")  and DEM ("Input_DEM") are in the TOC

  • the values are stored in field "YourFieldName"

Change the "YourTableName" and the name of the DEM "Input_DEM" to the actual name of the objects in the TOC. Also change the "YourFieldName" to the actual name of the field in your table holding the values you want to use. Also specify a valid output folder (or file geodatabase) to store the resulting rasters.

What happens in the code is that for each value in your table a new raster is created by multiplying the DEM with that value. The resulting raster is stored in your output folder with the name "dem_#" (where "#" is a consecutive number).

The code can be executed from the Python window. You can read more about this in the Help topic "What is the Python window?".

The code makes use of arcpy data access module which is available from version 10.1. You can also use a "normal" search cursor to loop through the values in the table. This a also available in version 10.0, but the syntax is different.

Kind regards,

Xander
0 Kudos
MarcinNowak
New Contributor
Hi Xander,

thank you for your answer. Unfortunately, I have a problem with the code. I'm getting the message like this:

Runtime error
Traceback (most recent call last):
    File "<string>", line 1, in <module>
NameError: name 'fc' is not defined


I changed all the names of objects, there are in TOC as well. Sorry if I'm doing something wrong. Could you explain me this problem?

Kind regards and once more again - thank you 🙂
Marcin
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Marcin,

Sorry, my bad. I had an error in the code. The line that states:

with arcpy.da.SearchCursor(fc, fields) as cursor:


... should actually be:

with arcpy.da.SearchCursor(tbl, fields) as cursor:


In many samples "fc" is used as variable to refer to a featureclass and I forgot to change it to "tbl".

Kind regards,

Xander
0 Kudos
MarcinNowak
New Contributor
Now everything works 🙂

Thanks for help

Kind regards,
Marcin
0 Kudos
MatthewJones3
New Contributor
Thanks for posting, Xander. I have similar requirements to Marcin, but I with some complications. Firstly, I want the input raster (DEM in Marcin's example) to be multiplied only by values in which another field (column 2) of the table matches the filename of the input raster. Secondly, I want the output file to assume the name of another field of the table (values in column 1). And thirdly, I have multiple input rasters and would like to automate the process.

To expand on Marcin's helpful input-output map:

"input_ras" * "value_from_rc_[3]" (IF "value_from_rc[2]" = "input_raster_filename") = "[value_from_rc[1]"

where r[] = row & c[] = column/field of the table.

Is there any way to do all of these things in python? I would really appreciate any help!!!
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Matthew,

If I understand this correctly it could look like the code below. I assumed the following things:


  • you have an input folder or fgdb (inWS) where your input rasters are stored

  • fldName1 holds the name of the output raster (no path to folder of fgdb included)

  • fldName2 holds the name of the input raster (stored in inWS)

  • fldName3 is numeric

  • output rasters will be written to the output folder of fgdb outWS

The code will need some small changes in case your fields include locations too.

import arcpy,os

inWS = r'c:\path\to\input\folder\or\filegdb.gdb' # input rasters are stored here
outWS = r'c:\path\to\output\folder\or\filegdb.gdb' # output rasters will be stored here

tbl = 'YourTableName'
fldName1 = 'YourFieldName1' # field with output raster name (no path included)
fldName2 = 'YourFieldName2' # field with input raster name (no path included)
fldName3 = 'YourFieldName3' # multiply value field

fields = [fldName1,fldName2,fldName3]
with arcpy.da.SearchCursor(tbl, fields) as cursor:
    for row in cursor:
        value_from_row = row[2]
        inName = row[1]
        outName = row[0]
        inRasLoc = os.path.join(inWS,inName)
        inRas = arcpy.Raster(inRasLoc)
        result = inRas * value_from_row
        outRasLoc = os.path.join(outFolder,outName)
        result.save(outName)

del row, inRas, tbl


Kind regards,

Xander
0 Kudos
MatthewJones3
New Contributor
Thanks so much for responding so quickly, Xander. Extremely helpful! And yes, the assumption that you listed are correct - sorry if my question could have been more clear. Unfortunately I am getting the following error message:

RuntimeError: ERROR 010240: Could not save raster dataset to C:\""\""\'SCRATCHWORKSPACE'.gdb\"OutName" with output format FGDBR

Are you aware of any work-around?

Thanks again!
Matt
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Matthew,

Can you post your code using the "#" button? It is probably the way the workspace is defined.

Kind regards, Xander
0 Kudos
MatthewJones3
New Contributor
Hi Xander,

Of course - please see below. I have emboldened the changes that I made to the original code that you posted.

>>> import arcpy,os
... arcpy.env.workspace = "C:\A\B\Test\TIMESLOOKUP.gdb"
... arcpy.env.scratchWorkspace = "C:\A\B\Test\TIMESLOOKUP.gdb"

... inWS = r'C:\A\B\C\EucDistRasters' # input rasters are stored here
... outWS = r'C:\A\B\Test\' # output rasters will be stored here
... 
... tbl = 'LOOKUP$'
... fldName1 = 'A' # field with output raster name (no path included)
... fldName2 = 'B' # field with input raster name (no path included)
... fldName3 = 'C' # multiply value field
... 
... fields = [fldName1,fldName2,fldName3]
... with arcpy.da.SearchCursor(tbl, fields) as cursor:
...     for row in cursor:
...         value_from_row = row[2]
...         inName = row[1]
...         outName = row[0]
...         inRasLoc = os.path.join(inWS,inName)
...         inRas = arcpy.Raster(inRasLoc)
...         result = inRas * value_from_row
...         outRasLoc = os.path.join(outWS,outName)
...         result.save(outName)
... 
... del row, inRas, tbl


Best wishes,
Matt
0 Kudos