空间多边形掩盖栅格中的错误 [英] error in mask a raster by a spatialpolygon

查看:96
本文介绍了空间多边形掩盖栅格中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能的栅格:

I have raster of the following features:

library(raster)
library(rgeos)
test <- raster(nrow=225, ncols=478, xmn=-15.8, xmx=32, ymn=-9.4, ymx=13.1)

我想在此栅格中遮盖点的给定距离内的像元. 我按照以下步骤创建空间点:

I want to mask in this raster the cells that are within a given distance of a point. I create the spatial points as followed:

p2=readWKT("POINT(31.55 -1.05)")   

然后我通过添加0.5缓冲区来创建空间多边形对象:

Then I create a spatial polygon object by adding a 0.5 buffer:

p2_Buffered <- gBuffer(p2, width = 0.5)

mask(test, mask=p2_Buffered,inverse=T)

在给定此空间对象的栅格蒙版时,出现以下错误消息:

When I mask my raster given this spatial object, I have the following error message:

.polygonsToRaster(x,y,field = field,fun = fun,background中的错误 =背景,:要替换的项目数不是替换长度的倍数

Error in .polygonsToRaster(x, y, field = field, fun = fun, background = background, : number of items to replace is not a multiple of replacement length

我不明白,因为这是我用不同的点和不同的缓冲区宽度运行了很多次的脚本,没有任何问题.

I do not understand because this is script I have been running many many times with different point and different buffer width without any problem.

奇怪的是,当我更改缓冲区的宽度时,它可以正常工作:

What is strange is that when I change the width of the buffer, it works fine:

p2_Buffered <- gBuffer(p2, width = 0.4)
mask(test, mask=p2_Buffered,inverse=T)

对于其他焦点也是如此:

This is also true for a different focal point:

p2=readWKT("POINT(32.55 -1)")
p2_Buffered <- gBuffer(p2, width = 0.5)
mask(test, mask=p2_Buffered,inverse=T)

我想指出这一点上的具体问题,因为这是我应该在例程中运行的脚本(到目前为止,我一直没有遇到任何问题).

I would like to identify the specific problem I have for that point because this is a script I should run in a routine (I have been doing it without any problem so far).

非常感谢

推荐答案

您通常需要为栅格图层设置一些值.对于遮罩层,始终最好将值设置为1.

You usually need to set some values to the raster layer. For a mask layer its always best to set values to 1.

    library(raster)
    library(rgeos)

# make sample raster    
test <- raster(nrow=225, ncols=478, xmn=-15.8, xmx=32, ymn=-9.4, ymx=13.1)
# set values of raster for mask
test <- setValues(test, 1)

# make point buffer
p2=readWKT("POINT(15 5)")
p2_Buffered <- gBuffer(p2, width = 1.5)
# name projection of buffer (assume its the same as raster)
projection(p2_Buffered) <- projection(test)

# visual check 
plot(test); plot(p2_Buffered, add=T)

如果要将栅格图层修剪为仅一个多边形,请尝试此工作流程.

If you want to trim down your raster layer to the just the single polygon then try this workflow.

step1 <- crop(test, p2_Buffered) # crop to same extent
step2 <- rasterize(p2_Buffered, step1) # rasterize polygon 
final <- step1*step2 # make your final product
plot(final)

如果只想在栅格图层上戳一个孔,请使用遮罩功能

If you just want to poke a hole in your raster layer then use the mask function

# rasterize your polygon
p2_Buffered <- rasterize(p2_Buffered, test, fun='sum')

# now mask it 
my_mask <- mask(test, mask=p2_Buffered,inverse=T) # try changing the inverse argument 
plot(my_mask)

这篇关于空间多边形掩盖栅格中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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