I have the following puzzle regarding spatial data in R:
I have a dataset with street segments (with their respective starting and ending coordinates). I want to create a buffer of X meters around these points and then check whether a list of lat/lon points is within that buffer. Is there a way to do this in R?
I'm able to map the points and map the buffers using a combination of various packages: maptools, ggmap, rgdal, sp, and rgeos. But this procedure seems to only map the points and the buffers without allowing me to then check if the other coordinates I have are within the buffers. Ideally, I'd like to produce a vector of 1s and 0s describing whether the list of lat/lon points are within the buffer around the street segments.
Any ideas?
This is the code I've been using, but I get all missing values (and I know this shouldn't be the case). I've also tried using the gContains function from rgeos but it crashed R.
#Load shapefile in R and transform to appropriate CRS
shp <- readOGR(dsn="/Users/Maps/shapes", layer="shp")
shp_transf <- spTransform(shp_transf, CRS( "+init=epsg:21897" ))
#Create buffer around polygons
shp_buff <- gBuffer(shp_transf, width=40, byid=TRUE, quadsegs=10)
#Make my dataframe of lat/lon points into same projection as buffers
points <- SpatialPoints(points,proj4string=CRS(proj4string(shp_buff)))
#Use over function from SP pacakge
result <- as.integer(over(points, shp_buff)$OBJECTID)
result <- over(points, shp_buff)
? – Jacob Fpoints
object, are the coordinates in the data frame already in the epsg:21897 CRS? The wording of the comment above that line kind of sounds like you are trying to transform them. Also, what happens when you runplot(shp_buff)
thenplot(points, add=T)
? – Jacob Fpoints
vector are in the same projection as the shapefileshp
. When runningplot(shp_buff)
it creates a plot of the buffers but runningplot(points, add=T)
creates a null value. – op4