[gdal-dev] Polygonize: are these steps the right ones?

Jorge Arevalo jorgearevalo at gis4free.org
Thu Mar 11 18:59:40 EST 2010


Hi,

I'm using GDALPolygonize to polygonize a dataset with one band created
using MEM driver:

memdataset = GDALCreate(GDALGetDriverByName("MEM"),"", width, height,
0, GDT_Byte, NULL);
GDALAddBand(memdataset, nPixelType, apszOptions); // in apszOptions[0]
there are the band data

Then, I create a MEM datasource, to store the polygons. Using MEM
driver for OGR:

memdatasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("Memory"), "", NULL);

Of course, I create a Layer to store the pols:
hLayer = OGR_DS_CreateLayer(memdatasource, "Polygonized layer", NULL,
wkbPolygon, NULL);

As I can read in the source code of GDALPolygonize function
(http://trac.osgeo.org/gdal/browser/trunk/gdal/alg/polygonize.cpp), a
new Feature is created, based on the Feature Definition of the layer.

hFeat = OGR_F_Create( OGR_L_GetLayerDefn( hOutLayer ) );

Then, the field of the Feature with index "iPixValue" is set with a
pixel value (the pixel value of all the raster cells with the same
value, polygonized in the same geometry, really)

 if( iPixValField >= 0 )
       OGR_F_SetFieldInteger( hFeat, iPixValField, poRPoly->nPolyValue
); //poRPoly->nPolyValue is the pixel value

The problem is that the MEM-based layer doesn't have fields. So, if I
use "0" as iPixValField, I get an error, because there's no field with
index 0 in the Feature. Of course, the same result if I use any value
> 0.

I thought the solution was to "modify" the Layer's Feature Definition
to allow fields in the Features created. So, I created a new Feature
Defintion, with a Field Definition of one field, of type OFTReal.
Then, I created a new Feature following this schema, and I added this
Feature to the Layer. I know, it's not the best way, and it didn't
work. But I don't know how to "force" the MEM-based layer to allow
fields in its features. Is it possible? If not, how could I polygonize
a mem-based raster?

Thanks in advance, and best regards,
Jorge


More information about the gdal-dev mailing list