Solved! Go to Solution.
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! 🙂
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
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! 🙂
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
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'fc' is not defined
with arcpy.da.SearchCursor(fc, fields) as cursor:
with arcpy.da.SearchCursor(tbl, fields) as cursor:
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
>>> 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