lazysf

Delayed Read for GDAL Vector Data Sources

https://github.com/hypertidy/lazysf

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.6%) to scientific vocabulary

Keywords

spatial-gdal-sf-dbi
Last synced: 11 months ago · JSON representation

Repository

Delayed Read for GDAL Vector Data Sources

Basic Info
Statistics
  • Stars: 22
  • Watchers: 1
  • Forks: 2
  • Open Issues: 4
  • Releases: 0
Topics
spatial-gdal-sf-dbi
Created almost 6 years ago · Last pushed over 1 year ago
Metadata Files
Readme Changelog Code of conduct

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
library(dplyr)
library(sf)
```

# lazysf


[![CRAN status](https://www.r-pkg.org/badges/version/lazysf)](https://CRAN.R-project.org/package=lazysf)
[![](http://cranlogs.r-pkg.org/badges/last-month/lazysf?color=green)](https://cran.r-project.org/package=lazysf)


The goal of lazysf is to provide interactive delayed read of GDAL vector data sources. 

Note: very soon a release of [gdalraster](https://CRAN.R-project.org/package=gdalraster) will supersede what lazysf can
do and we recommend using that instead. 

Vector data sources, drawings (a.k.a. "shapefiles") are files or web services or databases that provide tables of data fields. These fields
may include spatial geometry data such as points, lines, polygons, and other planar types composed of paths of coordinates. 

lazysf uses the dplyr/dbplyr 'tbl_lazy' mechanism by providing a GDAL DBI-backend like many database packages in R.  The convenience function `lazysf()` provides a single-argument wrapper around the database-like workflows. 

See it in action!

```{r sf-action}
library(lazysf)
library(sf)
library(dplyr)

url <- "https://github.com/Nowosad/spData/raw/master/inst/shapes/NY8_bna_utm18.gpkg"
(x <- lazysf(url))
x %>% distinct(AREANAME) %>% arrange(AREANAME) 

plot(st_as_sf(x %>% 
                dplyr::filter(!(AREANAME %LIKE% "Ca%" | AREANAME %LIKE% "Bi%")) %>% 
                dplyr::select(AREANAME, geom)))
```

## Limitations

This is very largely format dependent, and by "format" we mean the *driver* as provided by GDAL.   

We make no claims about performance or convenience, it will be affected by your system and your sf installation - lazysf just takes you closer the GDAL capabilities. 

Performance can be excellent, and may be very competitive compared to reading an entire data source layer into memory. Really good drivers include ESRI Shapefile, Geopackage, PostgreSQL/PostGIS, MapInfo File, ESRI FileGDB, but there are dozens to choose from. 

A query on a CSV, GeoJSON, or KML file (local or remote) is entirely subject to the performance of the matching [GDAL driver](https://gdal.org/en/stable/drivers/vector/index.html).

* big text files will be slow, they aren't a format suitable for database-like access
* geometry is not automatic for real database formats, and will depend on the SQL used
* geometry *is* automatic for non-DB formats
* non-DB formats without a geometry column name will be called `_ogr_geometry_`, other non-DB formats like
ESRI's geodatabase have other names like `SHAPE`
* non-DB formats have access to special variable names and functions, listed on the [OGRSQL page](https://gdal.org/en/stable/user/ogr_sql_dialect.html). 

Real DBs don't have these special OGRSQL features, but they do have their own special syntax which for the most part can be sent straight through. 

When using dplyr verbs (`filter()`, `select()`, `mutate()`, `transmute()`,
`arrange()`, `left_join()`, ...) we are also subject to the rules of SQL
translation. There are no specific ones provided by lazysf but that might
change.

Wrappers around lazysf could provide more specific tools for particular formats. 

### Can't we just do this with sf's query argument?

Yes (actually that is what lazysf uses) but with sf alone you get a fully
materialized sf data frame, so you better get that query right first time!

With lazysf you get some control over intermediate steps, potentially expensive
queries will only be run for a preview of the data until you are ready to fetch
it.

## Installation

You can install the dev version of lazysf from [GitHub](https://github.com/hypertidy/lazysf) with:

``` r
# Enable this universe
options(repos = c(
    hypertidy = 'https://hypertidy.r-universe.dev',
    CRAN = 'https://cloud.r-project.org'))

# Install some packages
install.packages('lazysf')
```

## Example

This is a basic example. 

```{R basic}
library(lazysf)
f <- system.file("gpkg/nc.gpkg", package = "sf", mustWork = TRUE)

