Skip to content
Prev 10380 / 29559 Next

problem to use "perimeter" in RASTER Library

Dear Gianni,

perimeter is a function in the geosphere package.

You found a bug in the test whether this is a closed polygon.  (It
should be if  (isTRUE(all.equal(x[1, ], x[nrow(x), ])))  rather than
if (all.equal(x[1, ], x[nrow(x), ]))  ). Thanks for reporting it, it
is fixed in version 1.2-16.

The easy work-around would be

xy <- rbind(c(579471.4,6757500),c(581081.3,6757500),c(579471.4,6761301),c(581081.3,6761301))
xy <- rbind(xy, xy[1,])

perimeter(xy)

But then you get another error, with these data, because these are not
longitude/latitude coordinates. geosphere only does spherical
(lon/lat) computations, not computations with (the easier case of)
planar coordinates .

With these coordinates you can compute the perimeter like this:

library(raster)
x <- rbind(c(579471.4,6757500),c(581081.3,6757500),c(579471.4,6761301),c(581081.3,6761301))
y <- rbind(x[-1,], x[1,])
d <- pointDistance(x, y, lonlat=FALSE)
perimeter <- sum(d)

Robert