【问题标题】:Count of sampling points within a grid cell网格单元内的采样点数
【发布时间】:2019-05-21 00:18:41
【问题描述】:

计算空间网格的每个网格单元内的采样点总数。

我想制作一个网格并计算每个网格单元内的采样点总数。我创建了一个随机生成的数据和网格,并尝试使用 sf 和 raster 包计算网格单元格内的记录数,使用以前类似的 SO 问题,但没有成功。我还研究了提取功能。我对空间分析相当陌生。

 library(sf)
library(raster)
library(tidyverse)
library(mapview)
library(mapedit)

#Trial with sf package 
# load some spatial data. Administrative Boundary
#https://stackoverflow.com/questions/41787313/how-to-create-a-grid-of-       spatial-points
aut <- getData('GADM', country = 'aut', level = 0)
aut <- st_as_sf(aut)
#Try with polygons
grid <- aut %>% 
 st_make_grid(cellsize = 0.5, what = "polygons") %>% 
  st_intersection(aut)                               

#fake data
 lat<-runif(1000, 46.5, 48.5)
 lon<-runif(1000, 13,16)
pos<-data.frame(lat,lon)

 ggplot() + 
  geom_sf(data = aut) + 
  geom_sf(data = grid)+
geom_point(data=pos, aes(lon, lat)) 
#how to count number of records within each cell?  
 ########################################
#Trial with raster package
#https://stackoverflow.com/questions/32889531/r-how-can-i-count-how-   many-points-are-in-each-cell-of-my-grid
 r<-raster(xmn=13, ymn=46.5, xmx=16, ymx=48.5, res=0.5)
r[] <- 0
#How do I use the pos data here
xy <- spsample(as(extent(r), 'SpatialPolygons'), 100, 'random')
tab <- table(cellFromXY(r, xy))
r[as.numeric(names(tab))] <- tab
plot(r)
points(xy, pch=20)
d <- data.frame(coordinates(r), count=r[])

我想获得一个包含采样点数的表格。

【问题讨论】:

  • 您查看过 stat_bin_2d 吗?或者stat_hex_bin?这些计算每个 bin 的计数和密度。
  • 感谢您的建议。我没有考虑过 stat_bin_2d 或 stat_hex_bin。你如何获得垃圾箱的数量?我刚开始玩这些功能,但到目前为止还没有得到任何有意义的东西。 ggplot(pos, aes(lon, lat)) + geom_bin2d(binwidth = 2) + stat_bin2d(geom = "text", aes(label = ..count..),binwidth = 10) + scale_fill_gradient(low = "white" , 高 = "红色")

标签: r r-raster sf


【解决方案1】:

计算st_intersectslengths(注意:不是st_intersection)会给你一个包含在每个网格单元格中的点向量:

library(sf)
library(raster)
library(tidyverse)
library(mapview)
library(mapedit)

#Trial with sf package 
# load some spatial data. Administrative Boundary
#https://stackoverflow.com/questions/41787313/how-to-create-a-grid-of-       spatial-points
aut <- getData('GADM', country = 'aut', level = 0)
aut <- st_as_sf(aut)
#Try with polygons
grid <- aut %>% 
  st_make_grid(cellsize = 0.5, what = "polygons") %>% 
  st_intersection(aut)                               

#fake data
lat<-runif(1000, 46.5, 48.5)
lon<-runif(1000, 13,16)
pos<-data.frame(lat,lon)

pos = st_as_sf(pos, coords = c("lon", "lat"), crs = 4326)

tab = st_intersects(grid, pos)
lengths(tab)
[1]  0  0  0  0  4 24 23 34 23 13 14  0  0  0  0  0  0  0  3 38 40 48 46 47 33  0  0  0  0  0  0  0
[33]  0 35 48 51 35 38 44  0  0  0  0 44 43 41 53 44 32  0  0  0  0  8  8 10 12  7  0  0  0  0  0

如果您想将其作为sf 对象绑定到网格,您可以这样做:

grid = st_sf(n = lengths(tab), geometry = st_cast(grid, "MULTIPOLYGON"))

mapview(grid, zcol = "n")

【讨论】:

    【解决方案2】:

    示例数据

    library(raster)   
    aut <- getData('GADM', country = 'aut', level = 0)
    r <- raster(aut, res=0.5)
    lat <- runif(1000, 46.5, 48.5)
    lon <- runif(1000, 13,16)
    # note that you should use (lon, lat), in that order!
    pos <- data.frame(lon, lat)
    

    解决方案

    r <- rasterize(pos, r, fun="count")
    plot(r)
    

    要得到一张桌子,你可以这样做

    x <- rasterToPoints(r)
    z <- cbind(cell=cellFromXY(r, x[,1:2]), value=x[,3])
    head(z)
    #     cell value
    #[1,]   22    4
    #[2,]   23   45
    #[3,]   24   36
    #[4,]   25   52
    #[5,]   26   35
    #[6,]   27   38
    

    或者,或者,na.omit(cbind(1:ncell(r), values(r)))

    【讨论】:

      【解决方案3】:

      试试

      ggplot(pos, aes(x = lon, y=lat)) + 
      geom_bin2d(binwidth = 2) +
      stat_bin_2d(aes(label=stat(count)), binwidth=2, geom="text", position="identity") +
      scale_fill_gradient(low = "white", high = "red")
      

      【讨论】:

      • 谢谢。您能否提供一个关于如何在地图上覆盖这个数字的建议,例如我以奥地利地图为例。它是一个 sf 对象,但可以使用 geom_sf 可视化 sf 对象。我一直在尝试没有成功。
      • 我不熟悉 sf 对象,但每当我需要绘制一些空间数据时,我都会使用传单 (rstudio.github.io/leaflet/shapes.html)
      猜你喜欢
      • 2013-06-15
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2013-08-23
      • 2016-03-02
      相关资源
      最近更新 更多