使用R中的voronoi镶嵌将点匹配到节点

时间:2019-07-15 08:09:02

标签: r voronoi sf

我正在尝试将多年来固定位置上的一组点的值与其也在固定点上的最近节点进行匹配。 为此,我选择使用Voronoi镶嵌,其中每个点的分数与最近的节点完全相关。 我正在使用不太了解的sf软件包。我做了两个排序假设,由于结果与预期不符,因此这些假设似乎不正确。假设显示在reprex的第37和38行。

在下面的reprex中,设置坐标,以便可以使用Voronoi方法或简单联接来计算节点分数。可以看出,这两个值不相同

我的问题是:我如何正确地将点与Voronoi镶嵌中的正确单元格匹配?

library(sf); library(dplyr); library(tibble)

#Create the data set of node locations
set.seed(256)
node_locations <- expand.grid(x = 1:10, y = 1:10) %>%
  as_tibble() %>% 
  mutate(Node = expand.grid(x = LETTERS[1:10], y = LETTERS[1:10], stringsAsFactors = FALSE)  %>%
           { paste0(.$x,.$y)})

#create the score spread across the geographical area of the points for multiple years
score_by_year <- expand.grid(x = 1:10, y = 1:10, year = 2001:2010) %>% as_tibble %>%
  mutate(score = sample(1:1000, size = nrow(.), replace = TRUE),
         ID = 1:n())

#convert to spatial data
node_locations_sf <- node_locations  %>% 
  st_as_sf(., coords = c("x", "y")) 
#add in the correct projection data to align with the map
st_crs(node_locations_sf) <- "+proj=longlat +datum=WGS84 +no_defs"

#convert to spatial data
score_by_year_sf <- score_by_year  %>% 
  st_as_sf(., coords = c("x", "y")) 
#add in the correct projection data to align with the map
st_crs(score_by_year_sf) <- "+proj=longlat +datum=WGS84 +no_defs"

#create voronoi tesselation
node_v <- node_locations_sf  %>% st_union() %>% st_voronoi()

#this is what it looks like
plot(node_v, col = "0")

#find which scores are associated with the nodes
voronoi_intersection <- st_intersects(st_cast(node_v), score_by_year_sf, sparse = FALSE) 

#create a dictionary to match nodes to score IDs
Node_data_dictionary <- voronoi_intersection %>% as_tibble(.) %>%
  bind_cols(node_locations %>% select(Node)) %>% #I assume that the row order is the same as the node_locations df
  set_names(c(score_by_year$ID, "Node")) %>% #I assume that the columns are the same order as the date_by_year df
  gather(key = ID, value = value, -Node) %>%
  filter(value) %>% #remove values that that show a point is NOT within a cell, this is the majority of values
  select(-value) %>%
  mutate(ID = as.integer(ID))

#join scores to nodes
Node_score_year <- left_join(Node_data_dictionary, score_by_year)

#create df of the sum of scores across all years for the voronoi matched df
score_across_years_voronoi <- Node_score_year %>%
  group_by(Node) %>%
  summarise(score = sum(score),
            counts = n())
#create df of sum of scores just by joining the original two dfs together
score_across_years_join <- left_join(node_locations,score_by_year) %>%
  group_by(Node) %>%
  summarise(score = sum(score),
            counts = n())

#Calculating the score using the two different methods does not produce the same result
score_diffs <- left_join(
  score_across_years_voronoi %>% select(Node, score_voronoi = score),
  score_across_years_join %>% select(Node, score_join = score)
) %>%
  mutate(diffs = score_voronoi-score_join)

1 个答案:

答案 0 :(得分:0)

我怀疑问题出在定义变量voronoi_intersection的行中。该变量中的矩阵不是我期望的直截了当的结构。