if(requireNamespace("sf")) {
# 10 random line segments
# shared end vertices
.x <- runif(n = 11, min = 0, max = 100)
.y <- runif(n = 11, min = 0, max = 100)
m <- matrix(c(.x, .y), ncol = 2, byrow = TRUE)
# init LINESTRING geometries
a <- lapply(1:(nrow(m) - 1), function(i) {
.idx <- c(i, i+1)
geom <- sf::st_sfc(sf::st_linestring(m[.idx, ]))
a <- sf::st_sf(geom)
})
# flatten list -> 10 feature sf object
a <- do.call('rbind', a)
# line hashes
a$id <- generateLineHash(a, precision = 0)
# graphical check
plot(a, lwd = 2, key.width = lcm(4), axes = TRUE, las = 1)
# simulate empty geometry
a$geom[2] <- sf::st_sfc(sf::st_linestring())
# NA returned for empty geometry
generateLineHash(a, precision = 0)
}
Run the code above in your browser using DataLab