创建一个空间多边形数据框,保留与另一个空间多边形数据框重叠的要素,但不裁剪多边形范围
Create a spatial polygons data frame that preserves overlapping features with another spatial polygons data frame but does not clip polygon extent
我一直在使用 R 中栅格包中的 intersect()
函数将空间多边形数据框(HUC-4 分水岭)裁剪到另一个空间多边形数据框(科罗拉多州组成的区域)的范围、爱达荷州、蒙大拿州、犹他州和怀俄明州)。
我想保留与我剪裁到的空间数据框重叠的空间多边形的整个范围。使用 intersect()
剪辑 HUC-4 分水岭,使它们不会延伸超过被剪辑到的州的范围。
我正在使用的流域数据可以从以下网址下载:ftp://rockyftp.cr.usgs.gov/vdelivery/Datasets/Staged/Hydrography/WBD/National/GDB/ (WBD_National_GDB.zip).
科罗拉多州、犹他州、爱达荷州、怀俄明州和蒙大拿州的地区数据是从此处提供的县数据中提取的:https://catalog.data.gov/dataset/tiger-line-shapefile-2017-nation-u-s-current-county-and-equivalent-national-shapefile。
我用intersect()
函数做剪辑的代码如下:
library(raster)
library(dplyr)
library(spdplyr)
library(rgdal)
library(rgeos)
# albers equal area projection
proj <- CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")
counties <- readOGR(dsn = "./data/tl_2017_us_county/tl_2017_us_county.shp")
# filtering out only counties in our 5 states of interest
counties <- counties %>%
filter(STATEFP %in% c("08", "16", "30", "49", "56"))
# transforming to albers projection
counties <- spTransform(counties, proj)
# create a region shapefile (to clip watersheds with)
region <- gUnaryUnion(counties)
# Make Region into a SpatialPolygonsDataFrame
row.names(region) <- as.character(1:length(region))
region_data <- c("West")
region_data <- as.data.frame(region_data)
colnames(region_data) <- "Region"
region <- SpatialPolygonsDataFrame(region, region_data)
file <- "./data/WBD_National_GDB/WBD_National_GDB.gdb"
# huc4 watersheds
huc4 <- readOGR(dsn = file, layer = "WBDHU4")
# transforming to albers projection
huc4 <- spTransform(huc4, proj)
# selecting only huc4 watersheds that intersect with our states of interest
huc4_clip <- raster::intersect(huc4, region)
# plot the result
plot(huc4_clip)
我想要一个输出文件,该文件不裁剪感兴趣区域边缘的空间多边形的范围,但不包含任何不与感兴趣区域直接重叠的空间多边形。是否有任何其他我可以使用的函数类似于 intersect()
但不会裁剪区域边界上空间多边形的范围?
如果我正确理解了这个问题,您可以使用函数 gIntersects
找出哪些分水岭与您的区域相交,然后仅从 huc4 数据集中提取那些分水岭。在实践中,这样的事情可能会奏效:
intersects <- which(gIntersects(huc4, region, byid = TRUE))
huc4_clip <- huc4[intersects, ]
我一直在使用 R 中栅格包中的 intersect()
函数将空间多边形数据框(HUC-4 分水岭)裁剪到另一个空间多边形数据框(科罗拉多州组成的区域)的范围、爱达荷州、蒙大拿州、犹他州和怀俄明州)。
我想保留与我剪裁到的空间数据框重叠的空间多边形的整个范围。使用 intersect()
剪辑 HUC-4 分水岭,使它们不会延伸超过被剪辑到的州的范围。
我正在使用的流域数据可以从以下网址下载:ftp://rockyftp.cr.usgs.gov/vdelivery/Datasets/Staged/Hydrography/WBD/National/GDB/ (WBD_National_GDB.zip).
科罗拉多州、犹他州、爱达荷州、怀俄明州和蒙大拿州的地区数据是从此处提供的县数据中提取的:https://catalog.data.gov/dataset/tiger-line-shapefile-2017-nation-u-s-current-county-and-equivalent-national-shapefile。
我用intersect()
函数做剪辑的代码如下:
library(raster)
library(dplyr)
library(spdplyr)
library(rgdal)
library(rgeos)
# albers equal area projection
proj <- CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")
counties <- readOGR(dsn = "./data/tl_2017_us_county/tl_2017_us_county.shp")
# filtering out only counties in our 5 states of interest
counties <- counties %>%
filter(STATEFP %in% c("08", "16", "30", "49", "56"))
# transforming to albers projection
counties <- spTransform(counties, proj)
# create a region shapefile (to clip watersheds with)
region <- gUnaryUnion(counties)
# Make Region into a SpatialPolygonsDataFrame
row.names(region) <- as.character(1:length(region))
region_data <- c("West")
region_data <- as.data.frame(region_data)
colnames(region_data) <- "Region"
region <- SpatialPolygonsDataFrame(region, region_data)
file <- "./data/WBD_National_GDB/WBD_National_GDB.gdb"
# huc4 watersheds
huc4 <- readOGR(dsn = file, layer = "WBDHU4")
# transforming to albers projection
huc4 <- spTransform(huc4, proj)
# selecting only huc4 watersheds that intersect with our states of interest
huc4_clip <- raster::intersect(huc4, region)
# plot the result
plot(huc4_clip)
我想要一个输出文件,该文件不裁剪感兴趣区域边缘的空间多边形的范围,但不包含任何不与感兴趣区域直接重叠的空间多边形。是否有任何其他我可以使用的函数类似于 intersect()
但不会裁剪区域边界上空间多边形的范围?
如果我正确理解了这个问题,您可以使用函数 gIntersects
找出哪些分水岭与您的区域相交,然后仅从 huc4 数据集中提取那些分水岭。在实践中,这样的事情可能会奏效:
intersects <- which(gIntersects(huc4, region, byid = TRUE))
huc4_clip <- huc4[intersects, ]