[GIS] Resampling a raster file with GDAL C#

cgdalrasterresampling

Is it possible to resample the raster by using GDAL C# binaries, in a way that every other cell in a row and column is removed?

Basically a replication of this GDAL command:

gdal_translate -outsize 50% 50% C:/input.tif C:/output.tif

Looks like I need to create a new dataset first:

outputDataset.GetDriver().Create("C:/output.tif", inputDataset.RasterXSize/2, inputDataset.RasterYSize/2, channelCount, DataType.GDT_Byte, null); 

Then read the elevation values from the initial (non-resampled) dataset by applying the buffer size to be half of those the inputDataset had (inputDataset.RasterXSize/2 , inputDataset.RasterYSize/2):

inputDataset.ReadRaster(0, 0, inputDataset.RasterXSize, inputDataset.RasterYSize, buffers, inputDataset.RasterXSize/2, inputDataset.RasterYSize/2, inputDataset.RasterCount, [inputDataset.RasterCount], 0, 0, 0);

And finally write the elevation values to a new .tif file:

finalDataset.WriteRaster(0, 0, inputDataset.RasterXSize, inputDataset.RasterYSize, buffers, inputDataset.RasterXSize/2, inputDataset.RasterYSize/2, inputDataset.RasterCount, [inputDataset.RasterCount], 0, 0, 0);

But the final .tif file I am getting does not seem to be correct.

Can somebody provide a C# example please?

Best Answer

If you download the GDAL source, you'll have the source for gdal_translate.cpp. Just peruse the code and duplicate it in C#. The translation is straight-forward.