maps - Crop rasterLayer with SpatialPolygonDataFrame leaves out part of the target region R -
library(raster) library(ncdf4) library(rgdal)
i trying crop raster layer based on polygon: sample data (~45mb) can found here sample data illustration:
dat<- raster('data.nc')# make subset dat class : rasterlayer dimensions : 824, 935, 770440 (nrow, ncol, ncell) resolution : 10000, 10000 (x, y) extent : -5000, 9345000, -5000, 8235000 (xmin, xmax, ymin, ymax) coord. ref. : na data source : c:\users\data.nc names : total.precipitation zvar : total.precipitation
as coord. ref = na
, assign polar stereographic projection based on information website of data provider.
projj=crs("+proj=stere +lat_0=90 +lat_ts=90 +lon_0=-100 +k=0.994 +x_0=2000000 +y_0=2000000 +datum=wgs84 +units=m +no_defs") proj4string(dat) <- projj #rx <- projectraster(from=dat, crs=wgs84.p4s)
then transform bbox shapefile projj
shgrid <- sptransform(prairie.boundaries, projj)# transform dat spat reference plot(dat) plot(shgrid,add=t)# gives
cropping
cr <- crop(dat, extent(shgrid)) plot(cr) fr <- rasterize(shgrid, cr) dat1<- mask(x=cr, mask=fr) levelplot(dat1)
output:
the cropped image shows part of canada want of canada.
what did miss within code? projection?
besides prefer axes labels in latlon coordinates.
any suggestions?
Comments
Post a Comment