View source: R/ddbs_as_spatial.R
| ddbs_as_points | R Documentation |
Converts a data frame with coordinate columns into spatial point geometries.
ddbs_as_points(
x,
coords = c("lon", "lat"),
crs = "EPSG:4326",
remove = TRUE,
na.fail = TRUE,
conn = NULL,
name = NULL,
mode = NULL,
overwrite = FALSE,
quiet = FALSE,
...
)
x |
Input spatial data. Can be:
Data is returned from this object. |
coords |
Character vector of length 2 specifying the names of the
longitude and latitude columns (or X and Y coordinates). Defaults to
|
crs |
Character or numeric. The Coordinate Reference System (CRS) of the
input coordinates. Can be specified as an EPSG code (e.g., |
remove |
Logical. If |
na.fail |
Logical. If |
conn |
A connection object to a DuckDB database. If |
name |
A character string of length one specifying the name of the table,
or a character string of length two specifying the schema and table
names. If |
mode |
Character. Controls the return type. Options:
Can be set globally via |
overwrite |
Boolean. whether to overwrite the existing table if it exists. Defaults
to |
quiet |
A logical value. If |
... |
Additional arguments. Currently supports |
Depends on the mode argument (or global preference set by ddbs_options):
duckspatial (default): A duckspatial_df (lazy spatial data frame) backed by dbplyr/DuckDB.
sf: An eagerly collected object in R memory, that will return the same data type as the
sf equivalent (e.g. sf or units vector).
When name is provided, the result is also written as a table or view in DuckDB and the function returns TRUE (invisibly).
## Not run:
## load packages
library(duckspatial)
## create sample data with coordinates
cities_df <- data.frame(
city = c("Buenos Aires", "Córdoba", "Rosario"),
lon = c(-58.3816, -64.1811, -60.6393),
lat = c(-34.6037, -31.4201, -32.9468),
population = c(3075000, 1391000, 1193605)
)
# option 1: convert data frame to sf object
cities_ddbs <- ddbs_as_points(cities_df)
# specify custom coordinate column names and keep them in output
cities_df2 <- data.frame(
city = c("Mendoza", "Tucumán"),
longitude = c(-68.8272, -65.2226),
latitude = c(-32.8895, -26.8241)
)
ddbs_as_points(cities_df2, coords = c("longitude", "latitude"), remove = FALSE)
## option 2: convert table in duckdb to spatial table
# create a duckdb connection and write data
conn <- duckspatial::ddbs_create_conn()
DBI::dbWriteTable(conn, "cities_tbl", cities_df, overwrite = TRUE)
# convert to spatial table in database
ddbs_as_points(
x = "cities_tbl",
conn = conn,
name = "cities_spatial",
overwrite = TRUE
)
# read the spatial table
ddbs_read_table(conn, "cities_spatial")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.