[gdal-dev] Resampling a raster file with GDAL C#

Didier Bernard deedeebernard at hotmail.com
Mon Apr 18 16:12:17 PDT 2016


Hi Jukka,

I really appreciate your suggestion!
Believe it or not, I am not a programmer either.

Manual editing of a .vrt file looks to be far more complicated issue than I have previously thought it would be
There is no "Pixel size" variable inside a .vrt file, but I know it's the second and fourth element of the <GeoTransform> tag. Nevertheless I do not know what do with the <DstInvGeoTransform> and how to edit it.


Maybe I should simply quit struggling with creation of a .vrt file, and go with saving a new .tif file, instead.

In this case, can somebody please provide a C# example of how a dataset can be saved into a new .tif file, but by applying the output of 50% (removing every second cell in a column and row). Basically replicating this GDAL command in C#:

    gdal_translate -outsize 50% 50% input.tif output.tif

?

I actually found some C# examples by googling but they either do not deal with -outsize 50% 50%, or I am using the wrong arguments for the following functions:

    sourceDataset.GetDriver().Create
    sourceDataset.ReadRaster
    finalDataset.WriteRaster

I would be very grateful if somebody would provide just a small C# snippet.

Thank you in advance!

Didier


________________________________________
From: gdal-dev <gdal-dev-bounces at lists.osgeo.org> on behalf of Jukka Rahkonen <jukka.rahkonen at maanmittauslaitos.fi>
Sent: Tuesday, April 19, 2016 12:39 AM
To: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] Resampling a raster file with GDAL C#

Didier Bernard <deedeebernard <at> hotmail.com> writes:


> Are you suggesting to simply convert the .tif file from command prompt by
using gdal_translate? Like so:
>
>     "C:/gdalwin32-1.4.1/bin/gdal_translate.exe" -of VRT
"C:/vesuvius_wgs84.tif" "C:/vesuvius_wgs84.vrt"
>
> ?
>
> And "build" my .vrt file according to: C:/vesuvius_wgs84.vrt?
> That won't help me for other .tif files

Of course it will help you with all files. When you know what you need to do
with one image you know what you need to do with any other image.

Do something like this:

Take a test image, for example 88x88 pixels. Run these commands:

gdal_translate -of vrt 88.png 88.vrt
gdal_translate -outsize 50% 50% -of vrt 88.png 44.vrt

Check the result:

gdalinfo 88.vrt
Driver: VRT/Virtual Raster
Files: 88.vrt
       88.png
Size is 88, 88
Pixel Size = (1.000000000000000,-1.000000000000000)

gdalinfo 44.vrt
Driver: VRT/Virtual Raster
Files: 44.vrt
       88.png
Size is 44, 44
Pixel Size = (2.000000000000000,-2.000000000000000)

All right, the result is correct, double pixel size and half of the width
and height. Now have a look at the vrt files, find what is essential and
learn to build similar ones from code.

88.vrt

<VRTDataset rasterXSize="88" rasterYSize="88">
  <GeoTransform> 9.9500000000000000e+001, 1.0000000000000000e+000,
0.0000000000000000e+000, 1.0050000000000000e+002,
0.0000000000000000e+000,-1.0000000000000000e+000</GeoTransform>
  <VRTRasterBand dataType="Byte" band="1">
    <ColorInterp>Red</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">88.png</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="88" RasterYSize="88" DataType="Byte"
BlockXSize="88" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="88" ySize="88" />
      <DstRect xOff="0" yOff="0" xSize="88" ySize="88" />
    </SimpleSource>
  </VRTRasterBand>

44.vrt

<VRTDataset rasterXSize="44" rasterYSize="44">
  <GeoTransform> 9.9500000000000000e+001, 2.0000000000000000e+000,
0.0000000000000000e+000, 1.0050000000000000e+002,
0.0000000000000000e+000,-2.0000000000000000e+000</GeoTransform>
  <Metadata domain="IMAGE_STRUCTURE">
    <MDI key="INTERLEAVE">PIXEL</MDI>
  </Metadata>
  <VRTRasterBand dataType="Byte" band="1">
    <ColorInterp>Red</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">88.png</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="88" RasterYSize="88" DataType="Byte"
BlockXSize="88" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="88" ySize="88" />
      <DstRect xOff="0" yOff="0" xSize="44" ySize="44" />
    </SimpleSource>

Find the differences: The latter seems to differ here:
<VRTDataset rasterXSize="44" rasterYSize="44">
and here
  <GeoTransform> 9.9500000000000000e+001, 2.0000000000000000e+000,
0.0000000000000000e+000, 1.0050000000000000e+002,
0.0000000000000000e+000,-2.0000000000000000e+000</GeoTransform>
and here:
      <DstRect xOff="0" yOff="0" xSize="44" ySize="44" />

I am not a programmer but obviously you are and because programmers tend to
be good at doing some small calculations with numbers I am sure that you can
- find SourceProperties (here RasterXSize="88" RasterYSize="88")
- devide by 2 and feed values to VRTDataset rasterXSize and rasterYSize
- feed the same numbers to DstRect xSize and ySize
- double the pixel size in 2nd and 6th terms of GeoTransform


-Jukka Rahkonen-






_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list