2

I've been playing around with C++ and the API for GDAL to create some functions that I have done using Python. One of them was to create a virtual raster based on a list of files found in certain directory. I accomplished the later using the boost library. And when it comes the turn to write the virtual raster, I find that the vrt generated only contains one band. I checked some questions regarding this problems, and the most close to my issue is found in the following link:

https://gis.stackexchange.com/questions/269976/c-gdal-api-add-more-than-one-rasters-to-vrt

However when compiling my program using g++ -o stack_raster.exe stack_raster.cpp -lboost_system -lboost_filesystem -lgdal

I get the following message:

stack_raster.cpp:35:99: error: 'YES' was not declared in this scope ver->CreateCopy("media/B02_stack.vrt", poSrcDS,RALSE,APPEND_SUBDATASET=YES,NULL,NULL,NULL);

Before I was having no issues during the compilation by replacing APPEND_SUBDATASET=YES,NULL,NULL,NULL), for NULL. The resulted Virtual raster generated only contains one band, as expected. Could someone gives me a hint about my problem?

This is my code:

#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h" /* for CPLMalloc() */
#include "gdal/vrtdataset.h"
#include <iostream>
#include <string>
#include "gdal/cpl_string.h"
#include <stdbool.h>


using namespace std;
using namespace boost::filesystem;

int main()
{
    GDALAllRegister();

    path p("/home/roger/Documents/09_Stack_Raster/media");
    GDALDriver *poDriver = (GDALDriver*) GDALGetDriverByName( "VRT" );
    GDALDataset *poSrcDS, *poVRTDS;
    string bdir = "media/";

    for (auto i = directory_iterator(p); i != directory_iterator(); i++)
    {   
        string filename = i -> path().filename().string();
        cout << filename << endl;
        if (!is_directory(i->path()) && boost::ends_with(filename, ".tif"))
        {
            cout << i->path().filename().string() << endl;
            std::string str_path = "media/" + i->path().filename().string();
            poSrcDS = (GDALDataset *) GDALOpenShared(str_path.c_str(), GA_ReadOnly);
            poVRTDS = poDriver->CreateCopy("media/B02_stack.vrt", poSrcDS,FALSE,APPEND_SUBDATASET=YES,NULL,NULL);
            GDALClose((GDALDatasetH) poSrcDS);
        }

        else
            continue;
    }

    GDALClose((GDALDatasetH) poVRTDS);
}

1 Answer 1

0

I would expect that in the line:

poVRTDS = poDriver->CreateCopy("media/B02_stack.vrt", poSrcDS,FALSE,APPEND_SUBDATASET=YES,NULL,NULL);

you need to place quotes around (at least) APPEND_SUBDATASET=YES:

poVRTDS = poDriver->CreateCopy("media/B02_stack.vrt", poSrcDS,FALSE,"APPEND_SUBDATASET=YES",NULL,NULL);
2
  • I changed the line to yours and it compiled. I created a char **papszOptions = CSLSetNameVlue(papszOptions, "APPEND_SUBDATASET=YES), and afterwards pass it as variable in CreateCopy, so the next line is poVRTDS= poDriver->CreateCopy("media/B02_stack.vrt", poSrcDS, FALSE, papszOptions, NULL, NULL), but now I receive the following message: Segmentation fault (cored dumped) Commented Aug 13, 2019 at 18:49
  • This sort of programming question is better asked on stack overflow or the gdal user list.
    – Ian Turton
    Commented Aug 13, 2019 at 19:05

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.