Paamand
Paamand

Reputation: 716

Using WarpOptions in GDAL for Android (java via swig binding)

I want to reproject an geospatial image using GDAL compiled for Android. I am currently using the swig bindings but am considering going jni/ndk.

I have successfully warped an image with the AutoCreateWarpedVRT function, but I would like to use more options (e.g. cropping the output). Currently the below is my attempt at warping using the Warp. It produces an output raster that is not warped at all, and also is not applying the -te options.

The documentation for the GDAL swig bindings is very sparse (link) and I suspect that I did not get the WarpOptions right.

Any suggestions on how to make the WarpOptions(Vector) arguments work (or any of the ???Options(Vector) for that sake) is appreciated.

        Dataset src = gdal.Open("input_raster.jpg");
        src.SetProjection(src_wkt);

        // Set reference points
        double west = 451000;
        double east = 501005;
        double south = 6214995;
        double north = 6257000;
        int width = src.getRasterXSize();
        int height = src.getRasterYSize();
        GCP[] gcps = {  new GCP(0, 0, west, north),
                        new GCP(0, height, west, south),
                        new GCP(width, 0, east, north),
                        new GCP(width, height, east, south)};
        src.SetGCPs(gcps, dst_wkt);

        // Try to warp
        Dataset[] src_array = {src};
        WarpOptions warpOptions = new WarpOptions(
            new Vector(Arrays.asList("s_srs EPSG:32632", "t_srs EPSG:3857", "te 1000 820"))
        );

        Dataset warp = gdal.Warp("warp.vrt", src_array, warpOptions);
        Dataset warp_png = driverPNG.CreateCopy("warp_raster.png", warp);
        //Produces a raster output without errors, but does not apply the warp.            

        src.delete();
        warp.delete();
        warp_png.delete();

Upvotes: 1

Views: 530

Answers (1)

Paamand
Paamand

Reputation: 716

According to this discussion the syntax should be:

    Vector<String> options = new Vector<>();
    options.add("-s_srs");
    options.add("EPSG:32632");
    options.add("-t_srs");
    options.add("EPSG:3857");

Seems great, but throws a runtime error WarpOptions.java:17

    if (cPtr == 0)
        throw new RuntimeException();

So, apart from the error I think the solution is correct. The error may be platform specific.

Upvotes: 1

Related Questions