# [R-sig-Geo] Calculating area of polygons

Roger Bivand Roger.Bivand at nhh.no
Thu May 14 09:51:31 CEST 2009

On Thu, 14 May 2009, Wesley Roberts wrote:

> Dear list,
>
> I would like to calculate the area of a polygon. I have found areapl() in splancs but am having trouble using it.
>
> x<-1
> poly <- readShapePoly("a1_l4_aerial_ws.shp", IDvar="cat", proj4string=CRS("+proj=tmerc +south +ellips=WGS84 +datum=WGS84 +lon_o=31 +k_0=1 +units=m +no_defs"))
> Error in `[.data.frame`(x at data, i, j, ..., drop = FALSE) :
>  undefined columns selected

The areas are calculated when the Polygon objects are created - they are
used internally for ordering the painting of the polygons, largest first):

library(rgdal)
scot_BNG <- readOGR(system.file("vectors", package = "rgdal")[1],
"scot_BNG")
summary(scot_BNG)
getClass("Polygon")
areas <- sapply(slot(scot_BNG, "polygons"), function(x) sapply(slot(x,
"Polygons"), slot, "area"))
str(areas)

areas is a list - each member is the vector of areas for rings belonging
to the Polygons objects. You may also need to access the "hole" slot to
see which "are" seen as holes for your data.

The areas are OK for planar coordinates, but only for these - for plotting
the values, although not correct, are adequate for plot ordering.

Hope this helps,

Roger

>
> I have tried various combinations of commands with all returning either an error or 0. I know the area of the polygon is 4.760465 square meters (area slot) but cant seem to replicate this using areapl(). Is it possible to access the area slot directly instead of computing the area with areapl()?
>
> Many thanks,
> Wesley
>
>
> Wesley Roberts MSc.
> Researcher: Earth Observation (Ecosystems)
> Natural Resources and the Environment
> CSIR
> Tel: +27 (21) 888-2490
> Fax: +27 (21) 888-2693
>
> - Chinese proverb
>
>
>
>
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of