as_frame: Turn a spatial object into a frame

View source: R/frames.R

as_frameR Documentation

Turn a spatial object into a frame

Description

Converts an sf object (points or polygons) into the plain data frame that travel_matrix() and select_units() expect: a column unit, the coordinates lat/lon (centroids for polygons, in WGS 84) and, for polygons, their area in square kilometres, next to the other columns of the object. Nothing spatial is kept: use as_sf() to put the results back on the geometries.

Usage

as_frame(x, unit = NULL)

Arguments

x

An sf object (the sf package must be installed), or a data frame returned unchanged.

unit

Name of the column with the unit names; made from the row numbers when NULL and no unit column exists.

Value

A tibble with unit, lat, lon, area (polygons only) and the attribute columns.

Examples

if (requireNamespace("sf", quietly = TRUE)) {
  sq <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(0, 1), c(1, 1), c(1, 0), c(0, 0)))),
    crs = 4326
  )
  cells <- sf::st_sf(crop = 3, geometry = sf::st_make_grid(sq, n = 3))
  as_frame(cells)
}

fieldopt documentation built on Oct. 22, 2026, 1:07 a.m.