Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Add Comment
Please, Sign In to add comment