views:

122

answers:

1

Question

What R package has a function that can determine if a coordinate is within a closed, complex spherical polygon (i.e., a point inside a polygon on Earth's surface)?

Related Links

Related R Packages

Thank you!

+3  A: 

I was given this solution:

library(sp)
library(geosphere)

pol <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
sp1 <- SpatialPolygons(list(Polygons(list(Polygon(pol)), 1)))
sp2 <- makePoly(pol, interval=100000, sp=T)

pt = data.frame(cbind(x=-111, y=-41))
coordinates(pt) = ~ x + y

# pt is (incorrectly) outside sp1
overlay(pt, sp1)
# but (correctly) inside (polygon #1)
overlay(pt, sp2)

plot(sp1)
plot(sp2, add=T, border='red')
Dave Jarvis