quick script to read digital terrain elevation data?
I'm running python 2.3 on Windows XP. Anyone have a quick small script
to convert .DT1 and .DEM data to ASCII or some other format? I don't
need a viewer.
Re: quick script to read digital terrain elevation data?
I have some in-house python for dem2xyz but it isn't pretty. I would
suggest starting with ftp.blm.gov/pub/gis/dem2xyz6.zip ; it is a C
program with source for just such conversions (my initial dem2xyz.py is
a direct python rewrite of the C code). I think it's actually easier
to read in C (when reading C I expect C conventions, when reading
python I expect Python, this is C written in python and it gives off an
ugly vibe)
Sorry so long, the task is more complicated than a quick small script.
Anyway, for what it is worth:
------------------------------------------------
#Python rewrite of 'DEM2XYZ.C' which isn't a friendly compile
# slower than C, sure. But it should work on Win and UNIX without any
hassles.
#if this reads like a C program you know why. Sorry ;)
####
## from 'DEM2XYZ6.ZIP pulled from ftp.blm.gov
#
# Origional Header comments:
# convert dem to x, y, z format, one dem profile at a time,
# converts 3 arc second to lat/long, sol katz, mar. 94,
# added sampling and cutting to size, sol katz, apr 94
# changed the calculation of start position in sampling code,
# sol katz, jan 95
# ver 4, got rid of last 160 bytes on header line. sol katz, mar
97
for r in range(self.firs tRow, self.lastRow, self.rowInt):
#scale the raw value
tempFloat = float(self.base[r]) * self.verticalSc ale
oh.write("%f %f %i\n" % (XCoord, YCoord, int(tempFloat)) )
self.cellCount += 1
#move up the delta y distance
YCoord += self.deltaY
return None
#pull off 15, 24 byte sections and store in projectParams list
for k in range(5):
for l in range(3):
value = fh.read(24)
#we need to handle the exponent.. a simple float()
ain't doing the trick.
self.projectPar ams.append(floa t(value.replace ("D",
"E")))
print "proj: %15.7f %15.7f %15.7f" % (self.projectPa rams[-3],
self.projectPar ams[-2], self.projectPar ams[-1])
self.planeUnitO fMeasure = int(fh.read(6))
print "plane Unit Of Measure: %i" % self.planeUnitO fMeasure
#'kludge to force the end of processing'???
#if (profileID["current"] - 1) != lastProfile:
# print "%s - 1 != %s" % (profileID["current"],
lastProfile)
# print "%i lines were written to the file" %
self.cellCount
# return None
#skip ahead to the first row
self.base = []
for r in range(self.firs tRow):
self.base.appen d(0)
for r in range(self.firs tRow, self.lastRow):
value = fh.read(6)
print self.firstRow, r, self.lastRow, repr(value)
try:
self.base.appen d(int(value))
except:
raise "the horrors of war!"
#self.base.appe nd(int(fh.read( 3)))
#if cutting out a section, adjust the rows
if outType == 2:
#subset
self.firstRow = rowStr
planCoords[1] = planCoords[1] + (self.firstRow - 1) *
self.deltaY
lastRow = max(lastRow, rowEnd)
mod = c % colInt
if mod == 0 and c >= self.colStr:
if outType == 2:
writeSubset(oh, planCoords[0], planCoords[1])
else:
D.writeNormal(o h, planCoords[0], planCoords[1])
#trailer bytes?
#fh.read(424)
return None
YMAX = 2048
XMAX = 2048
SW = 0
NW = 1
NE = 2
SE = 3
print "DEM to x,y,z ascii file, Python variation based on C version 6"
print " Python version by Jason Kane, BroadLink Communications 2004"
print " C version by Sol Katz, BLM April 1997"
print "number of rows %i, number of columns %i" % (D.rowCount,
D.columnCount)
colInt = 1
D.rowInt = 1
outType = input("Enter 0 for all, 1 for samples, 2 for subset : ")
Comment