﻿ 如何将质心(标记)的数据分配给它所属的voronoi / thiessen多边形？ (R) - 代码日志

#### 如何将质心(标记)的数据分配给它所属的voronoi / thiessen多边形？ (R)

``````library(leaflet);    library(rgeos)
library(rgdal);    library(spatstat)
library(maptools)

fw <- subset(starbucks, City == 'Fort Worth')
coords <- cbind(fw\$Longitude, fw\$Latitude)
## Spatial points w/the WGS84 datum
sp_fw <- SpatialPointsDataFrame(coords = coords, data = fw,
proj4string = CRS("+proj=longlat +datum=WGS84"))
sp_fw_proj <- spTransform(sp_fw, CRS("+init=epsg:26914"))
fw_coords <- sp_fw_proj@coords
## Create the window for the polygons
window <- owin(range(fw_coords[,1]), range(fw_coords[,2]))
## Create the polygons
d <- dirichlet(as.ppp(fw_coords, window))
## Convert to a SpatialPolygonsDataFrame and calculate an "area" field.
dsp <- as(d, "SpatialPolygons")
dsp_df <- SpatialPolygonsDataFrame(dsp,
data = data.frame(id =  1:length(dsp@polygons)))
proj4string(dsp_df) <- CRS("+init=epsg:26914")
dsp_df\$area <- round((gArea(dsp_df, byid = TRUE) / 1000000), 1)
dsp_xy <- spTransform(dsp_df, CRS("+proj=longlat +datum=WGS84"))
## Map it!
leaflet() %>%
lat = ~ Latitude,
lng = ~ Longitude,
popup = fw\$Name) %>%
color = "green",
fill = "green",
popup = paste0("Area: ",
as.character(dsp_xy\$area),
" square km")) %>%
``````

``````library(leaflet)
library(sf)
library(dismo)
library(sp)

set.seed(1983)

# Get some sample data

long <- sample(seq(-118.4, -118.2, 0.001), 50, replace = TRUE)

lat <- sample(seq(33.9, 34.1, 0.001), 50, replace = TRUE)

type <- sample(c("Starbucks", "Dunkin"), 50, replace = TRUE)
``````

``````points <- data.frame(long = long, lat = lat, type = type) %>%
st_as_sf(crs = 4326, coords = c("long", "lat"))

plot(points)
``````

``````polys <- points %>%
as("Spatial") %>%
voronoi() %>%
st_as_sf() %>%
st_set_crs(., 4326)

plot(polys)
``````

``````pal <- colorFactor(c("purple", "green"), polys\$type)

polys %>%
leaflet() %>%