裁剪SpatialPolygonsDataFrame [英] Crop for SpatialPolygonsDataFrame

查看:128
本文介绍了裁剪SpatialPolygonsDataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 SpatialPolygonsDataFrame 文件:dat1,dat2

I have two SpatialPolygonsDataFrame files: 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。我不知道该怎么做我只是使用 crop功能处理栅格文件。

I want to crop the file dat1 using the extent of dat2. I don't know how to do it. I just handle raster files using "crop" function before.

当我将此功能用于当前数据时,会发生以下错误:

When I use this function for my current data, the following error occurs:

> 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 * )对象。

raster::crop() can be used to crop Spatial* (as well as Raster*) objects.

例如,以下是使用它来裁剪 SpatialPolygons * 对象的方法:

For example, here's how you might use it to crop a SpatialPolygons* object:

## 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")






原始(且仍在起作用)答案:

r geos 函数 gIntersection()使此操作非常简单。

The rgeos function gIntersection() makes this pretty straightforward.

使用mnel的漂亮示例作为过渡点:

Using mnel's nifty example as a jumping off point:

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")

这篇关于裁剪SpatialPolygonsDataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