简体   繁体   English

在R中用多边形提取点

[英]Extracting points with polygon in R

I'm trying to extract points by a polygon using the 'sp' package function 'over' 我正在尝试使用'sp'包函数'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? 是因为我的多边形包含100个特征?

Your plgn is a SpatialPolygonsDataFrame , and as such, is.na(over(d, plgn)) returns a logical matrix. 您的plgnSpatialPolygonsDataFrame ,因此, is.na(over(d, plgn))返回逻辑矩阵。 This cannot be used to subset your SpatialPoints* . 这不能用于子集您的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)), ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM