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

Didier Bernard deedeebernard at hotmail.com
Mon Apr 18 14:21:02 PDT 2016


Hi Even,

I loaded the initial vesuvius_wgs84.tif file by using C# bindings.
Then I've read the following of its dataset variables and properties: dataset.RasterXSize, dataset.RasterYSize and dataset.GetGeoTransform. Then I took a template .VRT example, and replaced the upper mentioned values in that template. I saved that template with the same name as the initial .tif file: vesuvius_wgs84.vrt. I hope it makes sense now.


    "I'd recommand you do it
    manally with gdal_translate -of VRT to have a reference VRT, and then modify
    your code until it produces the same result."

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.

Can you show me how to replicate this GDAL command:

    "C:/gdalwin32-1.4.1/bin/gdal_translate.exe" -of VRT "C:/vesuvius_wgs84.tif" "C:/vesuvius_wgs84.vrt"

In GDAL C#?

Thank you.

Didier.

________________________________________
From: Even Rouault <even.rouault at spatialys.com>
Sent: Monday, April 18, 2016 10:40 PM
To: Didier Bernard
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] Resampling a raster file with GDAL C#

Le lundi 18 avril 2016 22:35:42, Didier Bernard a écrit :
> Hi Even,
>
> I can not use GDAL commands from command prompt. I have to resample my .tif
> by using GDAL C# bindings.
>
> I manually created the previously shown .vrt file, based on examples from
> GDAL .vrt tutorial page: http://www.gdal.org/gdal_vrttut.html
>
> I took one example from mentioned page, and replaced the
> dataset.RasterXSize, dataset.RasterYSize and dataset.GetGeoTransform
> values according to the data returned from the original (non-resampled:
> "vesuvius_wgs84.tif") .tif file.
>
> What is wrong with the XML code of the .VRT file I posted in my previous
> reply?

I'm not sure to understand what you do exactly... I'd recommand you do it
manally with gdal_translate -of VRT to have a reference VRT, and then modify
your code until it produces the same result.

>
> Thank you.
> Didier
>
> ________________________________________
> From: Even Rouault <even.rouault at spatialys.com>
> Sent: Monday, April 18, 2016 10:25 PM
> To: gdal-dev at lists.osgeo.org
> Cc: Didier Bernard
> Subject: Re: [gdal-dev] Resampling a raster file with GDAL C#
>
> Le lundi 18 avril 2016 19:59:32, Didier Bernard a écrit :
> > Hello,
> >
> >
> > I am trying to resample a .tif raster in C# GDAL in a way that every
> > other cell in a row and column is removed.
> >
> > Something similar to the lower GDAL command:
> >     gdal_translate -outsize 50% 0 input.tif output.tif
> >
> > But this command creates a new resampled .tif file.
> > While I would like to avoid saving a new .tif file, and if possible just
> > resample the dataset.
> >
> > It looks like this can be achieved by the use of VRT
> > file<http://www.gdal.org/gdal_vrttut.html>.
> >
> > Here is one example of a .vrt file, which resamples the .tif file by
> > removing the every other cell:
> >
> >
> > <VRTDataset rasterXSize="42" rasterYSize="32">
> >
> >   <GeoTransform>14.3779166666, 0.000833333333333, 0.0, 40.8404166667,
> >   0.0,
> >
> > -0.000833333333333</GeoTransform> <VRTRasterBand dataType="Byte"
> > band="1">
> >
> >     <ColorInterp>Gray</ColorInterp>
> >     <SimpleSource resampling="bilinear">
> >
> >       <SourceFilename
> >       relativeToVRT="1">vesuvius_wgs84.tif</SourceFilename>
> >       <SourceBand>1</SourceBand>
> >       <SrcRect xOff="0" yOff="0" xSize="85" ySize="65"/>
> >       <DstRect xOff="0" yOff="0" xSize="42" ySize="32"/>
> >
> >     </SimpleSource>
> >
> >   </VRTRasterBand>
> >
> > </VRTDataset>
>
> It is not clear how you produced this VRT file. If you for example create
> it with gdal_translate -of VRT, you'll see that the resolution is doubled.
>
> > What I noticed is that resolution of the resampled dataset does not
> > change in comparison to the original dataset.
> >
> > In this case it remains the 0.00083 degrees.
> > The same thing happens if I reproject the .tif file to some projected
> > coordinate system (UTM for example): resolution remains the same (70
> > meters for example).
> >
> > Why is this the case?
> >
> > Shouldn't the resolution of the resampled dataset be twice of the
> > resolution of the original (non-resampled) dataset, based on the fact
> > that every other cell is removed (original dataset: 85x65, resampled
> > dataset: 42x32)?
> >
> >
> > I would be very grateful if I could get any kind of reply.
> >
> > Thank you in advance.
> >
> > With kind regards,
> > Didier
>
> --
> Spatialys - Geospatial professional services
> http://www.spatialys.com

--
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list