[gdal-dev] Extracting Z coordinate from DEM - Python

Ivan Lucena ivan.lucena at outlook.com
Mon Jul 1 05:27:12 PDT 2019


César,

The error message you posted indicated that the function float() received a NoneType argument and you have a call to ReadAsArray inside float(), so I guess that ReadAsArray has failed without an exception. Maybe you tried to read from an area outside the image.

You might want to call gdal.UseException() at the beginning of you code to get the GDAL exceptions to work. That should crash your code before calling float() but with some kind of error message. See more on this wiki page:

https://trac.osgeo.org/gdal/wiki/PythonGotchas

Cheers,

Ivan




________________________________
From: gdal-dev <gdal-dev-bounces at lists.osgeo.org> on behalf of Cesar Francisco de Paula <cesarengenheiro1 at hotmail.com>
Sent: Monday, July 1, 2019 4:56 AM
To: gdal-dev at lists.osgeo.org
Subject: [gdal-dev] Extracting Z coordinate from DEM - Python

Hi!!!



Im writing an algorithm in Python to extract Z coordinate of an DEM for a pair of X,Y coordinates. Im using GDAL. The algorithm works correctly by extracting the Z coordinate from the DEM.

Z = 1872 limiar = 0.00001
COORD_X = []
COORD_Y = []
for i in range(size):
print()
print()
print()
X = Xcp + (Z - Zcp) * (((m11 * xf[i]) + (m21 * yf[i]) - (m31 * focal)) / ((m13 * xf[i]) + (m23 * yf[i]) - (m33 * focal)))

Y = Ycp + (Z - Zcp) * (((m12 * xf[i]) + (m22 * yf[i]) - (m32 * focal)) / ((m13 * xf[i]) + (m23 * yf[i]) - (m33 * focal)))

rasterx = int((X - gt[0]) / gt[1])
rastery = int((Y - gt[3]) / gt[5])
Z_dem = float(layer.GetRasterBand(1).ReadAsArray(rasterx, rastery, 1, 1))
a = abs(Z_dem - Z)

Until this part of the algorithm everything works properly. In sequence I need to enter a condition to compare the result of the variable "a" with variable "limiar" and when I enter the conditional in the algorithm and try to execute, the python returns an error in line referring to "Z_dem".

    if (a < limiar):
    COORD_X.append(X)
    COORD_Y.append(Y)
    else:
    Z = Z_dem

Error on Python:

File "C:/Users/cesar/OneDrive/Projeto_RGB/Monorestituicao_v01.py", line 106, in Z_dem = float(layer.GetRasterBand(1).ReadAsArray(rasterx, rastery, 1, 1)) TypeError: float() argument must be a string or a number, not 'NoneType'


I don't understand this error because the value on "Z_dem" is a float and not "NoneType".


Can someone assist please?

Kind Regards.

César de Paula





















Enviado do Outlook<http://aka.ms/weboutlook>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20190701/71047138/attachment-0001.html>


More information about the gdal-dev mailing list