spencermeyer

Join .txt to raster using python?

Discussion created by spencermeyer on Oct 25, 2012
Latest reply on Oct 25, 2012 by spencermeyer
Hello,

I am trying to join the results of an external model (.txt) to a raster produced from another process and then export that raster to a new file. I've been trying to use TabletoDBase or TabletoTable to convert the .txt to a .dbf to do the join. The former works, but my primary problem is that even though one of my fields in the .txt is a double, it gets converted to an integer in the .dbf. Worse, is that this only happens sometimes. The first record happens to have a 1 in that field, so I'm wondering if the TabletoDBF tries to guess the datatype from the first record. By the way, I'm on ArcGIS 10.1 and Python 2.7.

So, my questions is:
1. What is the best overall flow for joining a .txt to a raster and saving it to a new raster?
2. I am also having overwrite issues. What is the best approach for checking if a file already exists and then deleting it before running the code?
3. Any other tips would be much appreciated. You'll see from my code that I'm a rookie. Please go easy on me!

Thanks for your help.

# Set environment settings
root = Tk()
root.withdraw
env.workspace = askdirectory(parent=root, initialdir="C:\\GIS\\AltFutures_GIS\\" + studyarea + "\\" + whichmodel, title='Please select a Model Run')
env.qualifiedFieldNames = False
gdbDir = "C:\\GIS\\AltFutures_GIS\\MasterStatewideDB\\"  + LU_GDB + ".gdb\\"
FORMAT = '%Y%m%d'

## 1. Convert Netica output .txt file to .dbf
# Set local variables
inTable = askopenfilename(parent=root, initialdir=env.workspace, title='Please select a Netica output file') # show an "Open" dialog box and return the path to the selected file
outDBF = inTable[:-4] + ".dbf"
outLocation = env.workspace

try:
    # Execute TableToDBASE
    arcpy.TableToDBASE_conversion(inTable, outLocation)
    #arcpy.TableToTable_conversion(inTable, outLocation, outDBF)
except:
    print arcpy.GetMessages()

## 2. Join composite raster from GDB_to_Netica with .dbf from step 1 above and export it to new raster.
# Set local variables
inRaster = askopenfilename(parent=root, initialdir=env.workspace, title='Please select the combined raster file') # show an "Open" dialog box and return the path to the selected file
layerName = "RstTMP"
joinTable = outDBF
joinField1 = "Value"
joinField2 = "IDNum"
outRaster = whichmodel + "_SUIT_" + env.workspace[-8:] + ".tif"
outRaster2 = gdbDir + outRaster
# Create a feature layer from the vegtype featureclass and add index to joinField1
arcpy.MakeRasterLayer_management (inRaster,  layerName)
arcpy.AddIndex_management (layerName, "Value", "Index", "NON_UNIQUE", "NON_ASCENDING")

# Join the feature layer to a table
arcpy.AddJoin_management(layerName, joinField1, joinTable, joinField2, "KEEP_COMMON")

# Copy the layer to a new permanent feature class
arcpy.CopyRaster_management(layerName, outRaster)

# Copy this suitability raster to the appropriate GDB -------DOESN'T WORK 10.24.12
#arcpy.Copy_management(env.workspace + "/" + outRaster, outRaster2, "RasterDataset")

Outcomes