What is the equivalent to the following GDAL terminal command in C++? I looked into GDAL C++ API, but could not figure it out.

gdalwarp -te xmin ymin xmax ymax -ts 1 1 -r max input.vrt output.vrt

for now I have the following:

GDALDataset  *poDataset = (GDALDataset *) GDALOpen(inputFilename, GA_ReadOnly);
string count_name = num_str1 + num_str2 + num_str3 + num_str4;
const char* args[] = { "-te", num_str1+" "+num_str2+" "+num_str3+" "+num_str4 , NULL };
const char* args[] = { "-ts" , "1 1" , NULL };
const char* args[] = {  "-r" , "max" , NULL };
GDALWarpAppOptions* psOptions = GDALWarpAppOptionsNew((char**)args, NULL);
GDALDatasetH pooDataset = GDALWarp("/", NULL, 1, &poDataset, psOptions, NULL);

https://gdal.org/tutorials/warp_tut.html#overview has a simple reprojection tutorial but it does not show how to use -te,-ts,-r options.

  • 1
    The two most frequent questions in comments are "What have you tried?" and "Where are you stuck?" If your Question doesn't address these, it's likely to be closed. In this case we need your code attempt.
    – Vince
    Commented Sep 25, 2022 at 12:48
  • 1
    Why are you doing this, const char* args[] = , three times on consecutive lines? Each one replaces the one before it, making the earlier ones pointless. Also, the first of the three is missing some quotation marks. Additionally, please update your post to explain what goes wrong when you run the code (including full text of any error messages). Commented Sep 25, 2022 at 23:17

1 Answer 1


(This should be a comment, but too long and too formatted.)

NB: This is using a Swift wrapper for GDAL, so not actual C++, but hopefully it will give you some idea of how I use GDALWarp(). In particular, my use case DOES use the -r option.

(Note the opts, here, is the Swift array, and cOpts is the equivalent C array.)

I hope this helps.

In brief, I just generate each element of each option as an element of the array. So in the case of the -r option, both the -r flag and the cubic value are consecutive elements of the options arrary.

        guard let destSRSWKT = srs.wkt() else { return (false, nil) }
        let opts = ["-t_srs", destSRSWKT, "-r", "cubic", "-dstalpha", "-of", driver.nameShort()]

        return withMutableArrayOfMutableCStrings(opts) { (cOpts) in
            var optionalSourceDSRef: GDALDatasetH? = _ds
            return withUnsafeMutablePointer(to: &optionalSourceDSRef) { (dsPointer) in
                let warpOpts = GDALWarpAppOptionsNew(cOpts, nil)
                if let _ = reprojectDelegate {
                    //  Not possible to pass a closure as a C function parameter if the closure contains any context
                    //  so have to convert anything required in the closure to pointer to pass in
                    //  and then within the closure convert back to object
                    let selfPtrOuter = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
                    GDALWarpAppOptionsSetProgress(warpOpts, { (progress, message, selfPtrInner) in
                        if let selfPtrInner = selfPtrInner {
                            let mySelf = Unmanaged<RasterDataset>.fromOpaque(selfPtrInner).takeUnretainedValue()
                            if let repDel = mySelf.reprojectDelegate {
                                return repDel.reprojectProgressShouldContinue(progress)
                        return 1
                    }, selfPtrOuter)
                var err: Int32 = 0
                let destDSRef = GDALWarp(url.path, nil, 1, dsPointer, warpOpts, &err)

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.