在R中将Shapefile转换为栅格吗? [英] Shapefile to raster conversion in R?

查看:280
本文介绍了在R中将Shapefile转换为栅格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从worldwildlife.org下载了一个用于世界陆地生态区的shapefile.可以在此处加载文件: http://worldwildlife.org/publications/terrestrial -ecoregions-of-the-世界.

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

它是一个标准形状文件,我想用它做两件事. 首先:从我的本地目录中获取shapefile并将其裁剪到北美东部的某个范围(ext =范围(-95,-50,24,63))

It comes as a standard shape file and I would like to do two things with it. First: take the shapefile from my local directory and clip it to an extent of eastern 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)

我确定必须在软件包"raster"中使用栅格化功能,但仍然无法使其正常工作.如果有任何建议,我将不胜感激.

I am sure I have to use the rasterize function in the package "raster" but I am still not able to get it work correctly. I would appreciate any suggestions on how to do this.

推荐答案

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

You are right to think that you should be using raster (rather than the sp raster Spatial classes) for spatial raster data. You should also use rgdal (rather than maptools) for reading, writing, and otherwise manipulating spatial vector data.

这应该使您入门:

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)

这篇关于在R中将Shapefile转换为栅格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