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 (using r.mapcalc and r.reclass). And finally transforms the resultant raster to vector (r.to.vect) and saves it (v.out.ogr, using type=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 use db.select to search for the polygons I want to eliminate, but I don't know how to do it.

Thanks for your help!

  • If your products are classification rasters, you can parse r.report and r.describe to obtain the desired results. No need to create a vector
    – nickves
    Jul 28, 2014 at 14:34
  • Thanks nickves, I've been r.report and r.describe, both very useful but, in this case, I need to do some edition to the vector after the whole process.
    – Lucas
    Jul 28, 2014 at 17:44

1 Answer 1


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.

  • Thanks jzol! I didn't know ogr2ogr and is exactly what I needed.
    – Lucas
    Jul 28, 2014 at 17:47
  • @Lucas please upvote helpful answers and accept the answer which solves your problem. That way, the system automatically marks your question as answered
    – underdark
    Jul 28, 2014 at 17: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.