<div dir="ltr"><div><div class="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi list,</div><div><br></div><div>I need to get the pixel value of my tiff-file.</div><div>Using gdallocationinfo.exe I can get the correct value:</div><div dir="ltr"><font face="monospace, monospace">gdallocationinfo -wgs84 -valonly 201612021600.tif 5.22543631866552 51.2581472440154</font></div><div dir="ltr"><br></div><div dir="ltr">Now I need to do this in my C# code. Using examples I found with Google I came up with this piece of code, I also looked at the C++ code of gdallocationinfo</div><div dir="ltr"><br></div><div dir="ltr"><div dir="ltr"><font face="monospace, monospace">public string GdalLocate(string input, double x, double y, bool inWgs84 = true)</font></div><div dir="ltr"><font face="monospace, monospace">{</font></div><div dir="ltr"><font face="monospace, monospace"> using (var ds = Gdal.Open(input, Access.GA_ReadOnly))<br></font></div><div dir="ltr"><font face="monospace, monospace"> {</font></div><div dir="ltr"><font face="monospace, monospace"> if (ds == null) throw new Exception("Can't open GDAL dataset: " + input);</font></div><div dir="ltr"><font face="monospace, monospace"><br></font></div><div dir="ltr"><font face="monospace, monospace"> int col;<br></font></div><div dir="ltr"><font face="monospace, monospace"> int row;</font></div><div dir="ltr"><font face="monospace, monospace"> if (inWgs84)</font></div><div dir="ltr"><font face="monospace, monospace"> {</font></div><div dir="ltr"><font face="monospace, monospace"> var pGT = new double[6];</font></div><div dir="ltr"><font face="monospace, monospace"> ds.GetGeoTransform(pGT);</font></div><div dir="ltr"><font face="monospace, monospace"> col = (int) Math.Floor((x - pGT[0]) / pGT[1]);</font></div><div dir="ltr"><font face="monospace, monospace"> row = (int) Math.Floor((pGT[3] - y) / -pGT[5]);</font></div><div dir="ltr"><font face="monospace, monospace"> }</font></div><div dir="ltr"><font face="monospace, monospace"> else</font></div><div dir="ltr"><font face="monospace, monospace"> {</font></div><div dir="ltr"><font face="monospace, monospace"> col = (int)Math.Floor(x);</font></div><div dir="ltr"><font face="monospace, monospace"> row = (int)Math.Floor(y);</font></div><div dir="ltr"><font face="monospace, monospace"> }</font></div><div dir="ltr"><font face="monospace, monospace"><br></font></div><div dir="ltr"><font face="monospace, monospace"> // Check input:</font></div><div dir="ltr"><div dir="ltr"><font face="monospace, monospace"> var rows = ds.RasterYSize;<br></font></div><div dir="ltr"><font face="monospace, monospace"> var cols = ds.RasterXSize;</font></div></div><div dir="ltr"><font face="monospace, monospace"> if (col < 0 || row < 0 || col > cols || row > rows)</font></div><div dir="ltr"><font face="monospace, monospace"> {</font></div><div dir="ltr"><font face="monospace, monospace"> // Location is off this file! No further details to report.</font></div><div dir="ltr"><font face="monospace, monospace"> return string.Empty;</font></div><div dir="ltr"><font face="monospace, monospace"> }</font></div><div dir="ltr"><font face="monospace, monospace"><br></font></div><div dir="ltr"><font face="monospace, monospace"> // Get the first band:</font></div><div dir="ltr"><font face="monospace, monospace"> using (var band = ds.GetRasterBand(1))</font></div><div dir="ltr"><font face="monospace, monospace"> {</font></div><div dir="ltr"><font face="monospace, monospace"> // Creating a C# array to hold the image data</font></div><div dir="ltr"><font face="monospace, monospace"> var byteBuffer = new byte[cols * rows];</font></div><div dir="ltr"><font face="monospace, monospace"> // Read the raster</font></div><div dir="ltr"><font face="monospace, monospace"> var IsThereAnError = band.ReadRaster(row, col, 1, 1, byteBuffer, 1, 1, 0, 0);</font></div><div dir="ltr"><font face="monospace, monospace"> if (IsThereAnError == CPLErr.CE_None) return byteBuffer[0].ToString();</font></div><div dir="ltr"><font face="monospace, monospace"><br></font></div><div dir="ltr"><font face="monospace, monospace"> throw new Exception("Error in reading raster using GdalLocate.");</font></div><div dir="ltr"><font face="monospace, monospace"> }</font></div><div dir="ltr"><font face="monospace, monospace"> }</font></div><div dir="ltr"><font face="monospace, monospace">}</font></div><div dir="ltr"><br></div><div>The command line call returns 29.</div><div>My method using the same input file and coordinates returns 1.</div><div><br></div><div>So I must be missing something!</div><div dir="ltr"><br></div><div>Thanks in advanced for any help you can provide.</div>
<br>
Paul<br><div style="font-size:13.3px;font-family:verdana,arial,helvetica,sans-serif"><br></div>
</div></div></div></div></div></div>
</div>