Skip to content

Instantly share code, notes, and snippets.

@PierrickKoch
Last active December 17, 2015 22:59
Show Gist options
  • Select an option

  • Save PierrickKoch/5686262 to your computer and use it in GitHub Desktop.

Select an option

Save PierrickKoch/5686262 to your computer and use it in GitHub Desktop.
def save_geotiff(filepath, image32, width, height):
import numpy
import gdal
driver = gdal.GetDriverByName('GTiff')
ds = driver.Create(filepath, width, height, 1, gdal.GDT_Float32)
tmp = [image32[row*width:(row+1)*width] for row in range(height)]
raster = numpy.asarray(tmp, dtype=numpy.float32)
#scipy.misc.imsave('%s.png'%filepath, raster)
ds.GetRasterBand(1).WriteArray( raster )
def read_dtm_ascii(filepath):
with open(filepath, 'r') as f:
buff = f.read().split()
# see libdtm/src/io.c:45 in dtm_writeAsciiDtm()
width = int(buff[16])
height = int(buff[15])
data = buff[24:]
image = [float(data[7+i*10]) for i in range(len(data)//10)]
assert(len(image) == width * height)
return image,width,height
def main(args):
if len(args) < 3:
print("usage: dtm2geotiff file.dtm file.tif")
img32,w,h = read_dtm_ascii(args[1])
save_geotiff(args[2], img32, w, h)
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment