Here's some Python code that will take an ascii file (output of the ArcGIS RastertoAscii tool, for which no SA license is needed), and reformat it to a .csv file with x, y, z values (x and y are cell centers). Note that depending on the particulars of your DEM, the .csv file will generally be much larger than the input ascii file. This script could easily be altered so as to write the xyz stuff to a binary format like a .dbf, .shp, FGDB, etc.# Description: Format an ascii text file (created using the RasterToAscii in ArcGIS) as a comma delimited .csv file containing x, y, z values
# Written By: Chris Snyder, 20101012
import os
inputAsciiDemFile = r"D:\csny490\dems\dem90m\dem90.txt" #ascii file created using the RasterToAscii in ArcGIS
outputCsvFile = r"D:\csny490\temp\test.csv" #output comma delimited file ascii file (x,y,z fields)
if os.path.exists(outputCsvFile):
os.remove(outputCsvFile)
inputLines = open(inputAsciiDemFile, 'r')
nColumns = int(inputLines.next().split(" ")[-1])
nRows = int(inputLines.next().split(" ")[-1])
xCorner = float(inputLines.next().split(" ")[-1])
yCorner = float(inputLines.next().split(" ")[-1])
cellSize = float(inputLines.next().split(" ")[-1])
nodataValue = int(inputLines.next().split(" ")[-1])
print >> open(outputCsvFile, 'a'), "X,Y,Z"
outputLines = open(outputCsvFile, 'a')
lineCounter = 0
for inputLine in inputLines:
if divmod(lineCounter,100)[1] == 0:
print "Processing row " + str(lineCounter) + " of " + str(nRows) + "..."
lineCounter = lineCounter + 1
valueList = [float(value) for value in inputLine.split(" ") if value != '\n']
columnCounter = 0
for rasterValue in valueList:
columnCounter = columnCounter + 1
if rasterValue != nodataValue:
xCoord = xCorner + (columnCounter - 1) * cellSize + cellSize / 2
yCoord = yCorner + (nRows - lineCounter) * cellSize + cellSize / 2
outputLines.write(str(xCoord) + "," + str(yCoord) + "," + str(rasterValue) + "\n")
outputLines.close()
inputLines.close()