[英]Shapefile to raster conversion in R?
我有一個從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功能,但我仍然無法使其正常工作。 我很感激有關如何做到這一點的任何建議。
您認為應該使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.