Learn R Programming

mapycusmaximus (version 1.0.7)

polygons_from_sf: Extract Polygon Coordinates from sf Objects

Description

Converts sf polygon or multipolygon geometries into a list structure containing coordinate arrays, suitable for serialization to JSON or use in JavaScript visualizations. Preserves both exterior rings and holes.

Usage

polygons_from_sf(sf_obj, id_col = NULL)

Value

A list of lists, each containing:

  • id: Character identifier for the polygon

  • rings: List of coordinate rings, where each ring is a list of [x, y] coordinate pairs. The first ring is the exterior boundary; subsequent rings (if present) are holes.

Arguments

sf_obj

An sf or sfc object containing POLYGON or MULTIPOLYGON geometries.

id_col

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

Details

This function is primarily used to prepare spatial data for client-side rendering in web applications. Each polygon may contain multiple rings (exterior + holes), and multipolygons are decomposed into separate ring lists.

The output format is compatible with JavaScript mapping libraries and SVG path generation.

See Also

lines_from_sf(), points_from_sf(), shiny_fisheye()

Examples

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

# Create a simple polygon
poly <- st_polygon(list(
  matrix(c(0,0, 1,0, 1,1, 0,1, 0,0), ncol = 2, byrow = TRUE)
))
sf_obj <- st_sf(id = "test", geometry = st_sfc(poly))

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

Run the code above in your browser using DataLab