1

I wish to dynamically build a GDAL VRT file from a java application. I really want to avoid driving the CLI from within the app. I can easily create the VRT file:

final String vrtFilePath = "target.vrt";
final Vector<String> sourceFilePaths = new Vector<String>();
sourceFilePaths.add( "aFilePath" );
sourceFilePaths.add( "anotherFilePath" );
final Vector<String> buildVRTOptions​ = new Vector<String>();
gdal.AllRegister();
final Dataset dataset =
    gdal.BuildVRT​
        (   vrtFilePath
        ,   sourceFilePaths
        ,   new BuildVRTOptions​( buildVRTOptions​ )
        );
dataset.delete();

I want to specify BuildVRTOptions​. In this case, I need the equivalent of "-te xmin ymin xmax ymax". But, I'd like to be able to specify other options too, if required. The Java API documentation does not help. I have tried following the source code, but have not been able to figure out how to specify these options. I have also examined the Python API, which is properly documented. But, it has not helped me determine the settings for Java. I assume that the options Vector contains Strings and have explored name/value pairs and some equivalents of the CLI args; without success.

Can anyone help me?

Even it it is to point me at the relevant source code or documentation.

I have seen a post that suggests that the officially correct approach is to generate the XML myself. Surely, this is not the case (for way too many good reasons to clutter up this post).

1 Answer 1

0

I had the exact same issue and like you, couldn't find an ounce of documentation anywhere on the internet regarding how to properly assign options to the BuildVRT function.

The solution lies in the gdalbuildvrt_lib.cpp file of the gdal source code. BuildVRTOptions expects a Vector of strings where the first element is the option tag (in our case "-te") and the following elements are the parameters.

Searching for "-te" in that file produces the following code to prove:

else if (EQUAL(papszArgv[iArg], "-te") && iArg + 4 < argc)
    {
        psOptions->xmin = CPLAtofM(papszArgv[++iArg]);
        psOptions->ymin = CPLAtofM(papszArgv[++iArg]);
        psOptions->xmax = CPLAtofM(papszArgv[++iArg]);
        psOptions->ymax = CPLAtofM(papszArgv[++iArg]);
    }

So to answer your question.

final String vrtFilePath = "target.vrt";
final Vector<String> sourceFilePaths = new Vector<String>();
sourceFilePaths.add( "aFilePath" );
sourceFilePaths.add( "anotherFilePath" );
final Vector<String> buildVRTOptions​ = new Vector<String>();
// Option code here
buildVRTOptions.add("-te"); // Option tag
buildVRTOptions.add(xMin); // Parameters as Strings
buildVRTOptions.add(yMin);
buildVRTOptions.add(xMax);
buildVRTOptions.add(yMax);
gdal.AllRegister();
final Dataset dataset =
    gdal.BuildVRT​
        (   vrtFilePath
        ,   sourceFilePaths
        ,   new BuildVRTOptions​( buildVRTOptions​ )
        );
System.out.println(dataset);
dataset.delete();

Produces a reference to the Dataset Object:

Dataset: org.gdal.gdal.Dataset@6ad1701a

This same process could be applied to any option for the BuildVRTOption class by searching for the Option tag desired.

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.