I've been going through a fairly lengthy process of polygonizing very intricate raster images, then creating a new layer with the extra area attribute, so that I can then remove the millions of tiny dots that turn what should be a 5 megabyte shapefile into a 40 megabyte one.

When I run the Raster|Conversion|Polygonize with qgis, it shows the gdal script that is run in the command line to complete the task. I don't know many gdal commands other than ogr2ogr stuff, so was wondering if any of you gdal pro's out there knew how to add an area attribute to the outputted shapefile (populated with calculated areas) without a second step by adding something to this script:

gdal_polygonize.py "/Users/douglas/Desktop/polygonized_rasters/zones.tif"

I've clicked a couple of options in this polygonize menu and saw code added to the script, so that gave me the idea that there might be some code I could add to this line to add that area column during the same process. Maybe not, I'm not sure. Thanks though anyway


As far as I know, it's not possible in the same step, without adapting the code of gdal_polygonize.py for this purpose. However, we can do with a bit of OGR SQL. For instance:

ogr2ogr output.shp input.shp -sql "SELECT *, OGR_GEOM_AREA AS area FROM input"

creates a new shapefile containing all fields of the input one, plus an area field.

  • Following your suggestion I get the message "ERROR 1: SELECT from table input failed, no such table/featureclass." gdal version 1.9 -- is not OGR_GEOM_AREA contained from gdal_polygonize? Any advice? – Max Mar 4 '14 at 12:01
  • Substitute input in the SQL statement with your input layer name (without the extension). Hope this helps. – Antonio Falciano Mar 4 '14 at 14:02
  • thanks, of course, that was my mistake! "input" is the layer name of "input.shp" -- now it works – Max Mar 5 '14 at 5:58

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.