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 numberI 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
import arcpy,os,arcpy.sa
import arcpy,os from arcpy.sa import *
result = arcpy.sa.Exp(inRas * value_from_row)
>>> import arcpy,os,arcpy.sa ... 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 = Exp(inRas * value_from_row) ... outRasLoc = os.path.join(outWS,outName) ... result.save(outName) ... ... del row, inRas, tbl
fldName1 = 'A' fldName2 = 'B' fldName3 = 'C'
fldName1 = 'Name' fldName2 = 'AP' fldName3 = 'Q'
import arcpy,os 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 = 'Name' # field with output raster name (no path included) fldName2 = 'AP' # field with input raster name (no path included) fldName3 = 'Q' # multiply value field fields = [fldName1,fldName2,fldName3] cnt=0 with arcpy.da.SearchCursor(tbl, fields) as cursor: for row in cursor: cnt+=1 value_from_row = row[2] inName = row[1] outName = row[0] inRasLoc = os.path.join(inWS,inName) outRasLoc = os.path.join(outWS,outName) print "Name={0}\tAP={1}\tQ={2}\t".format(outName,inName,value_from_row) print " - inRasLoc ={0}".format(inRasLoc) print " - outRasLoc={0}".format(outRasLoc) if cnt > 5: break del row, tbl
outWS = r'C:\A\B\Test\'
outWS = r'C:\A\B\Test'
# use "r" before path myFGDB = r"C:\A\B\Test\TIMESLOOKUP.gdb" # use forward slashes myFGDB = "C:/A/B/Test/TIMESLOOKUP.gdb" # use double slashes myFGDB = "C:\\A\\B\\Test\\TIMESLOOKUP.gdb"
arcpy.env.workspace = r"C:\A\B\Test\TIMESLOOKUP.gdb" arcpy.env.scratchWorkspace = r"C:\A\B\Test\TIMESLOOKUP.gdb"
>>> 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
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
with arcpy.da.SearchCursor(fc, fields) as cursor:
with arcpy.da.SearchCursor(tbl, fields) as cursor:
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'fc' is not defined
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.