Last active
October 18, 2017 06:08
-
-
Save pierreroudier/d68425dbc150783b3802db648a8e3f12 to your computer and use it in GitHub Desktop.
Converting a data.frame into a (MULTIPOLYGON) sf object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
This seems to work
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...