0

I'm currently trying to read a raster file with GDAL and C#. The reading part is fine, but I have troubles with reading the correct bounding box of the raster to map it correctly to the screen. If the coordinates are in lat, long then it's fine. However, if they're in pixel values (for example with EPSG:3857 Web Mercator), I can't read them out correctly. I know Gdalinfo include the original bounding box (see below).

Driver: GTiff/GeoTIFF
Files: F:\ArcGis\Gdal\NE1_50M_SR_W\DensityMaps\Pop1Warpped.tif
Size is 141988, 60942
Coordinate System is:
PROJCS["World_Mollweide",
GEOGCS["GCS_WGS_1984",
    DATUM["D_WGS_1984",
        SPHEROID["WGS_1984",6378137.0,298.257223563]],
    PRIMEM["Greenwich",0.0],
    UNIT["Degree",0.017453292519943295]],
PROJECTION["Mollweide"],
PARAMETER["False_Easting",0.0],
PARAMETER["False_Northing",0.0],
PARAMETER["Central_Meridian",0.0],
UNIT["Meter",1.0]]
Origin = (-17619594.547443531000000,8750529.461868489200000)
Pixel Size = (250.000000000000000,-250.000000000000000)
Metadata:
    AREA_OR_POINT=Area
Image Structure Metadata:
   COMPRESSION=LZW
   INTERLEAVE=BAND
Corner Coordinates:
   Upper Left  (-17619594.547, 8750529.462) (  4d35'30.71"W, 83d37'50.88"N)
   Lower Left  (-17619594.547,-6484970.538) (107d 3'58.73"E, 55d59' 2.97"S)
   Upper Right (17877405.453, 8750529.462) ( 15d11'38.88"E, 83d37'50.88"N)
   Lower Right (17877405.453,-6484970.538) (103d21'55.35"W, 55d59' 2.97"S)
   Center      (  128905.453, 1132779.462) (  1d17'47.23"E,  9d10'35.76"N)

As you can see the upper left includes the coordinates in lat, long as well. Is there anyway I can read it out programmatically?

1 Answer 1

0

So after digging a little bit, I finally managed to read the bounding box correctly. You just have to reproject the points to EPSG 4326 (Default Spatial reference) to get lat, long. Here is the code in C#

private Point3D ProjectPointToSRS(string oldSR, int defaultSRS, double[] point)
    {
        var oSR = new SpatialReference("");
        oSR.ImportFromWkt(ref oldSR);

        var defaultSR = new SpatialReference("");
        defaultSR.ImportFromEPSG(defaultSRS);

        var coordinateTransform = new CoordinateTransformation(oSR, defaultSR);
        coordinateTransform.TransformPoint(new double[] { point[0], point[1] });

        return new Point3D(point[0], point[1], 0);
    }

And then get the bounding box normally

 private void GetBoundingBox()
    {
        if (!IsValidFile || string.IsNullOrEmpty(SpatialReference))
            return;

        double[] geotransform = new double[6];

        _dataset.GetGeoTransform(geotransform);

        var ulX = geotransform[0];
        var ulY = geotransform[3];
        var xRes = geotransform[1];
        var yRes = geotransform[5];

        var lrX = ulX + RasterXSize * xRes;
        var lrY = ulY + RasterYSize * yRes;

        if (!IsSameDefaultProjection(SpatialReference, DEFAULT_SPATIAL_REFERENCE)) {
            var ul = ProjectPointToSRS(SpatialReference, DEFAULT_SPATIAL_REFERENCE, new double[] { ulX, ulY });
            var lr = ProjectPointToSRS(SpatialReference, DEFAULT_SPATIAL_REFERENCE, new double[] { lrX, lrY });
            Bbox = new BoundingBox(ul.Y, ul.X, lr.Y, lr.X);

        }
        else 
            Bbox = new BoundingBox(ulY, ulX, lrY, lrX);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.