问题
I'm trying to extract points by a polygon using the 'sp' package function 'over'
library(sp)
library(rgeos)
#my polygon plgn (many polygon features in one)
plot(plgn)
proj4string(plgn) = CRS("+proj=utm +zone=46 +datum=WGS84 +units=m +no_defs")
#giving spatial reference to point data d
coordinates(d) <- ~X+Y
proj4string(d) = CRS("+proj=utm +zone=46 +datum=WGS84 +units=m +no_defs")
#USE overlay (there are many NAs)
overlay=d[!is.na(over(d, plgn)),]
Unfortunately, I'm getting an ERROR
Error in d[!is.na(over(d, plgn)), ] :
matrix argument not supported in SpatialPointsDataFrame selection
Any idea?? Is it because my polygon contains 100s of features?
回答1:
Your plgn
is a SpatialPolygonsDataFrame
, and as such, is.na(over(d, plgn))
returns a logical matrix. This cannot be used to subset your SpatialPoints*
. You can do the following to convert the logical matrix to a vector that the subsetting operation can accommodate:
d[complete.cases(over(d, plgn)), ]
来源:https://stackoverflow.com/questions/21567028/extracting-points-with-polygon-in-r