Join .txt to raster using python?

1034
1
10-25-2012 04:28 AM
SpencerMeyer
New Contributor
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")
Tags (2)
0 Kudos
1 Reply
SpencerMeyer
New Contributor
I think I've answered part of my problem. I'm now using MakeTableView directly off the .txt:

# Prepare TableView for join
# Get the fields from the input
fields= ListFields(inTable)

# Create a fieldinfo object
fieldinfo = arcpy.FieldInfo()

# Iterate through the fields and set them to fieldinfo
for field in fields:
    if field.name == "P(+FtySuit)":
        fieldinfo.addField(field.name, "SuitYES", "VISIBLE", "")
    elif field.name == "P(-FtySuit)":
        fieldinfo.addField(field.name, "SuitNO", "VISIBLE", "")
# Create table view from Netica output
arcpy.MakeTableView_management(inTable, joinTable, "", "", fieldinfo)

# 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)


However, two of the fields I care about are getting imported as string instead of double. The first row happens to have the value 1 and 0 in each of the two relevant fields. I tried deleting that row from the file and now the first row contains 0.875 and 0.125. Now the fields properly get converted as doubles. So, what part of my process is deciding on data type based on the first row? Is there a way around this?

thanks!
0 Kudos