Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am desperately trying to export the values in a raster table to a .txt file. I've seen some other folk post about this, but for some reason can't get the code to work in my case.

I have a raster, with table already built, called "shed". I simply want to export two fields, "Value" and "Count", to a .txt or .csv file (called "try" in this case). So far, I haven't even been able to get a script that even looks at the raster's table. So far I have:

outfile=open("E:/Weathering_GIS/try.txt",'w')
rows=arcpy.SearchCursor('shed',"","","Value;Count","")
for row in rows:
    val=row.getValue('Value')
    count=row.getValue('Count') 
    outfile.write(val)

But "val" and "count" always fail to become anything- they just remain empty values! Am I missing something? Maybe some sort of import I have overlooked?

share|improve this question
    
Hi Ryan - First, does an attribute table definitely exist? Secondly, does it have to be in arcpy? You can just try a simple export from the attribute table if you urgently need the data. Thirdly, are you reading in the raster properly? You can't read in the file using the name unless you set the working directly properly using something like env.workspace = r"D:\DATA\\", after the line from arcpy import env –  djq Feb 14 '12 at 2:15
add comment

2 Answers 2

up vote 5 down vote accepted

I think you were missing closing your output text file - until you do that, you won't get anything written to the text file. Below code tested and works:

import arcpy

arcpy.env.workspace = r"C:\Users\chad\Documents\ArcGIS\Default.gdb"

arcpy.BuildRasterAttributeTable_management("raster17", "Overwrite")
outfile = open(r"D:\temp\raster17.txt", "w")

rows = arcpy.SearchCursor("raster17","","","Value;Count","")
for row in rows:
    val = row.getValue("Value")
    count = row.getValue("Count")
    print val, count
    outfile.write(str(val) + "," + str(count) + "\n")

outfile.close()

And it yields this text file:

enter image description here

share|improve this answer
    
THANK YOU SO MUCH!!! –  Ryan Utz Feb 14 '12 at 16:16
add comment

I don't think you need arcpy for this. It's be much easier to just export the attribute table to text as celenius suggested.. You can export a raster attribute table in ArcMap or ArcCatalog. You need to be viewing the raster attribute table to do this though. From the ArcGIS help page:

  1. Click the Options button and click Export.
  2. Click the Export drop-down arrow on the Export Data dialog box to choose to export All records or Selected records. The Selected records option is only available if records are selected in the table.
  3. Click the browse button and navigate to the folder or geodatabase in which you want to place the exported data.
  4. Click the Save as type drop-down arrow and click the format to which you want to export the data.
  5. Type a name for the exported table.
  6. Click Save.
  7. Click OK.

This assumes that you already have a raster attribute table. If you don't have it yet, you can build it using the Build Raster Attribute tool.

share|improve this answer
    
Celenius- thanks; I'll look into those tips. I did have the workspace set, though... R. K.-That'd be fine if I didn't have to do hundreds of these. I'm trying to get something that I can use for batch-processing. –  Ryan Utz Feb 14 '12 at 15:33
    
Your original question didn't really say anything about batch-processing. Anyways, good to know that you found your solution. –  R.K. Feb 15 '12 at 1:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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