Skip to content

Commit a5c4d9f

Browse files
author
Mike Johnson
committed
initial refactor
0 parents  commit a5c4d9f

135 files changed

Lines changed: 37648 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.Rbuildignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
^LICENSE\.md$
2+
^.*\.Rproj$
3+
^\.Rproj\.user$
4+
^\.github$
5+
^_pkgdown\.yaml$

.github/workflows/pkgdown.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
4+
on:
5+
push:
6+
branches: [main, master]
7+
pull_request:
8+
branches: [main, master]
9+
release:
10+
types: [published]
11+
workflow_dispatch:
12+
13+
name: Website
14+
15+
jobs:
16+
pkgdown:
17+
runs-on: ubuntu-latest
18+
# Only restrict concurrency for non-PR jobs
19+
concurrency:
20+
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
21+
env:
22+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
23+
permissions:
24+
contents: write
25+
steps:
26+
- uses: actions/checkout@v3
27+
28+
- uses: r-lib/actions/setup-pandoc@v2
29+
30+
- uses: r-lib/actions/setup-r@v2
31+
with:
32+
use-public-rspm: true
33+
34+
- uses: r-lib/actions/setup-r-dependencies@v2
35+
with:
36+
extra-packages: any::pkgdown, local::.
37+
needs: website
38+
39+
- name: Build site
40+
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
41+
shell: Rscript {0}
42+
43+
- name: Deploy to GitHub pages
44+
if: github.event_name != 'pull_request'
45+
uses: JamesIves/github-pages-deploy-action@v4.4.1
46+
with:
47+
clean: false
48+
branch: gh-pages
49+
folder: docs

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
.DS_Store

DESCRIPTION

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Package: hfutils
2+
Type: Package
3+
Title: hfutils
4+
Description: Core hydrofabric manipulation utilities.
5+
Version: 0.3.3
6+
Authors@R:
7+
c(person(given = "Mike",
8+
family = "Johnson",
9+
role = c("aut", "cre"),
10+
email = "mjohnson@lynker.com"),
11+
person(given = "Justin", family = "Singh-Mohudpur",
12+
role = c("aut")))
13+
Maintainer: Mike Johnson <mjohnson@lynker.com>
14+
BugReports: https://github.com/lynker-spatial/hfutils
15+
URL: https://github.com/lynker-spatial/hfutils
16+
Depends:
17+
R (>= 4.2)
18+
Imports:
19+
DBI,
20+
dbplyr,
21+
dplyr,
22+
glue,
23+
httr,
24+
igraph,
25+
methods,
26+
sf,
27+
stats,
28+
utils
29+
Suggests:
30+
testthat
31+
License: GPL (>= 3)
32+
Encoding: UTF-8
33+
LazyData: true
34+
RoxygenNote: 7.3.2
35+
Config/testthat/edition: 3
36+
Config/testthat/parallel: true
37+
Collate:
38+
'OGRSQL.R'
39+
'OGRSQLConnection.R'
40+
'OGRSQLDriver.R'
41+
'OGRSQLResult.R'
42+
'hfutils-package.R'
43+
'network_properties.R'
44+
'sf_ogr.R'
45+
'zzz.R'

LICENSE.md

Lines changed: 595 additions & 0 deletions
Large diffs are not rendered by default.

