Select to view content in your preferred language

Convert raster to txt in XYZ format

3046
2
08-08-2013 01:29 PM
GualbertoHernandez
Emerging Contributor
Hello friends,

I need to convert a raster to a txt file but XYZ format.
*
ASCII TO RASTER tool does not help because it does in another format.

Know if there are processes supported by python can do?

Thank you very much for your help.

Gualberto
Tags (2)
0 Kudos
2 Replies
StacyRendall1
Frequent Contributor
You can determine the x,y,z dimensions using Raster, as described here.

You can iterate over the dimensions and get the cell values, as described here.

You can write to a file like this:
f = 'D:\\temp\\test.txt'
with open(f, 'w') as fObj:
  fObj.write('x, y, z\n')
  for i in range(10):
    fObj.write('%f, %f, %f\n % (i, i, i))


So, your overall code might be something like (have not tested):
outTxt = 'D:\\temp\\test.txt'
inRasFP = 'D:\\temp\\raster.img'
inRas = arcpy.Raster(inRasFP)
extent = inRas.extent
with open(outTxt, 'w') as fObj:
  fObj.write('x, y, z\n') # only if you want a header in the text file
  for x in range(extent.XMin, extent.XMax):
    for y in range(extent.YMin, extent.YMax):
      fObj.write('%i, %i, %f\n % (x, y, arcpy.GetCellValue_management(inRasFP, "%i %i" % (x, y))))
0 Kudos
Luke_Pinner
MVP Regular Contributor
Use the Sample tool.  See also this answer.
0 Kudos