Calculating polygon area with R geosphere package

1.8k views Asked by At

I'm having trouble with the areaPolygon function in R. Sometimes it appears to produce the correct result, and sometimes the results seem orders of magnitude off.

E.g. I have a polygon with these points:

lng <- c(-51.74768, -51.74768, -51.74735, -51.74735)
lat <- c(-0.1838690, -0.1840993, -0.1840984, -0.1838682)

and combine them into a dataframe

data <- data.frame(lng, lat)

Then I try to get the area:

area <- geosphere::areaPolygon(x = data)

which comes out to 326928.8 m^2.

I was expecting something more in the 1000 m^2 range.

What's weird is that this seems to work fairly well for about half the cells I'm calculating, but not the other half.

A similar StackOverflow question noted strangely small values, due to the points being in an incorrect order. However, when I plot, e.g.

plot(data, type="l")

The polygon seems to be drawing correctly.

Anyone know what might be wrong here? Thanks!

1

There are 1 answers

0
Robert Hijmans On

With your example data, result is as expected, as noted in the comments.

library(geosphere)
lng <- c(-51.74768, -51.74768, -51.74735, -51.74735)
lat <- c(-0.1838690, -0.1840993, -0.1840984, -0.1838682)
d <- cbind(lng, lat)
geosphere::areaPolygon(d)
#[1] 935.2693

The idea that rounding plays a role seems far-fetched.