0

I'm trying to take a bounding box, and crop only that section from whatever source amount of HGT files are needed. I see it should be possible from this. Using GDALWarp with the following results in an error about sizing.

#include <cstddef>
#include <iostream>
#include <vector>

extern "C" {
#include "gdal.h"
#include "gdal_utils.h"
}

int main(int argc, char **argv) {
    GDALAllRegister();

    const char *hgts[] = {
        "/tmp/icarus/lv8n/N41W121.hgt",
        "/tmp/icarus/lv8n/N41W120.hgt",
        "/tmp/icarus/lv8n/N41W119.hgt",
        "/tmp/icarus/lv8n/N40W121.hgt",
        "/tmp/icarus/lv8n/N40W120.hgt",
        "/tmp/icarus/lv8n/N40W119.hgt",
        "/tmp/icarus/lv8n/N39W121.hgt",
        "/tmp/icarus/lv8n/N39W120.hgt",
        "/tmp/icarus/lv8n/N39W119.hgt",
    };

    auto datasets = std::vector<GDALDatasetH>();
    for (auto s : hgts) {
        GDALDatasetH dataset = GDALOpen(s, GA_ReadOnly);
        if (!dataset) {
            std::cerr << "error reading: " << s << std::endl;
            continue;
        }

        datasets.push_back(dataset);
    }

    GDALWarpAppOptions *opts = GDALWarpAppOptionsNew(NULL, NULL);
    GDALWarpAppOptionsSetWarpOption(opts, "-te", "-120.5 39.2 -118.5 41.5");

    GDALDatasetH cropped = GDALWarp("/tmp/output.hgt", NULL, 9, datasets.data(), opts, NULL);
    GDALWarpAppOptionsFree(opts);

    if (!cropped) {
        return 1;
    }

    for (auto dataset : datasets) {
        GDALClose(dataset);
    }

    GDALClose(cropped);

    return 0;
}

ERROR 1: Image dimensions should be 1201x1201, 3601x3601 or 1801x3601.

Is it possible to crop out a different size for an HGT file and make a new one, or does the output crop have to be one of those sizes? If the output as an HGT needs to keep the size limits of an HGT file, is there another format this can be converted into so that elevations could be queried and only the section we care about can be kept in memory?

1
  • Simply write a different kind of file. HGT files are very peculiar, they have almost no metadata and use fixed sizes. You won't have any problems with a GeoTiff for example. Commented Aug 2 at 19:15

0

Your Answer

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

Browse other questions tagged or ask your own question.