<div dir="ltr">I may be able to help - I have done a fair amount of this in c#.<div><br></div><div>First a general note - the c# API is very close to the Java API - so in general you can look at that documentation.<br><br>Next - kind of a criticism. Your code is kind of messy and uncommented - for instance, you seem to get the GeoTransform twice for each row - which is massively not optimized (just once for the raster will do) - and you get the value from gt before you get it and I don't think that will work. If you want help from a community list then it is a good idea to have clear and well-commented code.<br><br></div><div>As best as I can tell - you want to convert each value in a geotiff band into x,y,z coordinates in BNG coordinates. If that is not a correct understanding then much of the rest of this mail will be wrong.</div><div><br></div><div>You can only get z from a geotiff if it is a DEM or DTM - but I assume that you know what you are doing there. You do have a problem with projection that I will come back to.</div><div><br></div><div>Finally - what you are doing is not easy - especially with a reprojection. Where I do this same thing in C# I actually use a different library (PDAL - see <a href="https://pdal.io/">https://pdal.io/</a> using the pdalc library with extensions into c# <a href="https://github.com/PDAL/CAPI">https://github.com/PDAL/CAPI</a>). This uses GDAL to load the GeoTIFF but can reproject and return the data as a cloud of points in an array. If you want to know about that - let me know and I will pm you.  <br></div><div><br></div><div>Basically - you want it in this order :<br><br>1 <span style="">Get the Geotransform (once)</span></div><div><div>2. Project the GeoTransform to BNG</div></div><div>3 iterate through the raster</div><div><br></div><div>1 - Get the Geotransform - you know how to do that</div><div><br></div><div>2 - Reproject<br><br>A couple of points:</div><div>- It is better to import the destination CRS from an EPSG code than from a proj string - there are some gaps in proj strings and they are being phased out. To get BNG as the destination - I would do ;<br><br><span style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14.6667px">SpatialReference dst = new SpatialReference(null);</span><br></div><div><span style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14.6667px">dst.importFromEPSG(27700);</span></div><div><span style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14.6667px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14.6667px">- you have not mentioned the CRS of the geotiff. I assume that it is correctly defined in the GeoTIFF o GDAL should pick it up. I also assume that it is EPSG:4326 (i.e. WGS84 latlong). In your sample code, you seem to have the src and dst the wrong way around for what you said in the text.<br><br></span></div><div><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14.6667px">Create the top left and bottom right corners from the GT. these will be 4326 - convert them as you have done but you only need to this for the two corners - not each point. Note that the raster is always 2d.<br><br>The pixel sizes from the gt are in degrees (because 4326). You probably want to dump them and recreate them. 27700 (i.e. bng) is projected and use 1m units - so you can just do a simple interpolation from the corners.</span></font></div><div><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14.6667px"><br></span></font></div><div><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14.6667px">3 Iterate</span></font></div><div><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14.6667px"><br></span></font></div><div><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14.6667px">At this point, you can get the 2D coordinates for each point by LERPing from the corners. You asked about the z coord.</span></font></div><div><font color="#000000" face="Calibri, sans-serif"><span style="font-size:14.6667px"><br></span></font></div><div><font face="Calibri, sans-serif"><span style="font-size:14.6667px"><font color="#000000">You have to aware that neither GDAL nor GeoTIFF </font>'know' about the 3rd dimension. To them it i just data. If the tiff is a DEM or DTm, then the band value for the pixel is the z coord.</span></font></div><div><font face="Calibri, sans-serif"><span style="font-size:14.6667px"><br></span></font></div><div><font face="Calibri, sans-serif"><span style="font-size:14.6667px">However, since it is just data it is not reprojected in any way. so what you get will depend on the pc of the geotiff.<br><br>For instance, if the tiff is shipped EPSG:4326 or wgs84 then the values will often be an orthogonal height above the WG84 ellipsoid. After this process, they still will be. However, BNG does not use the WGS84 ellipsoid - it uses  OSGB36. So - a naive user might expect the z values to be orthogonal height above OSGB36 or above OSGM15 (i.e. UK MSW datum). You just need to b aware.</span></font></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 19 Feb 2021 at 15:50, Wael El-Sayegh <<a href="mailto:wael_elsayegh@hotmail.com">wael_elsayegh@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">