NAMESPACE

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
S3method(as_ogr,OGRSQLConnection)
4+
S3method(as_ogr,character)
5+
S3method(dbplyr_edition,OGRSQLConnection)
6+
S3method(st_as_sf,tbl_OGRSQLConnection)
7+
export(OGRSQL)
8+
export(accumulate_downstream)
9+
export(as_ogr)
10+
export(collect)
11+
export(st_as_sf)
12+
exportClasses(OGRSQLConnection)
13+
exportClasses(OGRSQLDriver)
14+
exportClasses(OGRSQLResult)
15+
exportMethods(dbClearResult)
16+
exportMethods(dbConnect)
17+
exportMethods(dbDataType)
18+
exportMethods(dbDisconnect)
19+
exportMethods(dbExistsTable)
20+
exportMethods(dbFetch)
21+
exportMethods(dbGetInfo)
22+
exportMethods(dbHasCompleted)
23+
exportMethods(dbIsValid)
24+
exportMethods(dbListTables)
25+
exportMethods(dbReadTable)
26+
exportMethods(dbSendQuery)
27+
exportMethods(dbUnloadDriver)
28+
exportMethods(show)
29+
importFrom(DBI,dbClearResult)
30+
importFrom(DBI,dbConnect)
31+
importFrom(DBI,dbDataType)
32+
importFrom(DBI,dbDisconnect)
33+
importFrom(DBI,dbExistsTable)
34+
importFrom(DBI,dbFetch)
35+
importFrom(DBI,dbGetInfo)
36+
importFrom(DBI,dbHasCompleted)
37+
importFrom(DBI,dbIsValid)
38+
importFrom(DBI,dbListTables)
39+
importFrom(DBI,dbReadTable)
40+
importFrom(DBI,dbSendQuery)
41+
importFrom(DBI,dbUnloadDriver)
42+
importFrom(dbplyr,dbplyr_edition)
43+
importFrom(dbplyr,sql)
44+
importFrom(dplyr,`%>%`)
45+
importFrom(dplyr,any_of)
46+
importFrom(dplyr,collect)
47+
importFrom(dplyr,distinct)
48+
importFrom(dplyr,everything)
49+
importFrom(dplyr,filter)
50+
importFrom(dplyr,if_any)
51+
importFrom(dplyr,mutate)
52+
importFrom(dplyr,pull)
53+
importFrom(dplyr,rename)
54+
importFrom(dplyr,select)
55+
importFrom(dplyr,tbl)
56+
importFrom(glue,glue)
57+
importFrom(httr,GET)
58+
importFrom(httr,progress)
59+
importFrom(httr,write_disk)
60+
importFrom(igraph,as_ids)
61+
importFrom(igraph,graph_from_edgelist)
62+
importFrom(igraph,is_dag)
63+
importFrom(igraph,topo_sort)
64+
importFrom(methods,callNextMethod)
65+
importFrom(methods,new)
66+
importFrom(methods,setClass)
67+
importFrom(methods,setMethod)
68+
importFrom(methods,setOldClass)
69+
importFrom(methods,show)
70+
importFrom(sf,sf_extSoftVersion)
71+
importFrom(sf,st_as_sf)
72+
importFrom(sf,st_layers)
73+
importFrom(utils,packageVersion)

R/OGRSQL.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# OGRSQLDriver.R -------------------------------------------------------------
2+
3+
# OGRSQLConnection.R --------------------------------------------------------
4+
5+
# OGRSQLResult.R ------------------------------------------------------------
6+
7+
# sf_ogr.R ------------------------------------------------------------------

R/OGRSQLConnection.R

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#' Class OGRSQLConnection (and methods)
2+
#'
3+
#' OGRSQLConnection objects are created by passing [OGRSQL()] as first
4+
#' argument to [DBI::dbConnect()].
5+
#' They are a superclass of the [DBIConnection-class] class.
6+
#' @keywords internal
7+
#' @export
8+
setClass(
9+
"OGRSQLConnection",
10+
contains = "DBIConnection",
11+
slots = list(DSN = "character",
12+
readonly = "logical")
13+
)
14+
15+
#' @rdname OGRSQLConnection-class
16+
#' @export
17+
18+
setMethod("show", "OGRSQLConnection", function(object) {
19+
cat("<OGRSQLConnection>\n")
20+
tables <- DBI::dbListTables(object)
21+
dsn <- object@DSN
22+
if (grepl("pass", dsn, ignore.case = TRUE) ||
23+
grepl("user", dsn, ignore.case = TRUE)) {
24+
dsn <- paste0(strsplit(dsn, "\\s")[[1L]][1L], "...")
25+
}
26+
cat("\tDSN: ", dsn, "\n", sep = "")
27+
cat("tables: ", paste(tables, collapse = ", "), "\n", sep = "")
28+
})
29+
30+
#' @rdname OGRSQLConnection-class
31+
#' @export
32+
33+
setMethod("dbSendQuery", "OGRSQLConnection", function(conn, statement, ...) {
34+
## quiet and fake layer because we aren't using layer = (it's in the query)
35+
args <- list(...)
36+
args$dsn <- conn@DSN
37+
args$layer <- "<fake layer>"
38+
args$query <- statement
39+
args$quiet <- TRUE
40+
args$as_tibble <- TRUE
41+
#browser()
42+
qu <- as.character(args$query)
43+
44+
if (grepl("AS.*q", qu) &&
45+
grepl("WHERE \\(0 = 1)", qu)) {
46+
## workaround for non-DB sources
47+
args$query <- dbplyr::sql(gsub("WHERE \\(0 = 1)", "LIMIT 0", qu))
48+
}
49+
op <- options(warn = -1)
50+
on.exit(options(op), add = TRUE)
51+
layer_data <- do.call(sf::st_read, args)
52+
53+
if (getOption("ogr.query.debug")) {
54+
message(
55+
sprintf(
56+
"-------------\nOGR debug ....\nSQL:\n%s\nnrows read:\n%i",
57+
statement
58+
),
59+
nrow(layer_data)
60+
)
61+
}
62+
if (inherits(layer_data, "try-error")) {
63+
if (length(gregexpr("SELECT", statement, ignore.case = TRUE)[[1]]) > 1) {
64+
stop(
65+
sprintf(
66+
"executing SQL failed: \n%s\n\nperhaps driver in use does not support sub-queries?",
67+
statement
68+
)
69+
)
70+
} else {
71+
stop("executing SQL failed")
72+
}
73+
}
74+
new("OGRSQLResult", layer_data = layer_data)
75+
76+
})
77+
78+
#' @rdname OGRSQLConnection-class
79+
#' @export
80+
setMethod("dbReadTable", c(conn = "OGRSQLConnection", name = "character"), function(conn, name, ...) {
81+
x <- dbSendQuery(conn, sprintf("SELECT * FROM %s", name))
82+
dbFetch(x)
83+
})
84+
85+
#' @rdname OGRSQLConnection-class
86+
#' @export
87+
88+
setMethod("dbListTables", c(conn = "OGRSQLConnection"), function(conn, ...) {
89+
layers <- sf::st_layers(conn@DSN, ...)
90+
layers$name
91+
})
92+
93+
#' @rdname OGRSQLConnection-class
94+
#' @export
95+
setMethod("dbExistsTable", c(conn = "OGRSQLConnection"), function(conn, name, ...) {
96+
name %in% dbListTables(conn, ...)
97+
})
98+
99+
#' @importFrom dbplyr dbplyr_edition
100+
#' @export
101+
dbplyr_edition.OGRSQLConnection <- function(con) 2L

