bstockton
bstockton

Reputation: 577

Using gContains to test whether lat lon points lie within a map

I have a map of an area obtained from the gmap function in the dismo package. I am then trying to test whether certain latitude and longitude points are within that map.

Here is my reproducible example:

library(dismo)
library(sp)
library(rgdal)
library(XML)
library(rgeos)


spatP <- SpatialPoints(gmap("Palo Alto, CA", lonlat = TRUE), 
    proj4string = CRS("+proj=longlat"))

point <- data.frame( x =  -122.129610, y = 37.399761)
pointSp <- SpatialPoints(point, proj4string = CRS("+proj=longlat"))

gContains(spatP, pointSp, byid = FALSE)

That point is within that map, but I am obviously missing something.

Upvotes: 2

Views: 1264

Answers (1)

bstockton
bstockton

Reputation: 577

Figured out a solution. If I turn the SpatialPoints data frame spatP into a convex polygon, it works.

polyHull <- gConvexHull(spatP)
gContains(polyHull, pointSp)
[1] TRUE 

Upvotes: 1

Related Questions