
Calculates the intersection of two geometries
ddbs_intersection.Rd
Calculates the intersection of two geometries, and return a sf
object
Usage
ddbs_intersection(
conn,
x,
y,
name = NULL,
crs = NULL,
crs_column = "crs_duckspatial",
overwrite = NULL
)
Arguments
- conn
a connection object to a DuckDB database
- x
a table with geometry column within the DuckDB database. Data is returned from this object
- y
a table with geometry column within the DuckDB database
- 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 it's NULL (the default), it will return the result as an
sf
object- crs
the coordinates reference system of the data. Specify if the data doesn't have crs_column, and you know the crs
- crs_column
a character string of length one specifying the column storing the CRS (created automatically by
ddbs_write_vector
). Set to NULL if absent- overwrite
whether to overwrite the existing table if it exists. Ignored when name is NULL
Examples
# \donttest{
## load packages
library(duckdb)
library(duckspatial)
library(sf)
## database setup
conn <- dbConnect(duckdb())
ddbs_install(conn)
#> ℹ spatial extension version <2905968> is already installed in this database
ddbs_load(conn)
#> ✔ Spatial extension loaded
## read data
countries_sf <- st_read(system.file("spatial/countries.geojson", package = "duckspatial"))
#> Reading layer `countries' from data source
#> `/home/runner/work/_temp/Library/duckspatial/spatial/countries.geojson'
#> using driver `GeoJSON'
#> Simple feature collection with 257 features and 6 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -178.9125 ymin: -89.9 xmax: 180 ymax: 83.65187
#> Geodetic CRS: WGS 84
argentina_sf <- st_read(system.file("spatial/argentina.geojson", package = "duckspatial"))
#> Reading layer `argentina' from data source
#> `/home/runner/work/_temp/Library/duckspatial/spatial/argentina.geojson'
#> using driver `GeoJSON'
#> Simple feature collection with 1 feature and 6 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -73.52455 ymin: -52.39755 xmax: -53.62409 ymax: -21.81793
#> Geodetic CRS: WGS 84
## store in duckdb
ddbs_write_vector(conn, countries_sf, "countries")
#> ✔ Table countries successfully imported
ddbs_write_vector(conn, argentina_sf, "argentina")
#> ✔ Table argentina successfully imported
## intersection
ddbs_intersection(conn, "countries", "argentina")
#> ✔ Query successful
#> Simple feature collection with 6 features and 6 fields
#> Geometry type: GEOMETRY
#> Dimension: XY
#> Bounding box: xmin: -73.52455 ymin: -52.39755 xmax: -53.62409 ymax: -21.81793
#> Geodetic CRS: WGS 84
#> CNTR_ID NAME_ENGL ISO3_CODE CNTR_NAME FID date
#> 1 AR Argentina ARG Argentina AR 2021-01-01
#> 2 BO Bolivia BOL Wuliwya-Volívia-Bulivya-Bolivia BO 2021-01-01
#> 3 BR Brazil BRA Brasil BR 2021-01-01
#> 4 CL Chile CHL Chile CL 2021-01-01
#> 5 PY Paraguay PRY Paraguay-Paraguái PY 2021-01-01
#> 6 UY Uruguay URY Uruguay UY 2021-01-01
#> geometry
#> 1 POLYGON ((-62.25779 -22.533...
#> 2 MULTILINESTRING ((-62.6452 ...
#> 3 MULTILINESTRING ((-57.61898...
#> 4 MULTILINESTRING ((-67.1775 ...
#> 5 MULTILINESTRING ((-54.5941 ...
#> 6 MULTILINESTRING ((-58.13027...
# }