[gdal-dev] How to properly reorder the raster data from bottom up to top down?

Soon Hui Ngu soonhui.ngu at gmail.com
Wed May 22 17:54:39 PDT 2019


When creating a raster I set the lower-left x/y values, and asc file has
the following "header":

ncols        1731
nrows        1863
xllcorner    17548488.944545675069
yllcorner    -8069349.376163828187
dx           500.162987955558
dy           500.081943659035

Note that the raster data is going from bottom to top.

However, now it seems that I need to reorder the raster data so that my
raster data will appear from top to bottom, instead of bottom to top. In
the above case, I want the dy to assume negative value.

What is the best way to do it?

Of course I can do the thing in a manual, hard way, ie:

  using (var ds = Gdal.Open(demInputTiff, Access.GA_Update))
    {
        var nCol = ds.RasterXSize;
        var nRow = ds.RasterYSize;
        double[] geoTransform = new double[6];
        ds.GetGeoTransform(geoTransform);

        if (geoTransform[5] < 0)
            return; //no need transformation

        geoTransform[3] += geoTransform[5] * (nRow -1);
        geoTransform[5] *= -1;

        ds.SetGeoTransform(geoTransform);

        Band band = ds.GetRasterBand(1);
        var allValues = new int[nCol * nRow];
        var noData = band.NoDataValue();
        band.ReadRaster(0, 0, nCol, nRow, allValues, nCol, nRow, 0, 0);

        //manual reordering, crude way of iterating through the array
        var allValues2 = new int[nCol * nRow];
        for (int i = 0; i < nRow; i++)
        {
            for (int j = 0; j < nCol; j++)
            {
                allValues2[(nRow - i - 1) * nCol + j] = allValues[i * nCol + j];
            }
        }

        band.WriteRaster(0, 0, nCol, nRow, allValues2, nCol, nRow, 0, 0);


        ds.FlushCache();
    }

But is there a built-in function for this? Will converting to other formats
like AAIGRID and back and forth correct for the orientation?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20190523/e679af2c/attachment.html>


More information about the gdal-dev mailing list