i have a tif file which I polygonized using gdal.Polygonize() method. As a result I got a vector layer with millions of polygons and values from 1 to 255 in the attribute table. Although I don't mind such huge number of polygons I would like to merge those which have the same values in the attribute table. Could you tell me if there is a way to do it programmatically using any GDAL/OGR tools?

I would like to merge such values

  • The gdal_polygonize manual page informs: This utility creates vector polygons for all connected regions of pixels in the raster sharing a common pixel value. So what you have now are not-connected polygons (or maybe connected at corners). If you union them you will get multipolygons which may be huge with very many members. Is that what you want? – user30184 May 29 '17 at 9:55
  • Yes, I suppose so. I'll try to union them now, thanks for the hint. – NecroSith May 29 '17 at 10:18

You're looking for a "Dissolve polygons" tool. QGIS by default offers two implementations, one as part of its internal toolset (QGIS geoalgorithms --> Vector Geometry Tools --> Dissolve ) and one part of the GDAL/OGR toolset (GDAL/OGR --> OGR Geoprocessing --> Dissolve Polygons ). There's one more in the SAGA toolset as well.

All of them differ slightly in the options they offer, but the GDAL/OGR tool is the most "verbose", allowing a lot of customization. So I'd recommend that for most cases.

If you want to use any of these programmatically, I'd recommend using processing.runalg() to run them properly in your script.

  • Hm, I found "gdalogr:merge" tool to run with processing.runlag() but it seems to be working only to merge rasters. Is it applicable to vectors or I am looking in the wrong direction? – NecroSith May 29 '17 at 11:38
  • 1
    If you can't see the toolbox, make sure you have that panel enabled (right-click anywhere, tick 'Toolbox'). If you don't know the algorithm name, run the process once from the toolbox. Then go to Processing->History (in the main toolbar at the top) and you can see the full pyqgis call to that algorithm, including all the parameters(!) . The tool I mean is called gdalogr:dissolvepolygons – Senshi May 29 '17 at 11:53
  • In addition, check out gis.stackexchange.com/a/219036/29005 for how to easily query processing algorithms from a python script/console. – Senshi May 29 '17 at 12:32
  • Yes, I believe it's just what I need. I found the tool you were talking about and manage to use this algorithm in the script. Thank you very much. – NecroSith May 30 '17 at 6:09

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.