[OSGeo-UK] Fw: Get Lat/Long/Altitude data from GeoTiff File

Wael El-Sayegh wael_elsayegh at hotmail.com
Fri Feb 19 07:50:06 PST 2021






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

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 SpatialReference() and CoordinateTransformation().

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.

How can I do this?



PS: I am not sure if the XY coordinates are right, also not sure how to read Z coordinates.



Looking forward to hearing from you



GdalConfiguration.ConfigureGdal();

            GdalConfiguration.ConfigureOgr();

            Gdal.AllRegister();



            Dataset ds = Gdal.Open(fileName, Access.GA_ReadOnly);









            double[] gt = new double[6];

            int Rows = ds.RasterYSize;

            int Cols = ds.RasterXSize;

            double startX = gt[0];

            double startY = gt[3];

            double interval;

            Band band = ds.GetRasterBand(1);

            double xx, y;



            for (int k = 0; k < Rows; k++)

            {



                ds.GetGeoTransform(gt);



                interval = gt[1];

                ds.GetGeoTransform(gt);

                y = startY - k * interval; //Current lat

                int[] buf = new int[Cols];

                //ReadRaster parameters are StartCol, StartRow, ColumnsToRead, RowsToRead, BufferToStoreInto, BufferColumns, BufferRows, 0, 0

                band.ReadRaster(0, k, Cols, 1, buf, Cols, 1, 0, 0);

                //iterate each item in one line

                for (int r = 0; r < Cols; r++)

                {

                    row = XYZData.NewRow();

                    if (buf[r] != -32768)   //if pixel value is not NoData value

                    {

                        xx = startX + r * interval;  //current lon



                        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");



                        SpatialReference dst = new SpatialReference("");

                        dst.ImportFromProj4("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");





                        double[] p = new double[3];

                        p[0] = xx;

                        p[1] = y;

                        p[2] = 0;



                        var coordinateTransform = new CoordinateTransformation(srs, dst);



                        coordinateTransform.TransformPoint(p);



                        var latLong = new LatitudeLongitude(p[1], p[0]);

                        var cartesian = GeoUK.Convert.ToCartesian(new Wgs84(), latLong);



                        row["X"] = cartesian.X;

                        row["Y"] = cartesian.Y;

                        row["Z"] = 0;

                        XYZData.Rows.Add(row.ItemArray);





                    }



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/uk/attachments/20210219/1e341099/attachment-0001.html>


More information about the UK mailing list