R/OGRSQLDriver.R

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#' Class OGRSQLDriver
2+
#'
3+
#' OGRSQLDriver objects are created by [OGRSQL()] and used to select the correct
4+
#' method in [dbConnect()].
5+
#' They are a superclass of the [DBIDriver-class] class, and used purely for dispatch.
6+
#' @keywords internal
7+
#' @export
8+
9+
setClass("OGRSQLDriver", contains = "DBIDriver")
10+
11+
#' @rdname OGRSQLDriver-class
12+
#' @export
13+
14+
setMethod("dbDataType", "OGRSQLDriver", function(dbObj, obj, ...) {
15+
## see "type of the fields" http://www.gdal.org/ogr_sql.html
16+
if (is.factor(obj)) return("character")
17+
if (is.data.frame(obj)) return(callNextMethod(dbObj, obj))
18+
19+
switch(typeof(obj),
20+
logical = "boolean",
21+
character = "character",
22+
double = "numeric",
23+
integer = "integer",
24+
list = "character",
25+
raw = "character",
26+
blob = "character",
27+
stop("Unsupported type", call. = FALSE)
28+
)
29+
}
30+
)
31+
32+
#' @rdname OGRSQLDriver-class
33+
#' @export
34+
35+
setMethod("dbIsValid", "OGRSQLDriver", function(dbObj, ...) { TRUE })
36+
37+
#' @rdname OGRSQLDriver-class
38+
#' @export
39+
40+
setMethod("dbUnloadDriver", "OGRSQLDriver", function(drv, ...) {
41+
TRUE
42+
})
43+
44+
#' @rdname OGRSQLDriver-class
45+
#' @export
46+
47+
setMethod("dbGetInfo", "OGRSQLDriver", function(dbObj, ...) {
48+
vers <- sf::sf_extSoftVersion()
49+
list(
50+
name = "OGRSQLDriver",
51+
note = "virtual SQL driver for OGR Resources",
52+
driver.version = vers["GDAL"],
53+
client.version = utils::packageVersion("hfsubsetR")
54+
)
55+
})
56+
57+
58+
59+

R/OGRSQLResult.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#' Class OGRSQLResult (and methods)
2+
#'
3+
#' OGRSQLResult objects are created by [dbSendQuery()] or [dbSendStatement()],
4+
#' and encapsulate the result of an SQL statement.
5+
#' They are a superclass of the [DBIResult-class] class.
6+
#' The "Usage" section lists the class methods overridden by \pkg{lazsf}.
7+
#' @seealso
8+
#' The corresponding generic functions
9+
#' [DBI::dbFetch()], [DBI::dbClearResult()], and
10+
#' [DBI::dbHasCompleted()].
11+
#'
12+
#' @export
13+
#' @keywords internal
14+
setClass("OGRSQLResult", contains = "DBIResult", slots = c(layer_data = "ANY"))
15+
16+
17+
#' @rdname OGRSQLResult-class
18+
#' @export
19+
20+
setMethod("show", "OGRSQLResult", function(object) {
21+
cat(sprintf("Field names: %s\n",
22+
paste(names(object@layer_data), collapse = ", ")))
23+
invisible(NULL)
24+
})
25+
26+
#' @rdname OGRSQLResult-class
27+
#' @export
28+
setMethod("dbFetch", "OGRSQLResult", function(res, n = -1, ...) { res@layer_data })
29+
30+
#' @rdname OGRSQLResult-class
31+
#' @export
32+
setMethod("dbClearResult", "OGRSQLResult", function(res, ...) { TRUE })
33+
34+
#' @rdname OGRSQLResult-class
35+
#' @export
36+
setMethod("dbHasCompleted", "OGRSQLResult", function(res, ...) { TRUE })
37+
38+

0 commit comments

Comments
 (0)