1

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.

Is there any built-in function for this?

I could have reorder it manually:

   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 I'm pretty sure that this is not the most efficient way of doing things, both memory and speed wise. There is also an issue of whether .Net array can hold that big the array size or not.

Is there a built-in function for this?

  • 1
    If GDAL reads it properly (no reason it shouldn't) translate (with GDAL_Translate utility) to GeoTIFF (GTIFF) or imagine (HFA) format then translate to AAIGRID and the pixel ordering should be top-down. – Michael Stimson May 21 at 7:08
  • @MichaelStimson, is the performance/memory better in that case? – Graviton May 21 at 7:12
  • @MichaelStimson is there any guarantee that such translation to AAIGRID will always result in top down pixel ? Guarantee as in the sense that this behavior is explicitly documented and unlikely to change – Graviton May 21 at 12:18
  • You can read the code in the driver github.com/OSGeo/gdal/blob/master/gdal/frmts/aaigrid/… and see how it is implemented, anecdotally AAIGRID files are written top down but I don't know for sure whether it's a copy of the existing orientation or an assertion of top down georeference; You might be able to convert directly from asc to asc but proceeding via a GeoTIFF or HFA should rectify the bottom up georeference, HFA being safer as TIFF files can have an orientation other than top-left addressing. – Michael Stimson May 21 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.