I have a raster GeoTIFF file with values from 0 to let's say 5. When I run gdal.polygonize (I'm working with JAVA and GDAL bindings to JAVA) on it, I get features with corresponding polygons.

The problem is that the value of the raster pixels are not passed anywhere and I lose the information about which polygon belongs to which value. Is there any way to store this information anywhere?

PS. Sorry if the question is not clear. This is my first experience with GIS world (and with advanced programming also to be honest :) )

  • From the gdal_polygonize.py manual page gdal.org/gdal_polygonize.html "Each polygon is created with an attribute indicating the pixel value of that polygon". The default field name seems to be "DN". Don't you see such an attribute in your polygons?
    – user30184
    Commented Jun 4, 2014 at 14:26
  • I thought so, but the field DN doesn't exist feat.GetFieldAsDouble("DN") gives: ERROR 1: No such field: 'DN'. Additionally the feat.GetFieldCount() gives 0. When I try to print the field with ID 0 I get consecutive numbers from 0 to the total number of polygons which were 'discovered'
    – matwod
    Commented Jun 4, 2014 at 14:40
  • I would recommend to run gdal_polygonize.py from Windows or Linux shell and check if the DN field gets created then. Next I would report about different behaviour from java bindings into gdal-dev mailing list.
    – user30184
    Commented Jun 4, 2014 at 14:48

1 Answer 1


Well it is a bit late but maybe there is someone else with this problem. There is the command gdalinfo . If you have pictures with only 1 band and a color palette you can use it to see which DN-code stands for wich RGB-Value. Here is an example of the code using the gdal java-bindings (gdal 1.11.1):

    public static void main(String[] args) {
        String[] src = { "C:\\Users\\User\\Desktop\\picture.png" };
        int[][] table = gdalInfo(src);


    public static int[][] gdalInfo(String[] args) {

        args = gdal.GeneralCmdLineProcessor(args);

        Dataset hDataset = gdal.Open(args[0], gdalconstConstants.GA_ReadOnly);

        Band hBand = hDataset.GetRasterBand(1);
        ColorTable hTable = hBand.GetRasterColorTable();
        int[][] colorCodes = new int[hTable.GetCount()][3];

        if (hBand.GetRasterColorInterpretation() == gdalconstConstants.GCI_PaletteIndex
                && (hTable != null)) {

            for (int count = 0; count < hTable.GetCount(); count++) {

                colorCodes[count][0] = hTable.GetColorEntry(count).getRed();
                colorCodes[count][1] = hTable.GetColorEntry(count).getGreen();
                colorCodes[count][2] = hTable.GetColorEntry(count).getBlue();


        return colorCodes;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.