Skip to contents

Introduction

This vignette demonstrates how to use the dbSpatial package to create a DuckDB database with spatial points and polygons starting from various data sources.

Creating a DuckDB connection

# create db connection in memory
duckdb_conn = DBI::dbConnect(duckdb::duckdb(), ":memory:")

Reading in spatial data from various sources

From data.frames

# test data
test_data = data.frame(x = 1:10, y = 1:10, id = 1:10)

# df, tbl
# specify x and y column names to cast to a point geometry
a <- dbSpatial(conn = duckdb_conn,
               name = "test_points",
               value = test_data,
               x_colName = "x",
               y_colName = "y",
               overwrite = TRUE)
a
#> # Class:    dbSpatial 
#> # Source:   SQL [?? x 4]
#> # Database: DuckDB 1.4.3 [unknown@Linux 6.11.0-1018-azure:R 4.5.2/:memory:]
#>        x     y    id geom         
#>    <int> <int> <int> <chr>        
#>  1     1     1     1 POINT (1 1)  
#>  2     2     2     2 POINT (2 2)  
#>  3     3     3     3 POINT (3 3)  
#>  4     4     4     4 POINT (4 4)  
#>  5     5     5     5 POINT (5 5)  
#>  6     6     6     6 POINT (6 6)  
#>  7     7     7     7 POINT (7 7)  
#>  8     8     8     8 POINT (8 8)  
#>  9     9     9     9 POINT (9 9)  
#> 10    10    10    10 POINT (10 10)

From .csv file

# test data
test_data = data.frame(x = 1:10, y = 1:10, id = 1:10)

# write to file
write.csv(test_data, "test_data.csv", row.names = FALSE)

# load file in db
a <- dbSpatial(conn = duckdb_conn,
               name = "test_points",
               value = 'test_data.csv',
               x_colName = "x",
               y_colName = "y",
               overwrite = TRUE)
a
#> # Class:    dbSpatial 
#> # Source:   SQL [?? x 4]
#> # Database: DuckDB 1.4.3 [unknown@Linux 6.11.0-1018-azure:R 4.5.2/:memory:]
#>    x     y     id    geom         
#>    <chr> <chr> <chr> <chr>        
#>  1 1     1     1     POINT (1 1)  
#>  2 2     2     2     POINT (2 2)  
#>  3 3     3     3     POINT (3 3)  
#>  4 4     4     4     POINT (4 4)  
#>  5 5     5     5     POINT (5 5)  
#>  6 6     6     6     POINT (6 6)  
#>  7 7     7     7     POINT (7 7)  
#>  8 8     8     8     POINT (8 8)  
#>  9 9     9     9     POINT (9 9)  
#> 10 10    10    10    POINT (10 10)

From {terra} objects: SpatVector

# load terra package
library(terra)
#> terra 1.8.93

# Create a SpatVector from the data.frame
dummy_spatvector <- terra::vect(test_data, geom = c("x", "y"))

# Load SpatVector in db
dbSpatial(conn = duckdb_conn,
          name = "spatVector_proxy",
          value = dummy_spatvector,
          overwrite = TRUE)
#> # Class:    dbSpatial 
#> # Source:   SQL [?? x 2]
#> # Database: DuckDB 1.4.3 [unknown@Linux 6.11.0-1018-azure:R 4.5.2/:memory:]
#>       id geom         
#>    <int> <chr>        
#>  1     1 POINT (1 1)  
#>  2     2 POINT (2 2)  
#>  3     3 POINT (3 3)  
#>  4     4 POINT (4 4)  
#>  5     5 POINT (5 5)  
#>  6     6 POINT (6 6)  
#>  7     7 POINT (7 7)  
#>  8     8 POINT (8 8)  
#>  9     9 POINT (9 9)  
#> 10    10 POINT (10 10)