[gdal-dev] Re: reg GDALPolygonize

Srikanth srikanth1947 at gmail.com
Wed Nov 18 06:53:21 EST 2009


Thanks for the mail Alan.

I am programming it in Cpp and using Qt architechture . I will try with a
new raster band and try to make it as a mask with forecfully assiging a
value to it.




 data = (uchar *) CPLMalloc(sizeof(uchar)*width*height*bytesPerPixel);



poBandR->RasterIO(GF_Read, xOff, yOff, width, height, data, width, height,
(GDALDataType)dataType,bytesPerPixel, 0);



for(int k = 0 ; k < width*height; k++)

{

     if(((uchar*)data)[k] != 255)

       ((uchar*)data)[k] = 0 ;

        else

       ((uchar*)data)[k] = 1 ;



poBandR->RasterIO(GF_Write, xOff, yOff, width, height, data, width, height,
(GDALDataType)dataType,bytesPerPixel, 0);



if(data != NULL)

{

delete data ;

data = NULL ;

}



const char *pszDriverName = "ESRI Shapefile";



OGRSFDriver *poDriver;



poDriver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);



OGRDataSource* ds = poDriver->CreateDataSource("Shape", NULL );



OGRLayer* poLayer = ds->CreateLayer("myfile",NULL,wkbMultiPolygon,NULL);



CPLErr er = GDALPolygonize(poBandR , NULL , poLayer ,1,NULL,NULL,NULL);

Regards,

Srikanth
On Wed, Nov 18, 2009 at 4:35 PM, Alan Steel <alan.steel at ext.jrc.ec.europa.eu
> wrote:

> Hi Srikanth,
>
> I have used gdal_polygonize.py, which is based on GDALpolygonize(). I think
> you know the algorithm better than I do, as you see that the 2nd argument is
> the mask band, which masks output values that are zero in the mask band.
>
> Ok, that doesn't answer your question, and I don't know what is available
> in GDAL to write custom masks. I only write to suggest that you try learning
> a bit of C (or other programming language), which will alow you to write
> simple routines quickly.
> It does take time to learn, but can be very rewarding - and you can also
> eventually incorporate GDAL libraries into your programs. Not a short-tem
> solution, I know, but good in the long term. A C compiler comes with linux
> (gcc) and there are free IDEs that you can install on any platform.
>
> The following is a loop that would (as part of a C program) write an output
> raster file that masks input pixel integer values with a value of 25 as 0.
>
>        for(b = 0; b < bands; b++){               for(i = 0; i < rows; i++){
>               //read a row of data from input file
>              fread(dataRow, sizeof(int), cols, fp_in1);
>              for(d=dataRow, o=dataRowOut; d < dataRow+cols; d++, o++){
>                  if(*d == 25)
>                      *o = 0;
>              }
>              fwrite(dataRowOut, sizeof(int), cols, fp_out);
>           }                              }
>
> Regards,
>
> Alan
>
>
> Srikanth wrote:
>
>> Hi ,
>>  I have found the solution myself.
>> Below is the piece of code for polygonizing the raster.
>>  Yet there is another problem i am facing. I am unable to create a mask
>> band for a particular pixel value.I understand that could be done using
>> thresholding. But I did not find any method for that in GDAL.
>>  Any help/suggestions are appreciated.
>>
>> GDALAllRegister();
>>
>> OGRRegisterAll();
>>
>> String pszFilename = "C:\\Images\\s3dImages\\geo_liss.tif";
>>
>> poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
>>
>> GDALRasterBand *poBandR,*poBandMask ;
>>
>> poBandR = poDataset->GetRasterBand(1);
>>
>> poBandR->CreateMaskBand(GMF_PER_DATASET);
>>
>> poBandMask = poBandR->GetMaskBand() ;
>>
>> poBandMask->Fill(10,0);
>>
>> const char *pszDriverName = "ESRI Shapefile";
>>
>> OGRSFDriver *poDriver;
>>
>> poDriver =
>> OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
>>
>> OGRDataSource* ds = poDriver->CreateDataSource("Shape", NULL );
>>
>> OGRLayer* poLayer = ds->CreateLayer("myfile",NULL,wkbMultiPolygon,NULL);
>>
>> CPLErr er = GDALPolygonize(poBandR , poBandMask , poLayer
>> ,1,NULL,NULL,NULL);
>>
>> poLayer->SyncToDisk();
>>
>> OGRDataSource::DestroyDataSource(ds);
>>
>>
>>
>
>


-- 
Regards,
Srikanth K.
www.servetheneedy.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20091118/2484d309/attachment-0001.html


More information about the gdal-dev mailing list