简体   繁体   English

R 中多边形内点(shapefile)的选择和提取

[英]Selection and extraction of points (shapefile) within a polygon in R

I have two shape-files;我有两个形状文件; one is a point file (some information for world) and the other is a 21-countries shape-file.一个是点文件(世界的一些信息),另一个是 21 个国家的形状文件。

I need to extract the points which falls with in a country.我需要提取一个国家的分数。 I have to repeat this step in 21 times in QGIS or ArcGIS.我必须在 QGIS 或 ArcGIS 中重复此步骤 21 次。

Is there any way to do this in R and if is in batch processing, that would be really great as I have to repeat this for other 4 data sets as well.在 R 中是否有任何方法可以做到这一点,如果是在批处理中,那将非常棒,因为我也必须对其他 4 个数据集重复此操作。 Thanks in advance提前致谢

So as an example how to use over() consider the following simulated data.因此,作为如何使用over()的示例,请考虑以下模拟数据。

library(raster)
library(sp)

#generate point distribution | with (x,y) coordinates
set.seed(42);pts <- SpatialPoints(data.frame(x = runif(30, 1, 5), y = runif(30, 1, 5)))

#create polygons
df<-data.frame(X = c(rep(1,5),rep(2,5),rep(3,5),rep(4,5),rep(5,5)),
               Y = c(rep(seq(1,5,1),5)))
df$cell<-1:nrow(df) #polygon identifier (for later)

#make into spatial object (probably better way to do this)
coordinates(df) <- ~X+Y 
rast <- raster(extent(df), ncol = 5, nrow = 5)
grid<-rasterize(df, rast, df$cell, FUN = max)
grid<-rasterToPolygons(grid) #create polygons

#plot to check
plot(grid); points(pts)

#and extract
pointsinpolygon = over(SpatialPolygons(grid@polygons), SpatialPoints(pts))

The last bit you can also do as最后一点你也可以这样做

df$pointsinpolygon <- over(SpatialPolygons(grid@polygons), SpatialPoints(pts))

to write the results directly to the dataframe.将结果直接写入 dataframe。

Here is an example polygons shapefile and how you can read it into R.这是一个示例多边形 shapefile 以及如何将其读入 R。

library(raster)
# example polygons filename
f <- system.file("external/lux.shp", package="raster")
pol <- shapefile(f)

I do not have a matching points file, so I generate some points for this example我没有匹配点文件,所以我为此示例生成了一些点

pts <- spsample(pol, 5, type="regular")
crs(pts) <- crs(pol)

To see what area the points fall in you can use over or extract .要查看点落在哪个区域,您可以使用overextract They both return the values in the order of the points, but extract also adds the (sequential) "point id" --- this is primarily for when you have overlapping polygons, such that a point might fall in two polygons.它们都按点的顺序返回值,但 extract 还添加了(顺序的)“点 id”——这主要用于当你有重叠的多边形时,这样一个点可能会落在两个多边形中。

over(pts, pol)
#  ID_1       NAME_1 ID_2           NAME_2 AREA
#1    3   Luxembourg    9 Esch-sur-Alzette  251
#2    2 Grevenmacher    7           Remich  129
#3    3   Luxembourg   11           Mersch  233
#4    2 Grevenmacher    6       Echternach  188
#5    1     Diekirch    1         Clervaux  312

extract(pol, pts)
#  point.ID poly.ID ID_1       NAME_1 ID_2           NAME_2 AREA
#1        1      10    3   Luxembourg    9 Esch-sur-Alzette  251
#2        2       7    2 Grevenmacher    7           Remich  129
#3        3      12    3   Luxembourg   11           Mersch  233
#4        4       6    2 Grevenmacher    6       Echternach  188
#5        5       1    1     Diekirch    1         Clervaux  312

If you just want the intersection, say of the points that fall in "Grenmacher", like you may want to do with your countries, you can如果您只想要交叉点,比如说落在“Grenmacher”中的点,就像您可能想要与您的国家一样,您可以

g <- pol[pol$NAME_1 == "Grevenmacher", ]
i <- intersect(pts, g)
i
#class       : SpatialPointsDataFrame 
#features    : 2 
#extent      : 6.27111, 6.27111, 49.53585, 49.7887  (xmin, xmax, ymin, ymax)
#crs         : +proj=longlat +datum=WGS84 +no_defs 
#variables   : 5
#names       : ID_1,       NAME_1, ID_2,     NAME_2, AREA 
#min values  :    2, Grevenmacher,    6, Echternach,  129 
#max values  :    2, Grevenmacher,    7,     Remich,  188 

And perhaps write that back to disk like this也许像这样写回磁盘

shapefile(i, "test.shp")

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

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