| sf_to_df | R Documentation |
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.
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)
x |
A |
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 |
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, |
sep |
If coords is a single column name with both longitude and
latitude, |
rev |
If |
remove_coords |
For df_to_sf, if |
geo |
If |
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. |
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.
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.
sf::st_coordinates()
ggspatial::df_spatial()
sf::st_as_sf()
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.