Reply To: DEM Help

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

#103008
jKrienert
Participant

@ABCottrell,
Here is an example of the adapted dem2grid.py script I used which functions when run from a PyCharm Python Console…

import sys
import os
import osgeo.gdal as gd
from gdalconst import GA_ReadOnly, GDT_Byte, GDT_UInt16, GDT_Int16, \
     GDT_UInt32, GDT_Int32, GDT_Float32, GDT_Float64
import struct

# GDAL data types to packt data_types
gd_type = {GDT_Byte:    "b",
           GDT_UInt16:  "H",
           GDT_Int16:   "h",
           GDT_UInt32:  "I",
           GDT_Int32:   "i",
           GDT_Float32: "f",
           GDT_Float64: "d"}
# establish input file
ifilename = r"C:\Users\Joe\map.asc"
# generate output file name 
ofilename = os.path.splitext(ifilename)[0] + ".grid"
# use gdal to read DEM file
idataset = gd.Open(ifilename, GA_ReadOnly)
if idataset is None:
    print("Cannot read input file {}".format(ifilename));
    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)
d = band.ReadRaster(0, 0, cols, rows, cols, rows, band.DataType)
data = struct.unpack(gd_type[band.DataType] * (rows * cols), d)
of.write(struct.pack("f" * (cols * rows), *data))
of.close()
  • This reply was modified 3 years, 7 months ago by jKrienert.
  • This reply was modified 3 years, 7 months ago by jKrienert. Reason: clarification and confidentiality

Comments are closed.