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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

nsoonhui

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?


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to properly reorder the raster data from bottom up to top down?

nsoonhui
Anyone has any idea on this?



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev