gpt4 book ai didi

r - 裁剪 SpatialPolygonsDataFrame

转载 作者:行者123 更新时间:2023-12-03 10:09:11 26 4
gpt4 key购买 nike

我有两个 SpatialPolygonsDataFrame文件:dat1、dat2

extent(dat1)
class : Extent
xmin : -180
xmax : 180
ymin : -90
ymax : 90


extent(dat2)
class : Extent
xmin : -120.0014
xmax : -109.9997
ymin : 48.99944
ymax : 60

我想使用 dat2 的范围裁剪文件 dat1。我不知道该怎么做。我之前只是使用“裁剪”功能处理光栅文件。

当我对当前数据使用此函数时,出现以下错误:
> r1 <- crop(BiomassCarbon.shp,alberta.shp)
Error in function (classes, fdef, mtable) :

unable to find an inherited method for function ‘crop’ for signature"SpatialPolygonsDataFrame"’

最佳答案

添加了简化方法 2014-10-9 :
raster::crop()可用于裁剪Spatial* (以及 Raster* )对象。

例如,这里是如何使用它来裁剪 SpatialPolygons*目的:

## Load raster package and an example SpatialPolygonsDataFrame
library(raster)
data("wrld_simpl", package="maptools")

## Crop to the desired extent, then plot
out <- crop(wrld_simpl, extent(130, 180, 40, 70))
plot(out, col="khaki", bg="azure2")

原始(仍然有效)答案:

rgeos 功能 gIntersection()使这非常简单。

使用 mnel 的漂亮示例作为起点:
library(maptools)
library(raster) ## To convert an "Extent" object to a "SpatialPolygons" object.
library(rgeos)
data(wrld_simpl)

## Create the clipping polygon
CP <- as(extent(130, 180, 40, 70), "SpatialPolygons")
proj4string(CP) <- CRS(proj4string(wrld_simpl))

## Clip the map
out <- gIntersection(wrld_simpl, CP, byid=TRUE)

## Plot the output
plot(out, col="khaki", bg="azure2")

enter image description here

关于r - 裁剪 SpatialPolygonsDataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com