在R中将Shapefile转换为光栅?

问题描述 投票:8回答:1

我有一个从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)

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

r raster shapefile data-conversion rasterizing
1个回答
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)

© www.soinside.com 2019 - 2024. All rights reserved.