Skip to contents

Filters data spatially based on a spatial predicate

Usage

ddbs_filter(
  conn,
  x,
  y,
  name = NULL,
  predicate = "intersection",
  crs = NULL,
  crs_column = "crs_duckspatial",
  overwrite = FALSE
)

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

predicate

geometry predicate to use for filtering the data

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

Value

an sf object or TRUE (invisibly) for table creation

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

## filter countries touching argentina
ddbs_filter(conn, "countries", "argentina", predicate = "touches")
#>  Query successful
#> Simple feature collection with 5 features and 6 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -75.58129 ymin: -55.58443 xmax: -34.84846 ymax: 5.25818
#> Geodetic CRS:  WGS 84
#>   CNTR_ID NAME_ENGL ISO3_CODE                       CNTR_NAME FID       date
#> 1      BO   Bolivia       BOL Wuliwya-Volívia-Bulivya-Bolivia  BO 2021-01-01
#> 2      BR    Brazil       BRA                          Brasil  BR 2021-01-01
#> 3      CL     Chile       CHL                           Chile  CL 2021-01-01
#> 4      PY  Paraguay       PRY               Paraguay-Paraguái  PY 2021-01-01
#> 5      UY   Uruguay       URY                         Uruguay  UY 2021-01-01
#>                         geometry
#> 1 POLYGON ((-58.16346 -20.162...
#> 2 POLYGON ((-56.47737 1.94594...
#> 3 POLYGON ((-67.1775 -22.8046...
#> 4 POLYGON ((-58.16346 -20.162...
#> 5 POLYGON ((-53.37795 -33.750...
# }