<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div id="gmail-m_-933514579785911114appendonsend"></div>
<br>
<div id="gmail-m_-933514579785911114divRplyFwdMsg" dir="ltr">
<div> </div>
</div>
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)">I am trying to obtain and convert latitude and longitude and altitude values into British National Grid coordinates from GeoTIFF using GDAL C# file, I managed to read the coordinate
 system and corner coordinates as </span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)">The thing is, since that GDAL is more of Python documentation and I can't go through it, I am not sure what is the next step or how to do it using </span><code style="font-family:"Courier New""><span style="font-size:10pt;font-family:Consolas;color:rgb(36,39,41);border:1pt none windowtext;padding:0cm">SpatialReference()</span></code><span style="font-size:11.5pt;color:rgb(36,39,41)"> and </span><code style="font-family:"Courier New""><span style="font-size:10pt;font-family:Consolas;color:rgb(36,39,41);border:1pt none windowtext;padding:0cm">CoordinateTransformation()</span></code><span style="font-size:11.5pt;color:rgb(36,39,41)">. </span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)">This is the code I tried so far but still getting the wrong numbers, I think its because they are projected coordinate system and needed to be converted in British coordinate system. </span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)">How can I do this? </span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)"> </span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)">PS: I am not sure if the XY coordinates are right, also not sure how to read Z coordinates.</span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)"> </span></p>
<p style="margin:0cm 0cm 0px;font-size:11pt;font-family:Calibri,sans-serif;background:rgb(249,248,246)">
<span style="font-size:11.5pt;color:rgb(36,39,41)">Looking forward to hearing from you </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>GdalConfiguration.ConfigureGdal(); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            GdalConfiguration.ConfigureOgr(); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            Gdal.AllRegister(); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            Dataset ds = Gdal.Open(fileName, Access.GA_ReadOnly); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>          </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            double[] gt = new double[6]; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            int Rows = ds.RasterYSize; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            int Cols = ds.RasterXSize; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            double startX = gt[0]; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            double startY = gt[3]; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            double interval; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            Band band = ds.GetRasterBand(1); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            double xx, y; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            for (int k = 0; k < Rows; k++) </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>            { </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                ds.GetGeoTransform(gt); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                interval = gt[1]; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                ds.GetGeoTransform(gt); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                y = startY - k * interval; //Current lat </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                int[] buf = new int[Cols]; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                //ReadRaster parameters are StartCol, StartRow, ColumnsToRead, RowsToRead, BufferToStoreInto, BufferColumns, BufferRows, 0, 0 </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                band.ReadRaster(0, k, Cols, 1, buf, Cols, 1, 0, 0); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                //iterate each item in one line </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                for (int r = 0; r < Cols; r++) </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                { </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                    row = XYZData.NewRow(); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                    if (buf[r] != -32768)   //if pixel value is not NoData value </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                    { </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        xx = startX + r * interval;  //current lon  </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        SpatialReference srs = new SpatialReference("+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601271625 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs"); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        SpatialReference dst = new SpatialReference(""); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        dst.ImportFromProj4("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                       </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>         </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        double[] p = new double[3]; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        p[0] = xx; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        p[1] = y; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        p[2] = 0; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        var coordinateTransform = new CoordinateTransformation(srs, dst); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        coordinateTransform.TransformPoint(p); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        var latLong = new LatitudeLongitude(p[1], p[0]); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        var cartesian = GeoUK.Convert.ToCartesian(new Wgs84(), latLong); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        row["X"] = cartesian.X; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        row["Y"] = cartesian.Y; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        row["Z"] = 0; </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                        XYZData.Rows.Add(row.ItemArray); </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span>                    } </span></p>
<p style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif">
<span> </span></p>
<br>
</div>
</div>
</div>
</div>

_______________________________________________<br>
UK mailing list<br>
<a href="mailto:UK@lists.osgeo.org" target="_blank">UK@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/uk" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/uk</a><br>
</blockquote></div>