[gdal-dev] Resampling a raster file with GDAL C#
Jukka Rahkonen
jukka.rahkonen at maanmittauslaitos.fi
Mon Apr 18 15:39:41 PDT 2016
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-
More information about the gdal-dev
mailing list