Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Mar 13, 2024
1 parent 4785aab commit 0a42f00
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
69 changes: 69 additions & 0 deletions notebook/duckdb-example.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: "duckdb"
author: Carl Boettiger
date: "2024-03-06"
---

```{r message = FALSE}
library(duckdbfs)
library(dplyr)
library(sf)
```


```{r}
# SQL
pad <- open_dataset("https://data.source.coop/cboettig/pad-us-3/pad-us3-combined.parquet")
pad_meta <- duckdbfs::st_read_meta("https://data.source.coop/cboettig/pad-us-3/pad-us3-combined.fgb", tblname = "pad_meta")
pad_meta
```


```{r}
pad |>
filter(State_Nm == "CA") |>
group_by(FeatClass) |>
summarise(total_area = sum(SHAPE_Area),
n = n()) |>
collect()
```

```{r}
duckdbfs::load_spatial()
```


Reading in as a normal tibble, and then converting to a spatial object:

```{r}
ca_fee <- pad |>
filter(State_Nm == "CA", FeatClass == "Fee") |>
collect()
ca_fee |> st_as_sf(sf_column_name = "geometry", crs = pad_meta$wkt)
```

Similarly, any normal data frame can be coerced to a spatial `sf` object, e.g.:
```{r}
data.frame(lon = c(1,2), lat=c(0,0)) |> st_as_sf(coords = c("lon", "lat"), crs=4326)
```


```{r}
spatial_ex <- paste0("https://raw.githubusercontent.com/cboettig/duckdbfs/",
"main/inst/extdata/spatial-test.csv") |>
open_dataset(format = "csv")
spatial_ex |>
mutate(geometry = st_point(longitude, latitude)) |>
to_sf(crs = 4326)
```
```{r}
ca_fee <- pad |>
filter(State_Nm == "CA", FeatClass == "Fee") |>
group_by(Own_Type) |>
summarise(total = sum(SHAPE_Area)) |> head(1000) |> collect()
```
2 changes: 2 additions & 0 deletions notebook/fire-example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ library(gdalcubes)
url <- "https://34c031f8-c9fd-4018-8c5a-4159cdff6b0d-cdn-endpoint.azureedge.net/-/media/calfire-website/what-we-do/fire-resource-assessment-program---frap/gis-data/april-2023/fire221gdb.zip?rev=9e3e1e5e61e242d5b2994d666d72a91a&hash=F424990CD64BB7C4CF01C6CE211C0A59"
download.file(url, "fire221.gdb.zip", mode="wb")
unzip("fire221.gdb.zip")
```

Expand Down

0 comments on commit 0a42f00

Please sign in to comment.