I have downloaded US soil raster data by state from https://gdg.sc.egov.usda.gov/GDGOrder.aspx at 10m resolution.

I am now trying to add a modified raster attribute table to tif that was exported from the geodatabase using arcpy, but have been unable to do so. The existing tif has an attribute table with an ID that matches the IDs in the modified attribute table.

I have tried to (1) make the raster a layer (not sure this is the right approach), (2) add a table using AddJoin_management, and (3) export the results. Obviously, I am missing something.

import arcpy
from arcpy import env

env.workspace = "C:/Data"
inFeatures = "InRaster.tif" ## This is tif raster exported from geodatabase
layerName = "raster_layer"
rat = "rat.dbf" ## attribute table for joining
arcpy.MakeRaster_management(inFeatures,layerName) ## This doesn't work, with or without extension.

tmp = arcpy.AddJoin_management(layerName,"Value",rat,"ID") # This works, but is a result and I am not sure how to export it.

There is a much simpler solution to writing code and that is to use the Join Field tool. This is like @FelixIP approach in that it adds a field to the input table and passes over the values. So no need for an export step. This tool can pass over multiple fields and works at all license levels.

  • For downstream analyses, I do need to export the values from the attribute table as a raster – user44796 Apr 15 '16 at 15:38

It seems that you'd like to create new raster from yours with a column added to it's table.

I also don't know how to export raster layer to a new raster. This is why I suggest to create new field in original raster and populate it by matching values from table:

import arcpy
from arcpy import env
env.workspace = "D:/scratch"
inFeatures = "InRaster.tif"
layerName = "raster_layer"
rat = "mouse.dbf"
# create dictionary from table
with arcpy.da.SearchCursor(rat,("ID","random")) as cursor:
    for row in cursor:aDict[row[0]]=row[1]
# create field in raster table
arcpy.MakeRasterLayer_management(inFeatures, layerName)
try:arcpy.AddField_management(layerName, "fldFloat","FLOAT")

# and transfer matching records from rat table
with arcpy.da.UpdateCursor(layerName,("Value","fldFloat")) as cursor:
    for row in cursor:
        if aKey in aDict:
            print 'Transferring value %s for key %s' %(aDict[aKey],aKey)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.