Monday 20 June 2016

attribute table - Is there any way to add an area column to new shapefile created from raster with gdal's polygonize in the same step?


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.



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...