I'm trying to convert a .tiff DEM to a numpy array using python. Ideally the array will store latitude, longitude (or some coordinate alternative), elevation).
I'm using Gdal but currently it only saves elevation values and the positional data is lost.
Current code:
import os, sys
import numpy as np
from osgeo import gdal, osr
gdal.AllRegister()
gdal.UseExceptions()
# Open Image
fpath = ('*location*\\DEM.tif')
inRas = gdal.Open(fpath)
if inRas is None:
print ('Could not open image file')
sys.exit(1)
# read in the crop data and get info about it
band1 = inRas.GetRasterBand(1)
rows = inRas.RasterYSize
cols = inRas.RasterXSize
cropData = band1.ReadAsArray(0,0,cols,rows)
Sample output from:
print(cropData.shape)
print(cropData.size)
numrows = len(cropData)
numcols = len(cropData[0])
for i in range(numrows):
for j in range(numcols):
print(cropData[i,j])