Shapefile for raster conversion to R?

I have a shapefile downloaded from worldwildlife.org for the terrestrial ecoregions of the world. The file can be downloaded here: http://worldwildlife.org/publications/terrestrial-ecoregions-of-the-world .

It comes as a standard form file, and I would like to do two things with it. First: take the shapefile from my local directory and copy it within the eastern part of North America (ext = extent (-95, -50, 24, 63))

# Read shapefile using package "maptools" eco_shp <- readShapeLines("F:/01_2013/Ecoregions/Global/wwf_terr_ecos.shp", proj4string=CRS("+proj=utm +zone=33 +datum=WGS84")) # Set the desired extent for the final raster using package "raster" ext <- extent(-95, -50, 24, 63) 

I am sure that I need to use the rasterization function in the β€œraster” package, but I still cannot get it to work correctly. I would appreciate any suggestions on how to do this.

+8
r data-conversion raster shapefile rasterizing
source share
1 answer

You are right to think that for spatial raster data you should use raster (and not sp raster spatial classes). You should also use rgdal (not maptools ) for reading, writing, and other manipulations with spatial vector data.

This should help you:

 library(rgdal) library(raster) ## Read in the ecoregion shapefile (located in R current working directory) teow <- readOGR(dsn = "official_teow/official", layer = "wwf_terr_ecos") ## Set up a raster "template" to use in rasterize() ext <- extent (-95, -50, 24, 63) xy <- abs(apply(as.matrix(bbox(ext)), 1, diff)) n <- 5 r <- raster(ext, ncol=xy[1]*n, nrow=xy[2]*n) ## Rasterize the shapefile rr <-rasterize(teow, r) ## A couple of outputs writeRaster(rr, "teow.asc") plot(rr) 

enter image description here

+11
source share

All Articles