[GRASS-user] Vector attribute 'algebra' - calculating difference between areas of polygons
Moritz Lennert
mlennert at club.worldonline.be
Wed May 17 08:47:01 PDT 2017
On 17/05/17 17:32, James Duffy wrote:
> Hello list,
>
> I have two vectors (A and B), both of which are polygons of different
> sizes. They overlap each other, but not fully. I wish to calculate the
> area of each polygon (in meters) and perform the following:
>
> (A - B) / A
>
> Currently I think I can do this with the following:
>
> 1) Create a difference polygon with `v.overlay`.
> 2) Calculate the area of the difference polygon with `v.to.db`.
> 3) Calculate the area of polygon A with `v.to.db`.
> 4) Access attribute tables of both polygons to perform the calculation
> with ???
>
> I have two questions:
>
> 1) Is there a more efficient way to do this? Is there some sort of
> vector algebra available in GRASS.
> 2) Do the area values have to be stored in an attribute table? Can they
> be written to a text file alongside an ID of the vector?
First of all the approach depends a bit on whether one polygon in A
always corresponds to one polygon in B, or whether you might have
several polygons in one map corresponding to one polygon in the other.
If its one-to-one, and you know which polygon id in A corresponds to
which polygon id in B, you can simply use v.to.db with '-p' to print the
results to standard output which you can redirect to file, e.g.
v.to.db -p A op=area sep=comma > areaA.csv
You can also load the areas to the respective attribute tables and then
use v.db.join to join the area column of one to the table of the other,
if you have a common id for corresponding polygons in each map.
In that case, you could also directly interact with the database in SQL
using db.select, e.g. something like this:
db.select sql="SELECT A.id, (A.area-B.area)/A.area FROM A JOIN B ON
A.id=B.id"
Moritz
More information about the grass-user
mailing list