<div>Hello,</div><div><br></div><div>I've created a WKTRasterBand-to-GDALRasterBand conversor, in the frame of WKT Raster project. I'm not sure if the results are correct. You can get the code from here <a href="https://svn.osgeo.org/postgis/spike/wktraster/rt_core/rt_api.c">https://svn.osgeo.org/postgis/spike/wktraster/rt_core/rt_api.c</a>, but basically, the steps are:</div>

<div><br></div><div>1.- Register GDAL/OGR drivers</div><div><br></div><div>2.- Create empty OGR in-memory Datasource: OGR_Dr_CreateDataSource(OGRGetDriverByName("Memory"),"", NULL);</div><div><br></div>

<div>3.- Create empty GDAL Dataset, taking width and height from WKT Raster Band ("band" variable): GDALCreate(GDALGetDriverByName("MEM"), "",rt_band_get_width(ctx, band), rt_band_get_height(ctx, band), 0, GDT_Byte, NULL);</div>

<div><br></div><div>4.- Add default geotransform to GDAL empty dataset: {0.0, 1.0, 0.0, 0.0, 0.0,1.0 }; (I can't get geotransform from WKT Raster Band)</div><div><br></div><div>5.- Add GDAL band, getting data from WKT Raster Band: </div>

<div><br></div><div> sprintf(szDataPointer, "DATAPOINTER=%p", rt_band_get_data(ctx, band));</div><div><div> apszOptions[0] = szDataPointer;</div><div> GDALAddBand(memdataset, nPixelType, apszOptions)</div><div>
<br>
</div><div>(pixel type got from WKT Raster Band too)</div><div><br></div><div>6.- Create empty OGR layer, to store polygonized raster: OGR_DS_CreateLayer(memdatasource, "Polygonized layer", NULL,</div><div>        wkbPolygon, NULL);</div>

</div><div><br></div><div>7.- Get GDAL RasterBand from GDAL Dataset, and polygonize it</div><div><br></div><div> gdal_band = GDALGetRasterBand(memdataset, 1);</div><div> GDALPolygonize(gdal_band, NULL, hLayer, -1, NULL, NULL, NULL);</div>

<div><br></div><div>8.- Get all the features from layer and export them to WKT format:</div><div><br></div><div><div> for(j = 0; j < nFeatureCount; j++)</div><div> {</div><div>        hFeature = OGR_L_GetFeature(hLayer, j);</div>

<div>        hGeom = OGR_F_GetGeometryRef(hFeature);</div><div>        OGR_G_ExportToWkt(hGeom, &pszSrcText);</div><div> }</div><div><br></div><div>So, at the end, pszSrcText has each of the WKT polygons from original raster. My problems are:</div>

<div><br></div><div>1.- I'm testing this code with really simple raster, and I'm confused. For example:</div><div>* 2x2 32 bits signed int raster, with "1" in all cells. The result: POLYGON((0 0, 0 2, 2 2, 2 0, 0 0)). I was expecting POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))</div>

<div>* the raster from <a href="http://trac.osgeo.org/postgis/attachment/wiki/WKTRaster/SpecificationWorking01/WKTRasterEnvelopeConvexHullAndShape.gif">http://trac.osgeo.org/postgis/attachment/wiki/WKTRaster/SpecificationWorking01/WKTRasterEnvelopeConvexHullAndShape.gif</a>. The result: </div>

<div>POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))</div><div>POLYGON ((3 3,3 6,6 6,6 3,3 3))</div><div>POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))</div>

<div>POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))</div><div><br></div><div>Something strange here? Am I doing things right?</div></div><div><br></div>

2.- I'd like to store the pixel value in each polygon. Is a good idea to create a field in the layer to store pixel value? <div><br></div><div>3.- From a GDAL RasterBand, I can fetch the owning dataset handle (GDALGetBandDataset).I think it could be useful to get the WKT Raster handle from a WKT RasterBand too... Am I right?<br>

<div><br></div><div>Sorry, I know this post is really long. Thanks for reading it, anyway :-)</div><div><br></div><div>Best regards,</div><div>Jorge</div><div><br></div><div><a href="http://www.gis4free.org/blog">http://www.gis4free.org/blog</a><br>


</div></div>