【问题标题】:R Leaflet. Group point data into cells to summarise many data pointsR 传单。将点数据分组到单元格中以汇总许多数据点
【发布时间】:2020-10-14 07:30:21
【问题描述】:

早上、下午或晚上。

我有以下位置数据(从'Count of sampling points within a grid cell'调整)

# Demo data 
set.seed(123)
#
lat <- runif(1000, 46.5, 48.5)
lon <- runif(1000, 13,16)
#
pos <- data.frame(lon, lat)

使用以下内容:

ggplot(pos, aes(x = lon, y=lat)) + 
  geom_bin2d(bins = 25) +
  stat_bin_2d(aes(label=stat(count)), bins = 25, position="identity") +
  scale_fill_gradient(low = "white", high = "red")+
  theme_void()

给予:

很棒,但是,

我想在传单中做同样的事情,但似乎找不到一个简单的解决方案。实际上,我有超过 5,000,000 个数据点。

在单元格上运行鼠标或使用传单弹出功能时,将显示单元格的数据点数。

【问题讨论】:

    标签: r leaflet gis rgdal


    【解决方案1】:

    这是我的解决方案.. 它使用 sf-package,以及 tim 惊人的 fast leafgl-package...

    样本数据

    # Demo data 
    set.seed(123)
    lat <- runif(1000, 46.5, 48.5)
    lon <- runif(1000, 13,16)
    pos <- data.frame(lon, lat)
    

    代码

    library( sf )
    library( colourvalues )
    #use leafgl for FAST rendering of large sets of polygons..
    #devtools::install_github("r-spatial/leafgl")
    library( leafgl )
    library( leaflet )
    
    #create a spatial object with all points
    pos.sf <- st_as_sf( pos, coords = c("lon","lat"), crs = 4326)
    #create e grid of polygons (25x25) based on the boundary-box of the points in pos.sf
    pos.grid <- st_make_grid( st_as_sfc( st_bbox( pos.sf ) ), n = 25 ) %>% 
      st_cast( "POLYGON" ) %>% st_as_sf()
    #add count of points in each grid-polygon, based on an 
    # intersection of points with polygons from the grid
    pos.grid$count <- lengths( st_intersects( pos.grid, pos.sf ) )
    #add color to polygons based on count
    cols = colour_values_rgb(pos.grid$count, include_alpha = FALSE) / 255
    #draw leaflet
    leaflet() %>% 
      addTiles() %>% 
      leafgl::addGlPolygons( data = pos.grid,
                             weight = 1,
                             fillColor = cols,
                             fillOpacity = 0.8,
                             popup = ~count )
    

    输出

    【讨论】:

    • 您好 Wimpel,非常感谢您的全面回复。我很快就会开始的。只是为了承认。
    • 嗨,Wimpel,抱歉,我花了一段时间才回复它。运行第10行后报如下错误:Error in UseMethod("st_as_sf") : no applicable method for 'st_as_sf' applied to an object of class "c('sfc_POLYGON', 'sfc')"
    • 你能发布一些出现问题的示例数据吗?
    • 对我来说,它与问题中给出的示例数据一起发生。混乱!
    • pos.grid.filt &lt;- filter(pos.grid, count &gt; 0)ez
    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2021-07-07
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多