[R] Can I calculate the area of a polygon?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Sun Dec 19 14:40:32 CET 2004

On 19-Dec-04 Andreas Kretschmer,,, wrote:
> Hello,
> I'm verry new in R.


> My Problem:
> I have a PostgreSQL-Server with installed support for the R-Language.
> And now I have a table with a Polygon like this:
> test_db=# select * from geo where id=1;
>  id |             koerper
> ----+---------------------------------
>   1 | ((0,0),(0,2),(2,2),(3,3),(5,0))
> (1 row)
> Now, I need to know the area of this object. Please, can you tell me,
> if it possible to use the installed plr - Language zu calculate the
> area?
> I have some experience to draw diagramms from Postgres via the
> R-language, but I'm a absolute novice to use R to solve problems like
> this.

Not sure what you mean by "the installed plr - Language", but for
calculating areas og polygons you may find the following function
Example based on the coordinates ((0,0),(0,2),(2,2),(3,3),(5,0))
you give above.

Let X be the matrix of the coordinates:

  > X
       [,1] [,2]
  [1,]    0    0
  [2,]    0    2
  [3,]    2    2
  [4,]    3    3
  [5,]    5    0

Function "area":

    x<-X[,1]; y<-X[,2]; lx<-length(x)

  > area(X)
  [1] -9.5

Note that this function generates a "signed area": positive
if the bounding contour is "counter-clockwise" in the order
of the vertices as given in X, negative if clockwise.

The principle of the function is that it first closes the
polygon by repeating the first point, and then (in effect)
calculates the contour integral

  Int -y*dx

round the contour of the polygon (equivalent to Int x*dy).

Hoping this helps,

E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 19-Dec-04                                       Time: 13:40:32
------------------------------ XFMail ------------------------------

More information about the R-help mailing list