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;
975 int excludeNodataValues[1] = {exclude_nodata_value};
978 assert(NULL != raster);
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;
1013 memdataset =
rt_raster_to_gdal_mem(raster, NULL, bandNums, excludeNodataValues, 1, &gdal_drv, &destroy_gdal_drv);
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 pszQuery = (
char *)
rtalloc(50 *
sizeof (
char));
1132 sprintf(pszQuery,
"PixelValue != %f", dBandNoData );
1133 OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
1134 if (e != OGRERR_NONE) {
1135 rtwarn(
"Error filtering NODATA values for band. All values will be treated as data values");
1148 nFeatureCount = OGR_L_GetFeatureCount(hLayer,
TRUE);
1154 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for geomval set");
1156 GDALClose(memdataset);
1157 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1158 OGR_Fld_Destroy(hFldDfn);
1159 OGR_DS_DeleteLayer(memdatasource, 0);
1160 if (NULL != pszQuery)
1162 OGRReleaseDataSource(memdatasource);
1173 OGR_L_ResetReading(hLayer);
1175 for (j = 0; j < nFeatureCount; j++) {
1176 hFeature = OGR_L_GetNextFeature(hLayer);
1177 dValue = OGR_F_GetFieldAsDouble(hFeature, iPixVal);
1179 hGeom = OGR_F_GetGeometryRef(hFeature);
1180 wkbsize = OGR_G_WkbSize(hGeom);
1183 wkb =
rtalloc(
sizeof(
unsigned char) * wkbsize);
1185 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for WKB buffer");
1187 OGR_F_Destroy(hFeature);
1188 GDALClose(memdataset);
1189 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1190 OGR_Fld_Destroy(hFldDfn);
1191 OGR_DS_DeleteLayer(memdatasource, 0);
1192 if (NULL != pszQuery)
1194 OGRReleaseDataSource(memdatasource);
1200 OGR_G_ExportToWkb(hGeom, wkbNDR, wkb);
1205 #if POSTGIS_DEBUG_LEVEL > 3 1208 OGR_G_ExportToWkt(hGeom, &wkt);
1212 d_print_binary_hex(
"GDAL wkb", wkb, wkbsize);
1221 OGR_F_Destroy(hFeature);
1232 if (ggeom == NULL) {
1233 rtwarn(
"Cannot test geometry for validity");
1237 isValid = GEOSisValid(ggeom);
1239 GEOSGeom_destroy(ggeom);
1250 if (lwgeomValid == NULL) {
1251 rtwarn(
"Cannot fix invalid geometry");
1256 lwgeom = lwgeomValid;
1263 #if POSTGIS_DEBUG_LEVEL > 3 1269 size_t lwwkbsize = 0;
1272 d_print_binary_hex(
"LWGEOM wkb", lwwkb, lwwkbsize);
1279 pols[j].
val = dValue;
1282 *pnElements = nFeatureCount;
1285 GDALClose(memdataset);
1286 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1289 OGR_Fld_Destroy(hFldDfn);
1290 OGR_DS_DeleteLayer(memdatasource, 0);
1291 if (NULL != pszQuery)
rtdealloc(pszQuery);
1292 OGRReleaseDataSource(memdatasource);
int rt_util_gdal_register_all(int force_register_all)
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
void lwgeom_free(LWGEOM *geom)
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.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
#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.
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
void lwgeom_geos_error(const char *fmt,...)
void rtwarn(const char *fmt,...)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
void rtinfo(const char *fmt,...)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
#define RASTER_DEBUGF(level, msg,...)
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
struct rt_geomval_t * rt_geomval
void rtdealloc(void *mem)
#define RASTER_DEBUG(level, msg)
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
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...