简体   繁体   English

将看起来像栅格的大型空间多边形与栅格图层R合并

[英]Merging a large spatial polygon that looks like a grid with a raster layer R

I have a large Spatial polygon data frame, i also have a raster layer that has many points of information. 我有一个很大的空间多边形数据框,也有一个包含许多信息点的栅格图层。 Both are geo referenced and i would like to overlay the grid on top of my raster layer and assign each point inside to a grid cell that it falls into. 两者都是地理参考,我想将栅格覆盖在栅格图层的顶部,并将内部的每个点分配给它所属的栅格像元。 The problem i am running into is that the grid i have is not being treated like a grid but just a plotted polygon.This is what i get when i plot one on top of the other in ggplot after converting both to points. 我遇到的问题是我所拥有的网格并没有像网格那样被视为网格,而只是一个绘制的多边形。这就是当我在将ggplot转换为点之后在ggplot中将一个绘制在另一个之上时得到的结果。 小号

A look at the data changed into points: 看一下数据变成点了:

Grid: 网格:

      long    lat order  hole piece   id  group 
1 -101.940 31.710     1 FALSE     1 1012 1012.1  
2 -101.940 31.715     4 FALSE     1 1012 1012.1  
3 -101.940 31.710     5 FALSE     1 1012 1012.1  
4 -101.930 31.670     3 FALSE     1 1016 1016.1  
5 -101.925 31.670     4 FALSE     1 1016 1016.1  
6 -101.890 31.715     1 FALSE     1 1028 1028.1  

Raster Layer: 栅格层:

      longS     latS intensity
1 -101.9395 31.73822        85
2 -101.9394 31.73822        85
3 -101.9393 31.73822        86
4 -101.9392 31.73822        87
5 -101.9391 31.73822        62
6 -101.9390 31.73822        65

Transformations done using: 使用以下方法完成的转​​换:

S <- rasterToPoints(S)
SHP <- fortify(SHP)

Things i have tried: Different merging techniques for polygons and raster but for the examples i found people made there own grid and in this case i must use this grid or one with the exact dimensions. 我尝试过的方法:对于多边形和栅格使用不同的合并技术,但是对于示例,我发现人们制作了自己的网格,在这种情况下,我必须使用此网格或具有精确尺寸的网格。

Iv also tried to change them to points and merge based on the id of the polygon but i found that the id is not related to the cell units just the lines being made themselves. Iv还尝试将其更改为点并根据多边形的ID进行合并,但是我发现ID与仅由自己制作的线与单元格无关。

Any help on merging the two and assigning the points inside the shown grid cells would be much appreciated. 我们非常感谢您对合并两者以及在所示网格单元内分配点的任何帮助。

Your question is confusing as the data you show do not seem to match what you state, and by transforming the data to make them ggplot-ready. 您的问题令人困惑,因为您显示的数据似乎与您陈述的内容不匹配,并且通过转换数据使其适合ggplot。 In stead, please provide some simple example data, if you can, like this: 相反,请尽可能提供一些简单的示例数据,如下所示:

library(raster)
r <- raster(nrow=2, ncol=4, vals=1:8)  
p <- as(r, 'SpatialPolygonsDataFrame')
r <- disaggregate(r, 2)
values(r) <- 1:ncell(r)
plot(r)
plot(p, add=TRUE)

With SpatialPolygonsDataFrame p and RasterLayer r , you can now do 使用SpatialPolygonsDataFrame pRasterLayer r ,您现在可以执行

p$ID <- 1:length(p)
x <- rasterize(p, r, 'ID')
s <- stack(x, r)
head(values(s))

#     layer.1 layer.2
#[1,]       1       1
#[2,]       1       2
#[3,]       2       3
#[4,]       2       4
#[5,]       3       5
#[6,]       3       6

Or 要么

e <- extract(r, p)

e[1:3]
#[[1]]
#[1]  1  2  9 10
#[[2]]
#[1]  3  4 11 12
#[[3]]
#[1]  5  6 13 14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM