More

How to batch-process attribute tables in QGIS (or GRASS)?

How to batch-process attribute tables in QGIS (or GRASS)?


I'm using GRASS 6.4.3 in Linuxmint 16. I have a bash script for GRASS which I use to process almost 300 raster files. The script first imports some raster files (r.in.gdal), then does some calculations and classification (usingr.mapcalcandr.reclass). And finally transforms the resultant raster to vector (r.to.vect) and saves it (v.out.ogr, usingtype=area).

Let's say each vector files has a field labeled category and that it takes only two values 1 and 2. As I'm interested only in the total area of polygons with category = 2 and with an area higher than 10,000 m2, I open each vector file in QGIS, edit its attribute table, calculate the area of each polygon and eliminate all of them with category=1 and $area<10,000.

But I have to do it manually,

Is there a way to do it in a batch process within QGIS?

Or even better, is it possible to do it in GRASS?

Is it possible to edit the attribute table within the command console in GRASS?

Could I do it in the same bash script which I use to do all the other processes in GRASS?

I've been reading this but I'm not sure how to apply it to my problem. I couldn't find how to calculate the polygons area and add it to a new column using GRASS. I think I could usedb.selectto search for the polygons I want to eliminate, but I don't know how to do it.

Thanks for your help!


Well, you can do that using ogr2ogr, after exporting vectors from GRASS to shp:

ogr2ogr -sql "SELECT * FROM file_in WHERE 'category' == 2 AND OGR_GEOM_AREA > 10000" file_out.shp file_in.shp

file_in.shp is your data exported from GRASS

file_out.shp is your filtered results.

Then you can run:

ogr2ogr -sql "SELECT count(OGR_GEOM_AREA) FROM file_out" file_out.shp

Or if you don't need filtered results, just place WHERE argument:

ogr2ogr -sql "SELECT count(OGR_GEOM_AREA) FROM file_in WHERE 'category' == 2 AND OGR_GEOM_AREA > 10000" file_in.shp

Hope it helps.


Watch the video: How to add PostGIS layer in QGIS