Skip to content

Instantly share code, notes, and snippets.

@dholstius
Last active August 29, 2015 14:17
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 dholstius/3c50a7ebe5c971382ff1 to your computer and use it in GitHub Desktop.
Save dholstius/3c50a7ebe5c971382ff1 to your computer and use it in GitHub Desktop.
Row-wise application of rgeos::readWKT
library(rgeos)
library(dplyr)
library(magrittr)
n_polygons <- 5
n_vertices_each <- 3
rand_coords <- function (n)
sprintf("%0.1f %0.1f", rnorm(n), rnorm(n))
wkt_tbl <- (n_polygons * n_vertices_each) %>%
rand_coords() %>%
matrix(nrow = n_polygons, ncol = n_vertices_each) %>%
apply(1, . %>% append(first(.)) %>% str_join(collapse = ",")) %>%
str_join("POLYGON((", ., "))") %>%
data_frame(id = LETTERS[1:n_polygons], wkt_repr = .)
sp_obj <- wkt_tbl %$%
mapply(rgeos::readWKT, id = id, text = wkt_repr) %>%
do.call(sp::rbind.SpatialPolygons, .)
plot(sp_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment