Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mdsumner/RGDALSQL

DBI GDAL
https://github.com/mdsumner/RGDALSQL

Last synced: about 2 months ago
JSON representation

DBI GDAL

Awesome Lists containing this project

README

        

---
output: github_document
editor_options:
chunk_output_type: console
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```

[![Travis-CI Build Status](https://travis-ci.org/mdsumner/RGDALSQL.svg?branch=master)](https://travis-ci.org/mdsumner/RGDALSQL)
[![Coverage Status](https://img.shields.io/codecov/c/github/mdsumner/RGDALSQL/master.svg)](https://codecov.io/github/mdsumner/RGDALSQL?branch=master)

## RGDALSQL

WIP

```{r}
library(RGDALSQL)
f = system.file("extdata/continents", package = "RGDALSQL")
db <- dbConnect(RGDALSQL::GDALSQL(), f)
dbSendQuery(db, "SELECT * FROM continent WHERE FID < 1")

res <- dbSendQuery(db, "SELECT * FROM continent WHERE continent LIKE '%ca'")
dbFetch(res)

(res <- dbReadTable(db, "continent"))

dplyr::tbl(db, "continent")
```

## Limitations

* currently read-only, so no temporary tables for `compute()`
* no temporary tables in non-DB drivers (i.e. GPKG is ok, SHP is not)
* no sub-queryies in non-DB drivers (i.e. no collapse for SHP or GDB)

filter, arrange, summarize, transmute, mutate, ok but cannot be chained for nested sub-queries

```{r}
library(dplyr)
tbl(db, "continent") %>% dplyr::filter(continent == "Australia")

tbl(db, "continent") %>% dplyr::filter(continent %in% c("Australia", "Antarctica")) %>% collect()
```

Try OSM PBF.

```{r, eval=FALSE}
# wget https://download.geofabrik.de/europe/albania-latest.osm.pbf
f <- fs::path_expand("~/albania-latest.osm.pbf")
pbf <- dbConnect(RGDALSQL::GDALSQL(),f)
## we have to use a normalized path
## (vapour doesn't do this yet, but GDALSQL will do it *when connecting*,
## currently maintains the input DSN)
pbf
# db_list_tables(pbf)

tbl(pbf, "points")
# Source: table [?? x 11]
# Database: GDALSQLConnection
osm_id name barrier highway ref address is_in place man_made other_tags `_ogr_geometry_`

1 154606… "" "" "traffic… "" "" "" "" "" "\"crossing\"=>\"traffic_signals\",\… \"uncontrolled\"" \"traffic_signals\",\… \"uncontrolled\"" \"traffic_signals\",\… \"uncontrolled\",\"su… \"peak\"" \"peak\"" \"peak\",\"wikidata\"=… \"peak\",\"prominence\… % filter(osm_id == "53292") %>% select(`_ogr_geometry_`, type)
# Source: lazy query [?? x 2]
# Database: GDALSQLConnection
type `_ogr_geometry_`

1 boundary
```

```{r}
f <- "inst/extdata/shapes.gpkg"
conn <- dbConnect(RGDALSQL::GDALSQL(),f)
conn
dbListTables(conn)

x <- dbSendQuery(conn, "SELECT * FROM sids WHERE SID74 < 10")

tbl(conn, "sids") %>%
arrange(desc(AREA)) %>%
transmute(a = AREA *8, geom, AREA) %>%
filter(a > 1.62) %>%
show_query()

## with SHP we can't do subquery
f <- system.file("shape/nc.shp", package = "sf")
conn <- dbConnect(GDALSQL(), f)
## but the special variables are there
tbl(conn, "nc") %>% arrange(OGR_GEOM_AREA)
tbl(conn, "nc") %>% arrange(OGR_GEOM_AREA, FID) %>% show_query()

## FAILS because subquery
tbl(conn, "nc") %>% mutate(aa = AREA) %>% transmute(a1 = FID) %>% show_query()

## ok because a single statement
tbl(conn, "nc") %>% transmute(a1 = FID, aa = AREA) %>% collect()

```