Reply To: DEM Help

Home Forums AR Sandbox Forum DEM Help Reply To: DEM Help

#102889
siki
Participant

The “Short read by” error was my mistake (using 2 byte integers instead of 4 bytes). I’ve just finished my small utility which convert dem file to grid file. The code is beta, no extensive tests were made. It is written in Python (tested in Python 2.7 but is should run in python 3 too).
Here it is:

“””
Convert USGS ASCII DEM file to SARnbox grid format
(c) Zoltan Siki siki.zoltan@epito.bme.hu

Usage:
python dem2grid.py input.dem [output.grid]
or
dem2grid.py input.dem [output.dgrid]
“””

import sys
import os
import osgeo.gdal
from gdalconst import *
import struct

# check command line parameters
if len(sys.argv) <= 1:
print(“Usage: dem2grid.py input.dem output.grid”)
sys.exit(1)
ifilename = sys.argv[1]
# generate output file name if not given on the command line
if len(sys.argv) > 2:
ofilename = sys.argv[2]
else:
ofilename = os.path.splitext(ifilename)[0] + “.grid”
# use gdal to read USGS DEM file
idataset = osgeo.gdal.Open(ifilename, GA_ReadOnly)
if idataset is None:
print(“Cant read input file”);
sys.exit(2)
# get size of dem
cols = idataset.RasterXSize
rows = idataset.RasterYSize
# get and calculate coordinate limits
tr = idataset.GetGeoTransform()
xul = tr[0]
yul = tr[3]
xlr = xul + (cols – 1) * tr[1]
ylr = yul + (rows – 1) * tr[5]
# write data to binary output
of = open(ofilename, “wb”)
of.write(struct.pack(“2i”, cols, rows))
of.write(struct.pack(“4f”, xul, ylr, xlr, yul))
band = idataset.GetRasterBand(1)
of.write(band.ReadRaster(0, 0, cols, rows, cols, rows, band.DataType))
of.close()

Comments are closed.