## specify only the data source
lazysf(f)

## specify the data source and a query to run
lazysf(f, query = "SELECT AREA, FIPS, geom FROM \"nc.gpkg\" WHERE AREA < 0.1")

## specify the data source and the table/layer to access
lazysf(f, layer = "nc.gpkg") %>% 
  dplyr::select(AREA, FIPS, geom) %>% 
  dplyr::filter(AREA < 0.1)


## above was a real database (Geopackage), now with an actual shapefile
shp <- lazysf(system.file("shape/nc.shp", package = "sf", mustWork = TRUE))
library(dplyr)
shp %>%
 filter(NAME %LIKE% 'A%') %>%
 mutate(abc = 1.3) %>%
 select(abc, NAME, `_ogr_geometry_`) %>%
 arrange(desc(NAME))  #%>% show_query()
```

Online sources can also work if your build of sf supports. 

```{r online}
# online sources can work
geojson <- file.path("https://raw.githubusercontent.com/SymbolixAU",
                      "geojsonsf/master/inst/examples/geo_melbourne.geojson")
lazysf(geojson)
```


Also works on PostgreSQL and many others as per GDAL vector driver support.   


To create a connection string for [GDAL for PostgreSQL](https://gdal.org/en/stable/drivers/vector/pg.html) use something like

```R
DSN <- glue::glue("PG:host='{host}' dbname='{dbname}' user='{user}' password='{password}'")

dbConnect(SFSQL(), DSN)
```

but the same can be done with generic DBI and (for example) the [Rpostgres package](https://rpostgres.r-dbi.org/). With `SFSQL()` we just know that it's executed by GDAL (via sf). 


## Some drivers are related

Note that GDAL drivers can be confusing, and it can be important to see the behaviours GDAL will provide by default. Here see that we can read from a Geopackage file but not as it was intended. We
have used the driver-prefix to make GDAL choose its SQLite driver rather than the Geopackage driver. 

```{r Geopackage-SQLite}
library(lazysf)
gpkgfile <- system.file("gpkg/nc.gpkg", package = "sf", mustWork = TRUE)
lazysf(glue::glue("SQLite:{gpkgfile}"))
```

That's not very spatial, but we can dig in to find out what else is there. 



  
---
## Code of Conduct

Please note that the lazysf project is released with a [Contributor Code of
Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By
contributing to this project, you agree to abide by its terms.

Owner

  • Name: hypertidy
  • Login: hypertidy
  • Kind: organization
  • Location: Hobart, Australia

[ ... ]

GitHub Events

Total
  • Push event: 3
Last Year
  • Push event: 3

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 52
  • Total Committers: 2
  • Avg Commits per committer: 26.0
  • Development Distribution Score (DDS): 0.019
Past Year
  • Commits: 3
  • Committers: 1
  • Avg Commits per committer: 3.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Michael Sumner m****r@g****m 51
olivroy 5****y 1

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 7
  • Total pull requests: 1
  • Average time to close issues: 17 days
  • Average time to close pull requests: about 4 hours
  • Total issue authors: 3
  • Total pull request authors: 1
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • mdsumner (5)
  • lorenzwalthert (1)
  • latot (1)
Pull Request Authors
  • olivroy (2)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 247 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 1
  • Total versions: 2
  • Total maintainers: 1
cran.r-project.org: lazysf

Delayed Read for 'GDAL' Vector Data Sources

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 247 Last month
Rankings
Stargazers count: 12.2%
Forks count: 21.0%
Dependent repos count: 24.0%
Average: 28.1%
Dependent packages count: 28.8%
Downloads: 54.4%
Maintainers (1)
Last synced: 11 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • actions/upload-artifact main composite
  • r-lib/actions/setup-pandoc master composite
  • r-lib/actions/setup-r master composite
.github/workflows/pkgdown.yaml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc master composite
  • r-lib/actions/setup-r master composite
DESCRIPTION cran
  • DBI * imports
  • dbplyr * imports
  • dplyr * imports
  • magrittr * imports
  • methods * imports
  • sf >= 0.7 imports
  • tibble * imports
  • knitr * suggests
  • rmarkdown * suggests
.github/workflows/rhub.yaml actions
  • r-hub/actions/checkout v1 composite
  • r-hub/actions/platform-info v1 composite
  • r-hub/actions/run-check v1 composite
  • r-hub/actions/setup v1 composite
  • r-hub/actions/setup-deps v1 composite
  • r-hub/actions/setup-r v1 composite