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

2 messages
 Hi!!! I`m writing an algorithm in Python to extract Z coordinate of an DEM for a pair of X,Y coordinates. I`m 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 _______________________________________________ gdal-dev mailing list [hidden email] https://lists.osgeo.org/mailman/listinfo/gdal-dev
 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: Cheers, Ivan From: gdal-dev <[hidden email]> on behalf of Cesar Francisco de Paula <[hidden email]> Sent: Monday, July 1, 2019 4:56 AM To: [hidden email] Subject: [gdal-dev] Extracting Z coordinate from DEM - Python   Hi!!! I`m writing an algorithm in Python to extract Z coordinate of an DEM for a pair of X,Y coordinates. I`m 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 _______________________________________________ gdal-dev mailing list [hidden email] https://lists.osgeo.org/mailman/listinfo/gdal-dev