Skip to content

Instantly share code, notes, and snippets.

@pierreroudier
Last active October 18, 2017 06:08
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 pierreroudier/d68425dbc150783b3802db648a8e3f12 to your computer and use it in GitHub Desktop.
Save pierreroudier/d68425dbc150783b3802db648a8e3f12 to your computer and use it in GitHub Desktop.
Converting a data.frame into a (MULTIPOLYGON) sf object
library(sf)
library(dplyr)
# A function that closes the polygons if
# the first row of coordinates is different than
# the last rows of coordinates
add_first_row <- function(x) {
if (!identical(
as.numeric(x[1,]),
as.numeric(x[nrow(x),])
)
) {
res <- rbind(x, x[1,])
} else {
res <- x
}
res
}
# Converts polygon coordinates into
# simple features
coords_to_poly <- function(coords) {
coords %>%
as.matrix %>%
list %>%
st_polygon %>%
st_sfc
}
# Test set
pdf <- structure(
list(
x = c(0L, 0L, 1L, 1L, 1L, 1L, 2L, 2L),
y = c(0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L),
id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L)
),
.Names = c("x", "y", "id"),
class = "data.frame",
row.names = c(NA, -8L)
)
pdf %>%
# id gives the polgon identifier in the original data.frame
group_by(id) %>%
# check the polygons are closed
add_first_row %>%
# create the geometry column
summarise(
geom = coords_to_poly(.)
) %>%
# converts to sf
st_sf(crs = 4326)
@tim-salabim
Copy link

tim-salabim commented Oct 18, 2017

This seems to work

tst = split(pdf, pdf$id)

pol = do.call(c, lapply(seq(tst), function(i) {
  tmp = coords_to_poly(add_first_row(tst[[i]]))
  st_crs(tmp) = 4326
  return(tmp)
}))

Note, that you will still need to take care of the polygon dimensions, as for now they are "XYZ" where it seems that the id is used as the "Z" value...

@pierreroudier
Copy link
Author

@tim-salabim yes it is XYZ because I would need to explicitely pass the xy coordanmes in coords_to_poly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment