Search code examples
c#gdalosgeo

GDAL C# wrapper for VRT doesn't write a VRT file


I am using the GDAL libraries for .NET (library version 2.3.3), and the OSGeo4W shell.

What I am doing is combining two (or more) raster files into a VRT, for later processing. This occurs in the code.

As a test, in the shell, I can issue this GDAL command:

gdalbuildvrt C:\Temp\GdalTests\z7_x43_y43.vrt F:\gdal\Aero\gnc\1\0000a013.gn1 F:\gdal\Aero\gnc\1\0000b013.gn1

As expected, it produces a VRT file named "z7_x43_y43.vrt" in the listed folder.

However, the corresponding GDAL C# library does not seem able to reproduce this correct behavior; no VRT file is written to vrtFile.

I am able to use other GDAL C# libraries, such as the wrapper_GDALTranslate(...) without issue (meaning, when a destination path is provided, a file is written there), it only seems to be the BuildVRT having a problem.

Here is the code I'm using, simplified for this question:

GdalConfiguration.ConfigureGdal();
var vrtFile = @"C:\Temp\GdalTests\z7_x43_y43.vrt";
var aeroFiles = new List<string>();
aeroFiles.Add(@"F:\gdal\Aero\gnc\1\0000a013.gn1");
aeroFiles.Add(@"F:\gdal\Aero\gnc\1\0000b013.gn1");
var vrtOptions = new GDALBuildVRTOptions(new [] { "-overwrite" });
Gdal.wrapper_GDALBuildVRT_names(vrtFile, aeroFiles.ToArray(), vrtOptions, null, null);

The source files (*.gn1) use the NITF driver and the WGS84 datum and work just fine in other GDAL C# library related code tasks (e.g., I can load them as rasters, apply gdal_translate to them, etc.).

Does anyone have experience with the VRT side of the GDAL C# libraries? Suggestions for what I may be doing incorrectly, or perhaps there is an issue with the library itself?

EDIT: It's not that the call to wrapper_GDALBuildVRT_names does nothing at all... it does have a Dataset return object, which correctly represents the VRT data, and can be passed into a translate function, etc.

var vrtDataset = Gdal.wrapper_GDALBuildVRT_names(vrtFile, aeroFiles.ToArray(), vrtOptions, null, null);
Gdal.wrapper_GDALTranslate(outPath, vrtDataset, new GDALTranslateOptions(translateOptions.ToArray()),
                    null, null);

This will produce an output file at outPath correctly (e.g., PNG, GeoTIFF, etc., depending on the options). I'm just wondering why there is never a physical VRT file produced, even though we pass in a destination name (vrtFile).


Solution

  • The VRT driver does not write the VRT file until a call to dispose is made (this tripped me up initially as well). The code below should result in the expected z7_x43_y43.vrt file being written. If you plan on operating on the VRT you'll want to dispose of it at the end of usage, or dispose and reload the VRT for further use.

    GdalConfiguration.ConfigureGdal();
    var vrtFile = @"C:\Temp\GdalTests\z7_x43_y43.vrt";
    var aeroFiles = new List<string>();
    aeroFiles.Add(@"F:\gdal\Aero\gnc\1\0000a013.gn1");
    aeroFiles.Add(@"F:\gdal\Aero\gnc\1\0000b013.gn1");
    var vrtOptions = new GDALBuildVRTOptions(new [] { "-overwrite" });
    var vrtDataset = Gdal.wrapper_GDALBuildVRT_names(vrtFile, aeroFiles.ToArray(), vrtOptions, null, null);
    vrtDataset.Dispose();