daamiansz
daamiansz

Reputation: 57

Tiff to array - error

Hello, I have some problem with converting Tiff file to numpy array. I have a 16 bit signed raster file and I want to convert it to numpy array. I using to this gdal libarary.

import numpy
from osgeo import gdal
ds = gdal.Open("C:/.../dem.tif")
dem = numpy.array(ds.GetRasterBand(1).ReadAsArray())

At first glance, everything converts well, but I compared the result obtained in python with result in GIS software and I got different results.

Python result

Python result

Arcmap result

Arcmap result

I found many value in numpy array that are below 91 and 278 (real min and max values), that should not exist.

Upvotes: 0

Views: 892

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64443

GDAL already returns a Numpy array, and wrapping it in np.array by default creates a copy of that array. Which is an unnecessary performance hit. Just use:

dem = ds.GetRasterBand(1).ReadAsArray()

Or if its a single-band raster, simply:

dem = ds.ReadAsArray()

Regading the statistics, are you sure ArcMap shows the absolute high/low value? I know QGIS for example often draws the statistics from a sample of the dataset (for performance) and depending on the settings sometimes uses a percentile (eg 1%, 99%).

edit: BTW, is this a public dataset? Like an SRTM tile? It might help if you list the source.

Upvotes: 1

Related Questions