2

I have a raster which I have successfully polygonised using: raster / conversion / polygonise (raster to vector).

However, my raster has multiple fields (data columns) within it and I've only been able to convert the first of these into my vector. I've done some online forum rummaging but can't find an answer.

What settings are needed to retain all data?

I presume it's something to do with the field name option, but can't figure it out.

I've added a picture to show what I mean about the different bands. The info shown is linked to one of the squares, I feel there should be a way to retain this in conversion. enter image description here

  • From the GDAL Polygonize Documentation, it seems to be not possible with just one step. However, there would be workarounds using "Polygonize" in batch mode and later merging the different polygon sets back together. This could also be done using QGIS Graphical Modeler – s6hebern Feb 22 '18 at 14:29
  • what do you mean by "multiple columns" ? Is it a multiple bands raster ? – radouxju Feb 23 '18 at 7:08
  • reply to radouxju. I'm a very happy shapefile editing, but I'm not very familiar with using rasters. I don't know what type of ratser it is, just that it's a raster. It seems it has 10 attributes feeding the raster. shp can have different attribute fields, each working like the heading in the attribute table. You can also see these when you select a polygon using the info tool. when I select the raster using the info tool it appears that it has 10 attribute fields When I polygonise the raster I can keep 1 attribute field, but I would like to retain all of them. Thanks for you time. – Louise Feb 23 '18 at 15:18
  • Reply to s6hebern. Ah; it seems I'm just a bit too basic of a QGIS user. I use the built in functions and plugins, but not GDAL or GRASS. I'll see if I can learn, thanks for the link to the documentation. Likewise for I'll try for QGIS graphical Modeler. – Louise Feb 23 '18 at 15:24
1

Here is a PyQGIS solution to save each band from a raster in a separate .TIF file and polygonize the results. To run it, all you need to do is copy paste the code into the QGIS Python Console and change the in_path and out_path variables:

import gdal,ogr, osr

in_path = 'C:\\Test\\Multiband Image.tif'
out_path = 'C:\\Test\\'

ds = gdal.Open(in_path)
ds_proj=ds.GetProjection()

raster_proj = osr.SpatialReference()
raster_proj.ImportFromWkt(ds_proj)

for i in range (1,ds.RasterCount+1): ##Begin at band 1 through to to the last one
    #Save each band individually
    srcband = ds.GetRasterBand(i)
    out_ds = gdal.Translate(out_path + 'band' + str(i) + '.tiff', ds, format='GTiff', bandList=[i])
    out_ds=None

    #Prepare shapefile
    outShapefile = "C:\\Test\\polygonized"
    driver = ogr.GetDriverByName("ESRI Shapefile")
    outDatasource = driver.CreateDataSource(outShapefile+ str(i) + ".shp")
    outLayer = outDatasource.CreateLayer("polygonized", srs=raster_proj)
    #Add the DN field 
    newField = ogr.FieldDefn('DN', ogr.OFTInteger)
    outLayer.CreateField(newField)
    gdal.Polygonize(srcband, None, outLayer, 0, [], callback=None )
    outDatasource.Destroy()
    sourceRaster = None

print "Done"

Depending on the number of bands and your data size, it could take a couple minutes. Moreover, I left out the merging of the vector layers, but you can merge them easily via the Merge algorithm in the Processing Toolbox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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