剪裁R中的栅格 [英] Clipping rasters in R
问题描述
我正在为美国东北部绘制地图.地图背景必须是海拔图或年平均温度图.我有来自Worldclim.org的两个栅格,这些栅格为我提供了这些变量,但是我需要将它们裁剪到我感兴趣的州的范围.有关如何执行此操作的任何建议.这是我到目前为止的内容:
I am building a map for the northeastern U.S.A. The map background needs to be either an altitude map or a mean annual temperature map. I have two rasters from Worldclim.org which give me these variables but I need to clip them to the extent of the states I am interested in. Any suggestions on how to do this. This is what I have so far:
#load libraries
library (sp)
library (rgdal)
library (raster)
library (maps)
library (mapproj)
#load data
state<- data (stateMapEnv)
elevation<-raster("alt.bil")
meantemp<-raster ("bio_1.asc")
#build the raw map
nestates<- c("maine", "vermont", "massachusetts", "new hampshire" ,"connecticut",
"rhode island","new york","pennsylvania", "new jersey",
"maryland", "delaware", "virginia", "west virginia")
map(database="state", regions = nestates, interior=T, lwd=2)
map.axes()
#add site localities
sites<-read.csv("sites.csv", header=T)
lat<-sites$Latitude
lon<-sites$Longitude
map(database="state", regions = nestates, interior=T, lwd=2)
points (x=lon, y=lat, pch=17, cex=1.5, col="black")
map.axes()
library(maps) #Add axes to main map
map.scale(x=-73,y=38, relwidth=0.15, metric=T, ratio=F)
#create an inset map
# Next, we create a new graphics space in the lower-right hand corner. The numbers are proportional distances within the graphics window (xmin,xmax,ymin,ymax) on a scale of 0 to 1.
# "plt" is the key parameter to adjust
par(plt = c(0.1, 0.53, 0.57, 0.90), new = TRUE)
# I think this is the key command from http://www.stat.auckland.ac.nz/~paul/RGraphics/examples-map.R
plot.window(xlim=c(-127, -66),ylim=c(23,53))
# fill the box with white
polygon(c(0,360,360,0),c(0,0,90,90),col="white")
# draw the map
map(database="state", interior=T, add=TRUE, fill=FALSE)
map(database="state", regions=nestates, interior=TRUE, add=TRUE, fill=TRUE, col="grey")
elevation
和 meantemp
对象是需要裁剪到 nestates
对象的区域范围的对象.任何输入都会有所帮助
The elevation
and meantemp
objects are the ones that need to be clipped to the area extent of the nestates
object. Any input would help
推荐答案
光栅包中的函数crop允许您使用一个范围对象或可以计算范围的对象来剪切(子集)另一个对象.包示例:
The function crop in the raster package allows you to use an Extent object or an object for which an Extent can be calculated to cut(subset) another object. The package example:
r <- raster(nrow=45, ncol=90)
r[] <- 1:ncell(r)
e <- extent(-160, 10, 30, 60)
rc <- crop(r, e)
如果您想更详细地进行切割,则可以在sp软件包中使用状态的SHP和over函数.
If you wanted to cut in a more detailed manner maybe you could use a SHP of the states and the over function in the sp package.
这篇关于剪裁R中的栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!