gpt4 book ai didi

r - R中的Shapefile到光栅转换?

转载 作者:行者123 更新时间:2023-12-04 06:21:21 24 4
gpt4 key购买 nike

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

enter image description here

关于r - R中的Shapefile到光栅转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com