Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided band.
From GDALPolygonize function header: "Polygon features will be created on the output layer, with polygon geometries representing the polygons". So,the WKB geometry type should be "wkbPolygon"
Optimization: Apply a OGR SQL filter to the layer to select the features different from NODATA value.
Thanks to David Zwarg.
945 CPLErr cplerr = CE_None;
948 OGRSFDriverH ogr_drv = NULL;
949 GDALDriverH gdal_drv = NULL;
950 int destroy_gdal_drv = 0;
951 GDALDatasetH memdataset = NULL;
952 GDALRasterBandH gdal_band = NULL;
953 OGRDataSourceH memdatasource = NULL;
955 OGRLayerH hLayer = NULL;
956 OGRFeatureH hFeature = NULL;
957 OGRGeometryH hGeom = NULL;
958 OGRFieldDefnH hFldDfn = NULL;
959 unsigned char *wkb = NULL;
962 int nFeatureCount = 0;
966 int iBandHasNodataValue =
FALSE;
967 double dBandNoData = 0.0;
970 GEOSGeometry *ggeom = NULL;
972 LWGEOM *lwgeomValid = NULL;
974 uint32_t bandNums[1] = {
nband};
975 int excludeNodataValues[1] = {exclude_nodata_value};
979 assert(NULL != pnElements);
990 rterror(
"rt_raster_gdal_polygonize: Error getting band %d from raster",
nband);
994 if (exclude_nodata_value) {
1004 if (iBandHasNodataValue)
1007 exclude_nodata_value =
FALSE;
1014 if (NULL == memdataset) {
1015 rterror(
"rt_raster_gdal_polygonize: Couldn't convert raster to GDAL MEM dataset");
1022 #ifdef GDAL_DCAP_RASTER
1034 ogr_drv = OGRGetDriverByName(
"Memory");
1035 memdatasource = OGR_Dr_CreateDataSource(ogr_drv,
"", NULL);
1036 if (NULL == memdatasource) {
1037 rterror(
"rt_raster_gdal_polygonize: Couldn't create a OGR Datasource to store pols");
1038 GDALClose(memdataset);
1039 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1044 if (!OGR_DS_TestCapability(memdatasource, ODsCCreateLayer)) {
1045 rterror(
"rt_raster_gdal_polygonize: MEM driver can't create new layers, aborting");
1048 GDALClose(memdataset);
1049 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1050 OGRReleaseDataSource(memdatasource);
1066 hLayer = OGR_DS_CreateLayer(memdatasource,
"PolygonizedLayer", NULL, wkbPolygon, NULL);
1068 if (NULL == hLayer) {
1069 rterror(
"rt_raster_gdal_polygonize: Couldn't create layer to store polygons");
1071 GDALClose(memdataset);
1072 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1073 OGRReleaseDataSource(memdatasource);
1083 hFldDfn = OGR_Fld_Create(
"PixelValue", OFTReal);
1086 if (OGR_L_CreateField(hLayer, hFldDfn,
TRUE) != OGRERR_NONE) {
1087 rtwarn(
"Couldn't create a field in OGR Layer. The polygons generated won't be able to store the pixel value");
1096 gdal_band = GDALGetRasterBand(memdataset, 1);
1097 if (NULL == gdal_band) {
1098 rterror(
"rt_raster_gdal_polygonize: Couldn't get GDAL band to polygonize");
1100 GDALClose(memdataset);
1101 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1102 OGR_Fld_Destroy(hFldDfn);
1103 OGR_DS_DeleteLayer(memdatasource, 0);
1104 OGRReleaseDataSource(memdatasource);
1110 cplerr = GDALFPolygonize(gdal_band, NULL, hLayer, iPixVal, NULL, NULL, NULL);
1112 if (cplerr != CE_None) {
1113 rterror(
"rt_raster_gdal_polygonize: Could not polygonize GDAL band");
1115 GDALClose(memdataset);
1116 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1117 OGR_Fld_Destroy(hFldDfn);
1118 OGR_DS_DeleteLayer(memdatasource, 0);
1119 OGRReleaseDataSource(memdatasource);
1130 if (iBandHasNodataValue) {
1131 size_t sz = 50 *
sizeof (char);
1132 pszQuery = (
char *)
rtalloc(sz);
1133 snprintf(pszQuery, sz,
"PixelValue != %f", dBandNoData );
1134 OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
1135 if (e != OGRERR_NONE) {
1136 rtwarn(
"Error filtering NODATA values for band. All values will be treated as data values");
1149 nFeatureCount = OGR_L_GetFeatureCount(hLayer,
TRUE);
1155 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for geomval set");
1157 GDALClose(memdataset);
1158 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1159 OGR_Fld_Destroy(hFldDfn);
1160 OGR_DS_DeleteLayer(memdatasource, 0);
1161 if (NULL != pszQuery)
1163 OGRReleaseDataSource(memdatasource);
1174 OGR_L_ResetReading(hLayer);
1176 for (j = 0; j < nFeatureCount; j++) {
1177 hFeature = OGR_L_GetNextFeature(hLayer);
1178 dValue = OGR_F_GetFieldAsDouble(hFeature, iPixVal);
1180 hGeom = OGR_F_GetGeometryRef(hFeature);
1181 wkbsize = OGR_G_WkbSize(hGeom);
1184 wkb =
rtalloc(
sizeof(
unsigned char) * wkbsize);
1186 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for WKB buffer");
1188 OGR_F_Destroy(hFeature);
1189 GDALClose(memdataset);
1190 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1191 OGR_Fld_Destroy(hFldDfn);
1192 OGR_DS_DeleteLayer(memdatasource, 0);
1193 if (NULL != pszQuery)
1195 OGRReleaseDataSource(memdatasource);
1201 OGR_G_ExportToWkb(hGeom, wkbNDR, wkb);
1205 if (!lwgeom)
rterror(
"%s: invalid wkb", __func__);
1207 #if POSTGIS_DEBUG_LEVEL > 3
1210 OGR_G_ExportToWkt(hGeom, &wkt);
1214 d_print_binary_hex(
"GDAL wkb", wkb, wkbsize);
1223 OGR_F_Destroy(hFeature);
1234 if (ggeom == NULL) {
1235 rtwarn(
"Cannot test geometry for validity");
1239 isValid = GEOSisValid(ggeom);
1241 GEOSGeom_destroy(ggeom);
1252 if (lwgeomValid == NULL) {
1253 rtwarn(
"Cannot fix invalid geometry");
1258 lwgeom = lwgeomValid;
1265 #if POSTGIS_DEBUG_LEVEL > 3
1271 size_t lwwkbsize = 0;
1274 d_print_binary_hex(
"LWGEOM wkb", lwwkb, lwwkbsize);
1281 pols[j].
val = dValue;
1284 *pnElements = nFeatureCount;
1287 GDALClose(memdataset);
1288 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1291 OGR_Fld_Destroy(hFldDfn);
1292 OGR_DS_DeleteLayer(memdatasource, 0);
1293 if (NULL != pszQuery)
rtdealloc(pszQuery);
1294 OGRReleaseDataSource(memdatasource);
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
void lwgeom_geos_error(const char *fmt,...)
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
void lwgeom_free(LWGEOM *geom)
#define LW_PARSER_CHECK_NONE
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
int rt_util_gdal_register_all(int force_register_all)
#define RASTER_DEBUGF(level, msg,...)
void rtinfo(const char *fmt,...)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
void rtwarn(const char *fmt,...)
struct rt_geomval_t * rt_geomval
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
void rtdealloc(void *mem)
GDALDatasetH rt_raster_to_gdal_mem(rt_raster raster, const char *srs, uint32_t *bandNums, int *excludeNodataValues, int count, GDALDriverH *rtn_drv, int *destroy_rtn_drv)
Return GDAL dataset using GDAL MEM driver from raster.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...