Extracting points with polygon in R

做~自己de王妃 提交于 2019-11-30 09:21:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!