-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from cboettig/spatial-read
support st_read() in open_dataset()
- Loading branch information
Showing
11 changed files
with
181 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: duckdbfs | ||
Title: High Performance Remote File System Access Using 'duckdb' | ||
Version: 0.0.3 | ||
Version: 0.0.3.99 | ||
Authors@R: | ||
person("Carl", "Boettiger", , "[email protected]", c("aut", "cre"), | ||
comment = c(ORCID = "0000-0002-1642-628X")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
library(duckdbfs) | ||
library(dplyr) | ||
library(DBI) | ||
|
||
#load_spatial() | ||
#con <- cached_connection() | ||
|
||
devtools::install_github("cboettig/duckdbfs@spatial-read") | ||
countries <- open_dataset("/vsicurl/https://github.com/cboettig/duckdbfs/raw/spatial-read/inst/extdata/world.gpkg", | ||
format = "sf", tblname = "countries") | ||
|
||
cities <- open_dataset("/vsicurl/https://github.com/cboettig/duckdbfs/raw/spatial-read/inst/extdata/metro.fgb", | ||
format = "sf", tblname = "cities") | ||
|
||
## We can count number of cities in each country with a bit of SQL | ||
x <- DBI::dbGetQuery(con, " | ||
SELECT countries.iso_a3, count(kbas.geom) AS total | ||
FROM countries | ||
LEFT JOIN kbas ON st_contains(countries.geom,kbas.geom) | ||
GROUP BY countries.iso_a3; | ||
") | ||
|
||
# in dplyr this could be nice and pretty, but `join_by` refuses the syntax | ||
countries |> | ||
left_join(cities, join_by(st_contains(geom, geom))) |> | ||
count(iso_a3, sort=TRUE) | ||
|
||
|
||
# other dplyr functions have no difficulty passing on these arguments: | ||
melbourne <- st_point(c(144.9633, -37.814)) |> st_as_text() | ||
countries |> filter(st_contains(geom, ST_GeomFromText({melbourne}))) | ||
|
||
|
||
# Aside: left_join() without count() looks like this in SQL .. much more verbose than dplyr | ||
x <- DBI::dbGetQuery(con," | ||
SELECT countries.iso_a3, cities.geom, countries.geom AS geometry | ||
FROM countries | ||
LEFT JOIN cities | ||
ON st_contains(countries.geom, cities.geom) | ||
") |> as_tibble() | ||
|
||
|
||
|
||
|
||
|
||
|
||
## accessing secure data with credentials | ||
|
||
KBAs <- "/vsis3/biodiversity/KBAsGlobal_2023_March_01_POL.shp" | ||
kba_pts <- "/vsis3/biodiversity/KBAsGlobal_2023_March_01_PNT.shp" | ||
Sys.setenv("AWS_ACCESS_KEY_ID"=Sys.getenv("NVME_KEY")) | ||
Sys.setenv("AWS_SECRET_ACCESS_KEY"=Sys.getenv("NVME_SECRET")) | ||
Sys.setenv("AWS_S3_ENDPOINT"="minio.carlboettiger.info") | ||
Sys.setenv("AWS_VIRTUAL_HOSTING"=FALSE) | ||
x <- sf::read_sf(kba_pts) | ||
kbas <- open_dataset(kba_pts, format="sf", tblname="kbas") | ||
|
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters