<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><span style="display: inline !important; background-color: rgb(255, 255, 255); font-family: Arial,Helvetica,sans-serif; font-size: 10pt; font-size-adjust: none">César</span>,</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span>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.</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
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:</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><a id="LPNoLP814708" href="https://trac.osgeo.org/gdal/wiki/PythonGotchas">https://trac.osgeo.org/gdal/wiki/PythonGotchas</a><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
Cheers,</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
Ivan</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div id="appendonsend"></div>
<div style="color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font color="#000000" face="Calibri, sans-serif" style="font-size:11pt"><b>From:</b> gdal-dev <gdal-dev-bounces@lists.osgeo.org> on behalf of Cesar Francisco de Paula <cesarengenheiro1@hotmail.com><br>
<b>Sent:</b> Monday, July 1, 2019 4:56 AM<br>
<b>To:</b> gdal-dev@lists.osgeo.org<br>
<b>Subject:</b> [gdal-dev] Extracting Z coordinate from DEM - Python</font>
<div> </div>
</div>
<div dir="ltr">
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
Hi!!!</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<p style="margin-top: 0px; margin-bottom: 0px;">I<code>m writing an algorithm in Python to extract Z coordinate of an DEM for a pair of X,Y coordinates. I</code>m using GDAL. The algorithm works correctly by extracting the Z coordinate from the DEM.</p>
<pre><code><span>Z </span><span>=</span><span> </span><span>1872</span><span> limiar </span><span>=</span><span> </span><span>0.00001</span><span>
COORD_X </span><span>=</span><span> </span><span>[]</span><span>
COORD_Y </span><span>=</span><span> </span><span>[]</span><span>
</span><span>for</span><span> i </span><span>in</span><span> range</span><span>(</span><span>size</span><span>):</span><span>
</span><span>print</span><span>()</span><span>
</span><span>print</span><span>()</span><span>
</span><span>print</span><span>()</span><span>
X </span><span>=</span><span> </span><span>Xcp</span><span> </span><span>+</span><span> </span><span>(</span><span>Z </span><span>-</span><span> </span><span>Zcp</span><span>)</span><span> </span><span>*</span><span> </span><span>(((</span><span>m11 </span><span>*</span><span> xf</span><span>[</span><span>i</span><span>])</span><span> </span><span>+</span><span> </span><span>(</span><span>m21 </span><span>*</span><span> yf</span><span>[</span><span>i</span><span>])</span><span> </span><span>-</span><span> </span><span>(</span><span>m31 </span><span>*</span><span> focal</span><span>))</span><span> </span><span>/</span><span> </span><span>((</span><span>m13 </span><span>*</span><span> xf</span><span>[</span><span>i</span><span>])</span><span> </span><span>+</span><span> </span><span>(</span><span>m23 </span><span>*</span><span> yf</span><span>[</span><span>i</span><span>])</span><span> </span><span>-</span><span> </span><span>(</span><span>m33 </span><span>*</span><span> focal</span><span>)))</span><span>
</span><span>
Y </span><span>=</span><span> </span><span>Ycp</span><span> </span><span>+</span><span> </span><span>(</span><span>Z </span><span>-</span><span> </span><span>Zcp</span><span>)</span><span> </span><span>*</span><span> </span><span>(((</span><span>m12 </span><span>*</span><span> xf</span><span>[</span><span>i</span><span>])</span><span> </span><span>+</span><span> </span><span>(</span><span>m22 </span><span>*</span><span> yf</span><span>[</span><span>i</span><span>])</span><span> </span><span>-</span><span> </span><span>(</span><span>m32 </span><span>*</span><span> focal</span><span>))</span><span> </span><span>/</span><span> </span><span>((</span><span>m13 </span><span>*</span><span> xf</span><span>[</span><span>i</span><span>])</span><span> </span><span>+</span><span> </span><span>(</span><span>m23 </span><span>*</span><span> yf</span><span>[</span><span>i</span><span>])</span><span> </span><span>-</span><span> </span><span>(</span><span>m33 </span><span>*</span><span> focal</span><span>)))</span><span>

rasterx </span><span>=</span><span> int</span><span>((</span><span>X </span><span>-</span><span> gt</span><span>[</span><span>0</span><span>])</span><span> </span><span>/</span><span> gt</span><span>[</span><span>1</span><span>])</span><span>
rastery </span><span>=</span><span> int</span><span>((</span><span>Y </span><span>-</span><span> gt</span><span>[</span><span>3</span><span>])</span><span> </span><span>/</span><span> gt</span><span>[</span><span>5</span><span>])</span><span>
Z_dem </span><span>=</span><span> float</span><span>(</span><span>layer</span><span>.</span><span>GetRasterBand</span><span>(</span><span>1</span><span>).</span><span>ReadAsArray</span><span>(</span><span>rasterx</span><span>,</span><span> rastery</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>1</span><span>))</span><span>
a </span><span>=</span><span> abs</span><span>(</span><span>Z_dem </span><span>-</span><span> Z</span><span>)</span></code></pre>
<p style="margin-top: 0px; margin-bottom: 0px;">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".</p>
<pre><code><span>    </span><span>if</span><span> </span><span>(</span><span>a </span><span><</span><span> limiar</span><span>):</span><span>
    COORD_X</span><span>.</span><span>append</span><span>(</span><span>X</span><span>)</span><span>
    COORD_Y</span><span>.</span><span>append</span><span>(</span><span>Y</span><span>)</span><span>
    </span><span>else</span><span>:</span><span>
    Z </span><span>=</span><span> Z_dem</span></code></pre>
<p style="margin-top: 0px; margin-bottom: 0px;">Error on Python:</p>
<p style="margin-top: 0px; margin-bottom: 0px;"><strong><em>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'</em></strong></p>
<p style="margin-top: 0px; margin-bottom: 0px;"><strong><em><br>
</em></strong></p>
<p style="margin-top: 0px; margin-bottom: 0px;">I don't understand this error because the value on "Z_dem" is a float and not "NoneType". </p>
<p style="margin-top: 0px; margin-bottom: 0px;"><br>
</p>
<p style="margin-top: 0px; margin-bottom: 0px;">Can someone assist please?</p>
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
Kind Regards.</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
César de Paula</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div style="color:rgb(0,0,0); font-family:Arial,Helvetica,sans-serif; font-size:10pt">
<br>
</div>
<div id="x_Signature">
<p style="margin-top: 0px; margin-bottom: 0px;">Enviado do <a href="http://aka.ms/weboutlook">
Outlook</a><br>
</p>
</div>
</div>
</body>
</html>