I am attempting to polygonize a raster using GDAL C API. The input rasters can contain nodata pixels. My current method (see the code below) produces a new polygon vector, but the output also includes polygons representing nodata pixels. Is there a way to exclude nodata pixels from being polygonized or to remove new nodata polygons after polygonization?

driver = GDALGetDriverByName("ESRI Shapefile");
dh = GDALCreate(driver, vector_fname, 0, 0, 0, GDT_Unknown, NULL );
OGRSpatialReferenceH srs = OSRNewSpatialReference(srs_txt);

layer = GDALDatasetCreateLayer(dh, "new_layer", srs, wkbMultiPolygon, NULL );

field = OGR_Fld_Create("new_field", OFTInteger);
OGR_L_CreateField(layer, field, TRUE) 

GDALPolygonize(hBand, NULL, layer, 0, NULL, NULL, NULL);
  • The 2nd argument to GDALPolygonize is a mask band which is used to select valid pixels (any value in the mask band >= 1). – user2856 Apr 29 '18 at 9:52
  • 1
    Thanks Luke! Can the mask be the same as an input file (e.g. GDALPolygonize(hBand, hBand, layer, 0, NULL, NULL, NULL); or should it be somehow preprocessed first? – Jot eN Apr 29 '18 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.