在R中将Shapefile转换为光栅?

I D*_*oro 8 r raster data-conversion shapefile rasterizing

我有一个从worldwildlife.org下载的shapefile,用于世界地球生态区.该文件可以在这里加载:http://worldwildlife.org/publications/terrestrial-ecoregions-of-the-world.

它是一个标准的形状文件,我想用它做两件事.首先:从我的本地目录中获取shapefile并将其剪辑到北美东部(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)
Run Code Online (Sandbox Code Playgroud)

我确信我必须在"raster"包中使用rasterize功能,但我仍然无法使其正常工作.我很感激有关如何做到这一点的任何建议.

Jos*_*ien 12

您认为应该使用空间栅格数据raster(而不是sp栅格空间类)是正确的.您还应该使用rgdal(而不是maptools)读取,写入和操纵空间矢量数据.

这应该让你开始:

library(rgdal)
library(raster)

## Read in the ecoregion shapefile (located in R's 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @MilesErickson确实.对于除了最小的栅格之外的任何栅格,我倾向于使用`gdalUtils :: gdal_rasterize`周围的包装器.对于1000×1000的栅格,我获得了6倍的加速,而对于2000×2000的栅格,我获得了12倍的加速(从70秒到6秒). (3认同)