Learn R Programming

mapycusmaximus (version 1.0.7)

lines_from_sf: Extract Line Coordinates from sf Objects

Description

Converts sf line or multiline geometries into a list structure containing coordinate arrays, suitable for serialization to JSON or use in JavaScript visualizations.

Usage

lines_from_sf(sf_obj, id_col = NULL)

Value

A list of lists, each containing:

  • id: Character identifier for the line

  • coords: List of [x, y] coordinate pairs representing the line vertices in sequence

Arguments

sf_obj

An sf or sfc object containing LINESTRING or MULTILINESTRING geometries.

id_col

Character. Optional column name to use as line IDs. If NULL, IDs are generated as "ln-1", "ln-2", etc. (default = NULL).

Details

This function prepares line geometries for client-side rendering. Multilinestrings are handled by extracting all coordinate points in order, which may or may not be appropriate depending on the use case.

See Also

polygons_from_sf(), points_from_sf(), shiny_fisheye()

Examples

Run this code
if (FALSE) {
library(sf)

# Create a simple linestring
line <- st_linestring(matrix(c(0,0, 1,1, 2,0), ncol = 2, byrow = TRUE))
sf_obj <- st_sf(id = "route1", geometry = st_sfc(line))

# Extract coordinates
coords <- lines_from_sf(sf_obj, id_col = "id")
str(coords)
}

Run the code above in your browser using DataLab