Skip to content

Instantly share code, notes, and snippets.

@paleolimbot
Created April 12, 2020 00:39
Show Gist options
  • Save paleolimbot/004450a95092531e4146c1bd0a15c65b to your computer and use it in GitHub Desktop.
Save paleolimbot/004450a95092531e4146c1bd0a15c65b to your computer and use it in GitHub Desktop.
library(tidyr)
# remotes::install_github("tidyverse/dplyr")
library(dplyr)
# remotes::install_github("paleolimbot/geovctrs")
library(geovctrs)
nc <- geo_nc %>% select(NAME, geometry)
nc
# explode coordinates
nc %>%
group_by(NAME) %>%
summarise(geo_coordinates(geometry)) %>%
separate_xy(xy)
# calculate the bounding box
nc %>%
group_by(NAME) %>%
summarise(geo_coordinates(geometry)) %>%
separate_xy(xy) %>%
summarise(
xmin = min(x),
ymin = min(y),
xmax = max(x),
ymax = max(y)
) %>%
unite_rect("bbox", xmin, ymin, xmax, ymax)
# create a random line between two vertices
lines <- nc %>%
group_by(NAME) %>%
summarise(geo_coordinates(geometry)) %>%
group_by(NAME) %>%
sample_n(2) %>%
mutate(which = c("start", "end")) %>%
ungroup() %>%
select(NAME, which, xy) %>%
pivot_wider(names_from = which, values_from = xy) %>%
unite_segment("transect", start, end)
lines
geo_plot(nc, lwd = 0.7)
geo_plot_add(transects, col = "red")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment