Guest User

Untitled

a guest
May 27th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. library(sf)
  2. library(tmap)
  3. library(tidyverse)
  4. library(leaflet)
  5. library(stringi)
  6.  
  7. mean_lat <- 43.9
  8. sd_lat <- 0.1
  9. mean_long <- -79.384293
  10. sd_long <- 0.1
  11.  
  12. set.seed(42)
  13. trees <- data.frame(lat = rnorm(500, mean = mean_lat, sd = sd_lat),
  14. long = rnorm(500, mean = mean_long, sd = sd_long))
  15.  
  16. trees <- mutate(trees, tree_type = stri_rand_strings(500, 1, pattern = "[A-I]"))
  17.  
  18. radius = 500
  19.  
  20. # Convert to sf, set the crs to EPSG:4326 (lat/long),
  21. # and transform CRS to Lambert conformal conic projection - North America
  22. tree_sf <- st_as_sf(trees, coords = c("long", "lat"), crs = 4326) %>%
  23. st_transform(102009)
  24.  
  25. # Buffer circles by 100m
  26. tree_circles <- st_buffer(tree_sf, dist = radius)
  27. enclosed_trees <- st_intersection(tree_sf, tree_circles)
  28.  
  29. # Transform back to 4326 to make Leaflet happy
  30. tree_circles <- st_transform(tree_circles, 4326)
  31. enclosed_trees <- st_transform(enclosed_trees,4326)
  32.  
  33. leaflet() %>%
  34. addTiles() %>%
  35. addPolygons(data = tree_circles) %>%
  36. addCircles(data = enclosed_trees)
Add Comment
Please, Sign In to add comment