Given a large spatial database of trees (600,000) find the tree that within radius r encircles the greatest variety of types of trees.
Looking at an answer by andyteucher I've simulated some tree data below using random trees with types A to I. I've then used buffer to draw a circle around each tree and intersect that with all the trees. Not sure what's next or if this direction is appropriate.
library(sf)
library(tmap)
library(tidyverse)
library(leaflet)
library(stringi)
mean_lat <- 43.9
sd_lat <- 0.1
mean_long <- -79.384293
sd_long <- 0.1
set.seed(42)
trees <- data.frame(lat = rnorm(500, mean = mean_lat, sd = sd_lat),
long = rnorm(500, mean = mean_long, sd = sd_long))
trees <- mutate(trees, tree_type = stri_rand_strings(500, 1, pattern = "[A-I]"))
radius = 500
# Convert to sf, set the crs to EPSG:4326 (lat/long),
# and transform CRS to Lambert conformal conic projection - North America
tree_sf <- st_as_sf(trees, coords = c("long", "lat"), crs = 4326) %>%
st_transform(102009)
# Buffer circles by 100m
tree_circles <- st_buffer(tree_sf, dist = radius)
enclosed_trees <- st_intersection(tree_sf, tree_circles)
# Transform back to 4326 to make Leaflet happy
tree_circles <- st_transform(tree_circles, 4326)
enclosed_trees <- st_transform(enclosed_trees,4326)
leaflet() %>%
addTiles() %>%
addPolygons(data = tree_circles) %>%
addCircles(data = enclosed_trees)