使用在 Leaflet 中创建 surface_polygons 的 voronoi 创建带有点数据的等值线图

Creating a choropleth map with point data using voronoi created surface_polygons in Leaflet

我有一个棘手的问题。

我正在尝试为某种 'pretty' 元数据浏览器可视化一些数据。基本点数据格式如下:

> print(tempdata[1:5, ])
  Station  Lat_dec  Long_dec Surface_T
1     247 50.33445 -2.240283     15.19
2     245 50.58483 -2.535217     14.11
3     239 50.16883 -2.509250     15.41
4     225 50.32848 -2.765967     15.34
5     229 50.63900 -2.964800     14.09

我可以使用 Lat、Long 和 Temp 创建以下 voronoi 多边形,并使用一个简单的框来裁剪它们,这样它们就不会永远延伸。

# Creating Stations
stations <- st_as_sf(df,
                     coords = c("Long_dec","Lat_dec")
                     )

# Create voronoi/thiessen polygons
v <- stations %>% 
  st_union() %>%
  st_voronoi() %>%
  st_collection_extract()

# Creating boundary box
box <- st_bbox(stations) %>% 
  st_as_sfc()

# Clipping voronoi to boundary box
hmm <- st_crop(v, box)

这将生成以下表面多边形:

> str(hmm)
sfc_POLYGON of length 107; first list element: List of 1
 $ : num [1:7, 1:2] -7.23 -6.94 -6.95 -7.04 -7.24 ...
 - attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"

绘制为:

leaflet() %>% 
  addPolygons(data = hmm) %>% 
  addProviderTiles(providers$Esri.WorldTerrain)

我想做的是根据温度为表面多边形着色,例如更热更红等。我已经尝试了所有方法,但通常 R 会崩溃。

我认为这与表面多边形上没有任何信息有关,例如站点或链接到原始数据的多边形 ID 号。

我很难过,任何帮助都会很棒!!

包:

library(sf)
library(dplyr)
library(rgdal)
library(leaflet)

更新:

> tempdata[1:10, ]
   Station  Lat_dec  Long_dec Surface_T
1      247 50.33445 -2.240283     15.19
2      245 50.58483 -2.535217     14.11
3      239 50.16883 -2.509250     15.41
4      225 50.32848 -2.765967     15.34
5      229 50.63900 -2.964800     14.09
6      227 50.33757 -3.303217     15.12
7      217 50.16657 -3.563817     15.13
8      207 49.66683 -3.556550     15.04
9      213 50.16512 -3.824667     14.97
10     219 49.83707 -3.815483     14.78

stations <- st_as_sf(tempdata,
                     coords = c("Long_dec","Lat_dec"))

test <- st_sample(stations,
                  size = as.numeric(count(tempdata))
                  )


join <- st_sf("temp" = stations$Surface_T, geometry = test) 

这对我来说也是新的。以前从未与 voronois 合作过。但问题确实是您的 stations 数据框失去了 st_union().
的所有功能

只加好像不行,因为多边形的顺序和之前点的顺序不一样。因此,空间连接可能是一个很好的解决方法。

使用我自己的示例数据:

library(sf)
library(leaflet)

#will work with any polygon
samplepoints_sf <- st_sample(bw_polygon, size = 2000, type = "random", crs = st_crs(4326))[1:500]
# although coordinates are longitude/latitude, st_intersects assumes that they are planar

#create an sf-object like your example
bw_sf <- st_sf("some_variable" = sample(1:50, 500, replace = TRUE), geometry = samplepoints_sf)

#create the voronoi diagram, "some_variable" gets lost.
v <- bw_sf %>% 
  st_union() %>%
  st_voronoi() %>%
  st_collection_extract()

#do a spatial join with the original bw_sf data frame to get the data back
v_poly <- st_cast(v) %>% 
            st_intersection(bw_polygon) %>%
              st_sf() %>%
                st_join(bw_sf, join = st_contains)

#create a palette (many ways to do this step)
colors <- colorFactor(
  palette = c("green", "yellow", "red"),
  domain = (v_poly$some_variable)

#create the leaflet
leaflet(v_poly) %>% addTiles() %>%
                 addPolygons(fillColor = colors(v_poly$some_variable),
                             fillOpacity = 0.7,
                             weight = 1,
                             popup = paste("<strong> some variable: </strong>",v_poly$some_variable))

所以,希望这对你有用。