1

I am currently trying to crop a raster by mask layer using the C++ GDAL API. I am using the GDALWarpOptions struct. GDALWarpOptions *psWO = GDALCreateWarpOptions(); I then enter all the necessary options into the struct. I then pass it to a GDALWarpOperation as explained in the GDALWarp tutorial. However for the psWO->hCutline option it is necessary to transform the geometry to the raster source dataset hSrcDS. This was explained in a previous post Cropping raster file with GDAL warp C++ API .For this there seems to two static functions declared as:

static CPLErr
LoadCutline( const char *pszCutlineDSName, const char *pszCLayer,
             const char *pszCWHERE, const char *pszCSQL,
             OGRGeometryH *phCutlineRet );
static CPLErr
TransformCutlineToSource( GDALDatasetH hSrcDS, OGRGeometryH hCutline,
                          char ***ppapszWarpOptions, char **papszTO );

that can be found in l. 222 of gdalwarp_lib.cpp. One is for loading the cutline, the other one deals with the transformation. The issue is that the functions in this file are not exposed to the API, which leads to the question:

How should a GDALWarpOperation be carried out without access to these functions?

Should I just create my own version of TransformCutlineToSource and use that?

Is there a clear reason why TransformCutlineToSource along with the other static functions aren't included in the API?

I also have trouble understanding why there is an hCutline option in the struct and there is another CUTLINE option that is entered into the **papszWarpOptions char array.

Is it a matter of preference which one I choose?

1
  • As per the Tour there should be only one question asked per question.
    – PolyGeo
    Commented Jul 30, 2021 at 19:52

1 Answer 1

1

This may be of value to future readers as I struggled with this too.

In short, I got this working by creating my own version of TransformCutlineToSource, but it was largely a copy of the open source code. I don't know why it isn't part of the API.

I didn't find myself needing to use LoadCutline anywhere. I had a shapefile with a single polygon in it, and accessed as follows:

OGRDataSource* poDS;
const char* clipPath = "C:\\SomePath\\clip.shp";
poDS = OGRSFDriverRegistrar::Open(clipPath, FALSE);
OGRLayer* lay = poDS->GetLayer(0);
OGRGeometry* geo = lay->GetNextFeature()->GetGeometryRef();

"geo" is fine to use as the cutline, as follows:

GDALWarpOptions* psWO = GDALCreateWarpOptions();
psWO->hCutline = geo;

I created an output with an alphaband and set teh interpretation of that band as follows:

GDALSetRasterColorInterpretation(GDALGetRasterBand(hDstDS, 2),GCI_AlphaBand);

Everything else was fairly standard.

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.