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
orsfc
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 toFALSE
.- geo
If
TRUE
, use address_to_sf to geocode address column; defaults toFALSE
.- 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.
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.
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.
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)
#> Simple feature collection with 100 features and 14 fields
#> Attribute-geometry relationship: 14 constant, 0 aggregate, 0 identity
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -84.05976 ymin: 34.07684 xmax: -75.80916 ymax: 36.49119
#> Geodetic CRS: WGS 84
#> # A tibble: 100 × 15
#> AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74
#> * <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.114 1.44 1825 1825 Ashe 37009 37009 5 1091 1 10
#> 2 0.061 1.23 1827 1827 Alle… 37005 37005 3 487 0 10
#> 3 0.143 1.63 1828 1828 Surry 37171 37171 86 3188 5 208
#> 4 0.07 2.97 1831 1831 Curr… 37053 37053 27 508 1 123
#> 5 0.153 2.21 1832 1832 Nort… 37131 37131 66 1421 9 1066
#> 6 0.097 1.67 1833 1833 Hert… 37091 37091 46 1452 7 954
#> 7 0.062 1.55 1834 1834 Camd… 37029 37029 15 286 0 115
#> 8 0.091 1.28 1835 1835 Gates 37073 37073 37 420 0 254
#> 9 0.118 1.42 1836 1836 Warr… 37185 37185 93 968 4 748
#> 10 0.124 1.43 1837 1837 Stok… 37169 37169 85 1612 1 160
#> # … with 90 more rows, and 4 more variables: BIR79 <dbl>, SID79 <dbl>,
#> # NWBIR79 <dbl>, geometry <POINT [°]>
# 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"))
#> Simple feature collection with 100 features and 16 fields
#> Attribute-geometry relationship: 16 constant, 0 aggregate, 0 identity
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -84.05976 ymin: 34.07684 xmax: -75.80916 ymax: 36.49119
#> Geodetic CRS: WGS 84
#> # A tibble: 100 × 17
#> AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74
#> * <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.114 1.44 1825 1825 Ashe 37009 37009 5 1091 1 10
#> 2 0.061 1.23 1827 1827 Alle… 37005 37005 3 487 0 10
#> 3 0.143 1.63 1828 1828 Surry 37171 37171 86 3188 5 208
#> 4 0.07 2.97 1831 1831 Curr… 37053 37053 27 508 1 123
#> 5 0.153 2.21 1832 1832 Nort… 37131 37131 66 1421 9 1066
#> 6 0.097 1.67 1833 1833 Hert… 37091 37091 46 1452 7 954
#> 7 0.062 1.55 1834 1834 Camd… 37029 37029 15 286 0 115
#> 8 0.091 1.28 1835 1835 Gates 37073 37073 37 420 0 254
#> 9 0.118 1.42 1836 1836 Warr… 37185 37185 93 968 4 748
#> 10 0.124 1.43 1837 1837 Stok… 37169 37169 85 1612 1 160
#> # … with 90 more rows, and 6 more variables: BIR79 <dbl>, SID79 <dbl>,
#> # NWBIR79 <dbl>, lon <dbl>, lat <dbl>, geometry <POINT [°]>