简体   繁体   English

R 中数据帧的空间子集

[英]Spatial subset of data frame in R

I have a big data frame that I plotted using plot() .我有一个使用plot()绘制的大数据框。 I then used:然后我用:

library(splancs) 

polygon_xy = getpoly(quiet=FALSE) 

and drew points on the plot to select my area of interest.并在 plot 到 select 我感兴趣的区域上画了点。 This generated the x,y coordinates of the polygon I drew.这生成了我绘制的多边形的 x,y 坐标。

I want to extract the data that sits inside the polygon, or to subset my df to include only the points that sit inside the polygon.我想提取位于多边形内的数据,或者将我的 df 子集化以仅包含位于多边形内的点。 Any suggestions how to do that?任何建议如何做到这一点?

The point data needs to be in a specific format (ie, a matrix with x and y) when you use plot and for getpoly to recognize the coordinates.当您使用plot并让getpoly识别坐标时,点数据需要采用特定格式(即带有 x 和 y 的矩阵)。

library(splancs)
library(tidyverse)
library(sf)

set.seed(543)
xy <-
  cbind(x = runif(n = 25, min = -118, max = -117),
        y = runif(n = 25, min = 40, max = 42))

plot(xy)

# Draw a polygon for study area.
poly <- getpoly()

# Convert to sf objects.
polysf <- st_as_sf(as.data.frame(poly), coords = c("V1", "V2"), crs = 4326) %>% 
  dplyr::summarise() %>%
  st_cast("POLYGON") %>% 
  st_convex_hull()

xysf <- st_as_sf(as.data.frame(xy), coords = c("x", "y"), crs = 4326)

# Do an intersection to keep only points inside the drawn polygon.
xy_intersect <- st_intersection(polysf, xysf)

Output Output

Simple feature collection with 9 features and 0 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -117.7913 ymin: 40.82405 xmax: -117.4264 ymax: 41.7448
Geodetic CRS:  WGS 84
                    geometry
1 POINT (-117.4264 41.18712)
2  POINT (-117.5756 41.7448)
3 POINT (-117.7913 40.82405)
4 POINT (-117.7032 41.15077)
5 POINT (-117.5634 41.23936)
6 POINT (-117.7441 40.84163)
7  POINT (-117.692 41.27514)
8 POINT (-117.6864 40.98462)
9 POINT (-117.5759 40.88477)

Plotted with mapview::mapview(xy_intersect) from library(mapview)library(mapview)中的mapview::mapview(xy_intersect)绘制

在此处输入图像描述

However, if you want to extract rows from your original dataframe, then here is another hack for extracting the points that fall within a drawn polygon (when the polygon coordinates look like 0.003456 for example).但是,如果您想从原始 dataframe 中提取行,那么这里有另一个技巧,用于提取落在绘制多边形内的点(例如,当多边形坐标看起来像 0.003456 时)。

library(splancs)
library(tidyverse)

set.seed(543)
xy <-
  cbind(x = runif(n = 25, min = -118, max = -117),
        y = runif(n = 25, min = 40, max = 42))

plot(xy)

# Draw a polygon for study area.
poly <- getpoly()

# Plot the results.
plot(xy)
polygon(poly)

# This will return a logical vector for points in the polygon
io <- inout(xy, poly)
points(xy[io,], pch = 16, col = "blue")

# Then, can use the index from io to extract the points that 
# are inside the polygon from the original set of points.
extract_points <- as.data.frame(xy)[which(io == TRUE),]

extract_points

Output Output

           x        y
2  -117.4506 41.17794
3  -117.4829 40.71030
8  -117.4679 40.71702
19 -117.3354 40.53687
21 -117.5219 40.47077
22 -117.4876 40.18188
25 -117.2015 40.86243

在此处输入图像描述

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

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