1

How can I read a gdal vrt file with GDALOpen function?

I have a vrt file built with gdalbuildvrt and I want to read it using GDALOpen but It returns a null dataset, Is it even possible? In this code poDataset is null

GDALDataset  *poDataset;
        GDALAllRegister();

           poDataset = (GDALDataset *) GDALOpen( "home/m/3857/srtm_merged.vrt", GA_ReadOnly );
        if( poDataset != NULL )
        {

}

It seems vrt files are ony supported in a few gdals tools.

Here is VRT:

<VRTDataset rasterXSize="21746" rasterYSize="17369">
  <SRS>PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],AUTHORITY["EPSG","3857"]]</SRS>
  <GeoTransform>  5.0093307430080175e+06,  5.1196462744776348e+01,  0.0000000000000000e+00,  4.3927264476078171e+06,  0.0000000000000000e+00, -5.1196462744776348e+01</GeoTransform>
  <VRTRasterBand dataType="Int32" band="1">
    <NoDataValue>-9999</NoDataValue>
    <ColorInterp>Gray</ColorInterp>
    <ComplexSource>
      <SourceFilename relativeToVRT="1">1.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="5344" RasterYSize="6593" DataType="Int32" BlockXSize="5344" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="5344" ySize="6593" />
      <DstRect xOff="0" yOff="0" xSize="3624.28574350654" ySize="4471.35402450199" />
      <NODATA>-9999</NODATA>
    </ComplexSource>
    <ComplexSource>
      <SourceFilename relativeToVRT="1">2.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="5529" RasterYSize="6439" DataType="Int32" BlockXSize="5529" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="5529" ySize="6439" />
      <DstRect xOff="0" yOff="13147.5894347461" xSize="3624.84957383552" ySize="4221.45169215535" />
      <NODATA>-9999</NODATA>
    </ComplexSource>
    <ComplexSource>
      <SourceFilename relativeToVRT="1">3.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="5470" RasterYSize="6489" DataType="Int32" BlockXSize="5470" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="5470" ySize="6489" />
      <DstRect xOff="10871.7953859191" yOff="4468.83771045391" xSize="10874.354045833" ySize="12900.1249366381" />
      <NODATA>-9999</NODATA>
    </ComplexSource>
    <ComplexSource>
      <SourceFilename relativeToVRT="1">4.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="5344" RasterYSize="6593" DataType="Int32" BlockXSize="5344" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="5344" ySize="6593" />
      <DstRect xOff="10871.7953859191" yOff="0" xSize="3624.28574350655" ySize="4471.354024502" />
      <NODATA>-9999</NODATA>
    </ComplexSource>
  </VRTRasterBand>
</VRTDataset>

well to others may face the same problem, there must be a / at the beginning of vrt path

GDALDataset  *poDataset;
        GDALAllRegister();

           poDataset = (GDALDataset *) GDALOpen( "/home/m/3857/srtm_merged.vrt", GA_ReadOnly );
        if( poDataset != NULL )
        {

}
8
  • I have only interfaced with GDAL via python and bash, but I am certain that almost all GDAL tools work with vrts. In python, gdal.Open('blah.vrt') will open the vrt. Open your vrt in a text editor to make sure it is valid, then double-check your code.
    – Jon
    Commented Sep 28, 2017 at 14:54
  • @Jon thanks for your help, In fact I am confused about this problem, I want to use vrts as a mosaic and then easily read different subsets from them, But when gdalopen can not read them it is a big problem Commented Sep 28, 2017 at 14:58
  • 1
    According to the documentation (gdal.org/gdal_8h.html#a6836f0f810396c5e45622c8ef94624d4) it should be able to deal with virtual files. "This function will try to open the passed file, or virtual dataset name by invoking the Open method of each registered GDALDriver in turn. The first successful open will result in a returned dataset. If all drivers fail then NULL is returned and an error is issued." Commented Sep 28, 2017 at 15:06
  • Try opening a tif instead of vrt. If it works, following @MikkelLydholmRasmussen 's info, you may need to update your version of GDAL.
    – Jon
    Commented Sep 28, 2017 at 15:19
  • @MikkelLydholmRasmussen so you mean there is not any vrt driver installed? but gdalinfo returns vrt's information, so it is installed Commented Sep 28, 2017 at 15:20

1 Answer 1

2

well to others may face the same problem, there must be a / at the beginning of vrt path

    GDALDataset  *poDataset;
    GDALAllRegister();
            
    poDataset = (GDALDataset *) GDALOpen("/home/m/3857/srtm_merged.vrt",GA_ReadOnly );
    if( poDataset != NULL )
        {
        //Valid
        }
2
  • Please accept this answer (using the grey/green arrow to the left) so others will see this question listed as answered.
    – Kersten
    Commented Sep 30, 2017 at 13:17
  • @Kersten sorry I just forgot to accept it after 2 days. thanks Commented Oct 1, 2017 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.