Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Created January 3, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jebyrnes/77a0efd00f7f5a1f142859ac0153f271 to your computer and use it in GitHub Desktop.
Save jebyrnes/77a0efd00f7f5a1f142859ac0153f271 to your computer and use it in GitHub Desktop.
Makes thresholded sf multipolygons from user classifications for floating forests with a sample data set
library(sf)
library(fasterize)
library(spex)
library(dplyr)
library(purrr)
library(ggplot2)
library(lwgeom)
test_set <- devtools::source_gist("c95701bd444cda3e342838fd9a090fb3",
filename = "test_set.R")$value
#make into a raster at the 10m scale to that it doesn't kill spex
test_raster <- fasterize(test_set %>% mutate(value = 1),
raster(test_set, res = 10),
field = "value", fun = "sum")
#plot the raster
plot(test_raster)
#polygonize by threshold
make_threshold_raster <- function(arast, threshold){
arast <- arast >= threshold
raster::values(arast)[!raster::values(arast)] <- NA
# raster::values(arast)[raster::values(arast)] <- threshold
arast
}
out_polys <- map(1:13, ~make_threshold_raster(test_raster, .x)) %>%
map(polygonize)
out_union <- map(out_polys, st_union)
op_mp <- out_union %>%
do.call(c, .) %>%
st_sf(threshold = 1:13, geometry = .) %>%
st_make_valid()
#plot the result
ggplot(op_mp) +
geom_sf() +
facet_wrap(~threshold)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment