sf_to_df: Convert between simple feature and data frame objects

View source: R/sf_to_df.R

sf_to_dfR Documentation

Convert between simple feature and data frame objects

Description

Helper function to convert a simple feature object to data frame by dropping geometry, converting geometry to well known text, or (if the geometry type is not POINT) getting coordinates for a centroid or point on surface. If an sfc object is provided,the "drop" geometry option is not supported.

Usage

sf_to_df(
  x,
  crs = 4326,
  coords = c("lon", "lat"),
  geometry = "centroid",
  keep_all = TRUE
)

df_to_sf(
  x,
  crs = NULL,
  coords = c("lon", "lat"),
  from_crs = 4326,
  into = NULL,
  sep = ",",
  rev = TRUE,
  remove_coords = FALSE,
  geo = FALSE,
  address = "address"
)

check_coords(x = NULL, coords = NULL, default = c("lon", "lat"), rev = FALSE)

has_coords(x, coords = NULL, value = TRUE)

Arguments

x

A sf or sfc object or a data frame with lat/lon coordinates in a single column or two separated columns.

crs

Cordinate reference system to return, Default: 4326 for sf_to_df and NULL for df_to_sf

coords

Coordinate columns for input dataframe or output sf object (if geometry is 'centroid' or 'point') Default: c("lon", "lat").

geometry

Type of geometry to include in data frame. options include "drop", "wkt", "centroid", "point", Default: 'centroid'.

keep_all

If FALSE, drop all columns other than those named in coords, Default: TRUE.

from_crs

For df_to_sf, coordinate reference system used by coordinates or well known text in data frame.

into

If coords is a single column name with both longitude and latitude, into is used as the names of the new columns that coords is separated into. Passed to tidyr::separate.

sep

If coords is a single column name with both longitude and latitude, sep is used as the separator between coordinate values. Passed to tidyr::separate.

rev

If TRUE, reverse c("lat", "lon") coords to c("lon", "lat"). check_coords only.

remove_coords

For df_to_sf, if TRUE, remove the coordinate columns after converting a data frame to simple feature object; defaults to FALSE.

geo

If TRUE, use address_to_sf to geocode address column; defaults to FALSE.

address

Address column name passed to tidygeocoder::geocode or tidygeocoder::geo

default

c("lon", "lat").

value

If TRUE, return the value of the coordinate column names. Used by has_coords.

Details

check_coords() is a helper function used by df_to_sf() to suggest the appropriate coordinate column names based on the column names in the provided data frame.

Value

sf_to_df() returns a data frame with geometry dropped or converted to wkt or coordinates for the centroid or point on surface; df_to_sf() returns a simple feature object with POINT geometry.

See Also

sf::st_coordinates()

ggspatial::df_spatial() sf::st_as_sf()

Examples

nc <- read_sf_path(system.file("shape/nc.shp", package = "sf"))

# Convert a sf object to a data frame
nc_df <- sf_to_df(nc)

# Convert a data frame to a sf object
df_to_sf(nc_df, coords = c("lon", "lat"), remove_coords = TRUE)

# If lon and lat values are present in a single column, use the into parameter
# to split the values back into separate columns
nc_df$xy <- paste(nc_df$lon, nc_df$lat, sep = ",")

df_to_sf(nc_df, coords = "xy", into = c("lon", "lat"))

elipousson/overedge documentation built on Aug. 13, 2022, 7:41 p.m.