2014-04-15 15 views
13

Sto usando {raster} per ritagliare (o ritagliare) un raster basato su un shapefile irregolare (il bioma di Amazon) ma l'output ha sempre un'estensione rettangolare. Tuttavia, ho bisogno dell'output nella stessa geometria dello shapefile. Qualche consiglio? Saluti.Ritaglio del raster utilizzando lo shapefile in R, ma mantenendo la geometria dello shapefile

library(raster) 
library(rgdal) 

myshp <- readOGR("Amazon.shp", layer="Amazon") 
e <- extent(myshp) 
myraster <- raster("Temperature.tif") 
myraster.crop <- crop(myraster, e, snap="out", filename="myoutput.tif") 

risposta

20

Una possibilità è quella di utilizzare raster::mask()

library(maptools) ## For wrld_simpl 
library(raster) 

## Example SpatialPolygonsDataFrame 
data(wrld_simpl) 
SPDF <- subset(wrld_simpl, NAME=="Brazil") 

## Example RasterLayer 
r <- raster(nrow=1e3, ncol=1e3, crs=proj4string(SPDF)) 
r[] <- 1:length(r) 

## crop and mask 
r2 <- crop(r, extent(SPDF)) 
r3 <- mask(r2, SPDF) 

## Check that it worked 
plot(r3) 
plot(SPDF, add=TRUE, lwd=2) 

enter image description here

+0

funzionato perfettamente! Grazie. –

Problemi correlati