Skip to content

Commit a0c39ef

Browse files
authored
Merge pull request #11 from r-spatial/2022-11-16_use-testthat-codecov
2 parents 66ea0fe + 35b3d04 commit a0c39ef

11 files changed

+288
-183
lines changed

.Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
^rgeopackage\.Rproj$
22
^\.Rproj\.user$
33
^LICENSE\.md$
4+
^codecov\.yml$
5+
^README\.Rmd$

DESCRIPTION

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@ Package: rgeopackage
22
Title: Helper Tools in Creating or Handling GeoPackage Files
33
Version: 0.0.0.900
44
Authors@R: c(
5-
person(given = "Floris",
6-
family = "Vanderhaeghe",
7-
role = c("aut", "cre"),
8-
email = "[email protected]",
5+
person("Floris", "Vanderhaeghe", , "[email protected]", role = c("aut", "cre"),
96
comment = c(ORCID = "0000-0002-6378-6229")),
10-
person("Research Institute for Nature and Forest",
11-
email = "[email protected]",
12-
role = "cph"))
13-
Description: The aim of this R package is to provide helper tools in
14-
creating or handling GeoPackage files, in order to complement
15-
other R spatial packages or GDAL. The package has no dependencies
16-
on other spatial packages and is not tied to any particular package
17-
by design.
7+
person("Research Institute for Nature and Forest", , , "[email protected]", role = "cph"),
8+
person("Eli", "Pousson", , "[email protected]", role = "ctb",
9+
comment = c(ORCID = "0000-0001-8280-1706"))
10+
)
11+
Description: The aim of this R package is to provide helper tools in
12+
creating or handling GeoPackage files, in order to complement other R
13+
spatial packages or GDAL. The package has no dependencies on other
14+
spatial packages and is not tied to any particular package by design.
1815
License: GPL (>= 3)
1916
Depends:
2017
R (>= 3.1.0)
2118
Suggests:
19+
covr,
2220
dplyr,
2321
openssl,
2422
RSQLite,
2523
sf,
26-
stars
24+
stars,
25+
testthat (>= 3.0.0),
26+
withr
27+
Config/testthat/edition: 3
2728
Encoding: UTF-8
2829
LazyData: true
2930
Roxygen: list(markdown = TRUE)
30-
RoxygenNote: 7.1.1
31+
RoxygenNote: 7.2.1

R/rgeopackage-package.R

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
## usethis namespace: end
6+
NULL

R/timestamp.R

+95-99
Original file line numberDiff line numberDiff line change
@@ -50,38 +50,38 @@
5050
#'
5151
#' # A rewrite changes the checksum:
5252
#' filepath_notimeset <- file.path(tempdir(), "b_pump_notimeset.gpkg")
53-
#' # write 1:
53+
#' # write 1:
5454
#' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
5555
#' (md5_notimeset1 <- md5sum(filepath_notimeset))
56-
#' # write 2:
56+
#' # write 2:
5757
#' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
5858
#' (md5_notimeset2 <- md5sum(filepath_notimeset))
59-
#' # compare:
59+
#' # compare:
6060
#' md5_notimeset1 == md5_notimeset2
6161
#'
6262
#' # Setting a fixed date
6363
#' filepath_timeset <- file.path(tempdir(), "b_pump_timeset.gpkg")
6464
#' (fixed_date <- as.Date("2020-12-25"))
6565
#' preset_timestamp(fixed_date)
66-
#' # write 1 (date):
66+
#' # write 1 (date):
6767
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
6868
#' md5_timeset1 <- md5sum(filepath_timeset)
69-
#' # write 2 (date):
69+
#' # write 2 (date):
7070
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
7171
#' md5_timeset2 <- md5sum(filepath_timeset)
72-
#' # compare:
72+
#' # compare:
7373
#' all.equal(md5_timeset1, md5_timeset2)
7474
#'
7575
#' # Setting a fixed time
7676
#' (fixed_time <- as.POSIXct("2020-12-25 12:00:00", tz = "CET"))
7777
#' preset_timestamp(fixed_time)
78-
#' # write 3 (time):
78+
#' # write 3 (time):
7979
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
8080
#' md5_timeset3 <- md5sum(filepath_timeset)
81-
#' # write 4 (time):
81+
#' # write 4 (time):
8282
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
8383
#' md5_timeset4 <- md5sum(filepath_timeset)
84-
#' # compare:
84+
#' # compare:
8585
#' all.equal(md5_timeset3, md5_timeset4)
8686
#'
8787
#' # Also works for GPKG 2D gridded coverage (with stars):
@@ -93,36 +93,37 @@
9393
#' preset_timestamp(fixed_time)
9494
#'
9595
#' stars_2d <-
96-
#' system.file("tif/L7_ETMs.tif", package = "stars") %>%
97-
#' read_stars() %>%
98-
#' slice(band, 1)
99-
#' # write 1:
96+
#' system.file("tif/L7_ETMs.tif", package = "stars") %>%
97+
#' read_stars() %>%
98+
#' slice(band, 1)
99+
#' # write 1:
100100
#' stars_2d %>%
101-
#' write_stars(filepath_stars, driver = "GPKG")
101+
#' write_stars(filepath_stars, driver = "GPKG")
102102
#' md5_stars1 <- md5sum(filepath_stars)
103-
#' # write 2:
103+
#' # write 2:
104104
#' stars_2d %>%
105-
#' write_stars(filepath_stars, driver = "GPKG")
105+
#' write_stars(filepath_stars, driver = "GPKG")
106106
#' md5_stars2 <- md5sum(filepath_stars)
107-
#' # compare:
107+
#' # compare:
108108
#' all.equal(md5_stars1, md5_stars2)
109109
#'
110110
#' @author Floris Vanderhaeghe, \url{https://github.com/florisvdh}
111111
#'
112112
#' @export
113113
preset_timestamp <- function(timestamp) {
114-
if (!inherits(timestamp, c("Date", "POSIXct"))) {
115-
stop("timestamp must be a Date or POSIXct object")
116-
}
114+
if (!inherits(timestamp, c("Date", "POSIXct"))) {
115+
stop("timestamp must be a Date or POSIXct object")
116+
}
117117

118-
timestamp <- format(timestamp,
119-
format = "%Y-%m-%dT%H:%M:%S.000Z",
120-
tz = "UTC")
118+
timestamp <- format(timestamp,
119+
format = "%Y-%m-%dT%H:%M:%S.000Z",
120+
tz = "UTC"
121+
)
121122

122-
old <- Sys.getenv("OGR_CURRENT_DATE")
123-
Sys.setenv(OGR_CURRENT_DATE = timestamp)
123+
old <- Sys.getenv("OGR_CURRENT_DATE")
124+
Sys.setenv(OGR_CURRENT_DATE = timestamp)
124125

125-
return(invisible(old))
126+
return(invisible(old))
126127
}
127128

128129

@@ -133,19 +134,6 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
133134

134135

135136

136-
137-
138-
139-
140-
141-
142-
143-
144-
145-
146-
147-
148-
149137
#' Amend the timestamp(s) in a GeoPackage file
150138
#'
151139
#' Overwrites all timestamps (column \code{last_change}) of the
@@ -198,40 +186,40 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
198186
#'
199187
#' # A rewrite changes the checksum:
200188
#' filepath_notimeset <- file.path(tempdir(), "b_pump_notimeset.gpkg")
201-
#' # write 1:
189+
#' # write 1:
202190
#' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
203191
#' (md5_notimeset1 <- md5sum(filepath_notimeset))
204-
#' # write 2:
192+
#' # write 2:
205193
#' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
206194
#' (md5_notimeset2 <- md5sum(filepath_notimeset))
207-
#' # compare:
195+
#' # compare:
208196
#' md5_notimeset1 == md5_notimeset2
209197
#'
210198
#' # Setting a fixed date
211199
#' filepath_timeset <- file.path(tempdir(), "b_pump_timeset.gpkg")
212200
#' (fixed_date <- as.Date("2020-12-25"))
213-
#' # write 1 (date):
201+
#' # write 1 (date):
214202
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
215203
#' amend_timestamp(filepath_timeset, fixed_date)
216204
#' md5_timeset1 <- md5sum(filepath_timeset)
217-
#' # write 2 (date):
205+
#' # write 2 (date):
218206
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
219207
#' amend_timestamp(filepath_timeset, fixed_date)
220208
#' md5_timeset2 <- md5sum(filepath_timeset)
221-
#' # compare:
209+
#' # compare:
222210
#' all.equal(md5_timeset1, md5_timeset2)
223211
#'
224212
#' # Setting a fixed time
225213
#' (fixed_time <- as.POSIXct("2020-12-25 12:00:00", tz = "CET"))
226-
#' # write 3 (time):
214+
#' # write 3 (time):
227215
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
228216
#' amend_timestamp(filepath_timeset, fixed_time)
229217
#' md5_timeset3 <- md5sum(filepath_timeset)
230-
#' # write 4 (time):
218+
#' # write 4 (time):
231219
#' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
232220
#' amend_timestamp(filepath_timeset, fixed_time)
233221
#' md5_timeset4 <- md5sum(filepath_timeset)
234-
#' # compare:
222+
#' # compare:
235223
#' all.equal(md5_timeset3, md5_timeset4)
236224
#'
237225
#' # Also works for GPKG 2D gridded coverage (with stars):
@@ -241,20 +229,20 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
241229
#' filepath_stars <- file.path(tempdir(), "stars_2d.gpkg")
242230
#'
243231
#' stars_2d <-
244-
#' system.file("tif/L7_ETMs.tif", package = "stars") %>%
245-
#' read_stars() %>%
246-
#' slice(band, 1)
247-
#' # write 1:
232+
#' system.file("tif/L7_ETMs.tif", package = "stars") %>%
233+
#' read_stars() %>%
234+
#' slice(band, 1)
235+
#' # write 1:
248236
#' stars_2d %>%
249-
#' write_stars(filepath_stars, driver = "GPKG")
237+
#' write_stars(filepath_stars, driver = "GPKG")
250238
#' amend_timestamp(filepath_stars, fixed_time)
251239
#' md5_stars1 <- md5sum(filepath_stars)
252-
#' # write 2:
240+
#' # write 2:
253241
#' stars_2d %>%
254-
#' write_stars(filepath_stars, driver = "GPKG")
242+
#' write_stars(filepath_stars, driver = "GPKG")
255243
#' amend_timestamp(filepath_stars, fixed_time)
256244
#' md5_stars2 <- md5sum(filepath_stars)
257-
#' # compare:
245+
#' # compare:
258246
#' all.equal(md5_stars1, md5_stars2)
259247
#'
260248
#' @author Floris Vanderhaeghe, \url{https://github.com/florisvdh}
@@ -263,54 +251,62 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
263251
amend_timestamp <- function(dsn,
264252
timestamp = Sys.time(),
265253
verbose = TRUE) {
266-
stopifnot(file.exists(dsn))
267-
stopifnot(is.logical(verbose), !is.na(verbose))
268-
# soft checking file format:
269-
if (!grepl("\\.gpkg$", dsn)) {
270-
stop("Expecting a file with extension '.gpkg'")
271-
}
272-
if (!inherits(timestamp, c("Date", "POSIXct"))) {
273-
stop("timestamp must be a Date or POSIXct object")
274-
}
254+
stopifnot(file.exists(dsn))
255+
stopifnot(is.logical(verbose), !is.na(verbose))
256+
# soft checking file format:
257+
if (!grepl("\\.gpkg$", dsn)) {
258+
stop("Expecting a file with extension '.gpkg'")
259+
}
260+
if (!inherits(timestamp, c("Date", "POSIXct"))) {
261+
stop("timestamp must be a Date or POSIXct object")
262+
}
275263

276-
if (!requireNamespace("RSQLite", quietly = TRUE)) {
277-
stop("Package \"RSQLite\" is needed when using this function. ",
278-
"Please install it.",
279-
call. = FALSE)
280-
}
264+
if (!requireNamespace("RSQLite", quietly = TRUE)) {
265+
stop("Package \"RSQLite\" is needed when using this function. ",
266+
"Please install it.",
267+
call. = FALSE
268+
)
269+
}
281270

282-
timestamp <- format(timestamp,
283-
format = "%Y-%m-%dT%H:%M:%S.000Z",
284-
tz = "UTC")
271+
timestamp <- format(timestamp,
272+
format = "%Y-%m-%dT%H:%M:%S.000Z",
273+
tz = "UTC"
274+
)
285275

286-
con <- RSQLite::dbConnect(RSQLite::SQLite(), dsn)
287-
updatequery <- sprintf("UPDATE gpkg_contents SET last_change = '%s'",
288-
timestamp)
289-
rows <- RSQLite::dbExecute(con, updatequery)
290-
if (verbose) {
291-
message(
292-
rows,
293-
" row(s) of the gpkg_contents table have been set with timestamp ",
294-
timestamp)
295-
}
276+
con <- RSQLite::dbConnect(RSQLite::SQLite(), dsn)
277+
updatequery <- sprintf(
278+
"UPDATE gpkg_contents SET last_change = '%s'",
279+
timestamp
280+
)
281+
rows <- RSQLite::dbExecute(con, updatequery)
282+
if (verbose) {
283+
message(
284+
rows,
285+
" row(s) of the gpkg_contents table have been set with timestamp ",
286+
timestamp
287+
)
288+
}
296289

297-
has_metadata <-
298-
nrow(RSQLite::dbGetQuery(con, "SELECT name FROM sqlite_master
290+
has_metadata <-
291+
nrow(RSQLite::dbGetQuery(con, "SELECT name FROM sqlite_master
299292
WHERE name == 'gpkg_metadata_reference'")) > 0
300-
if (has_metadata) {
301-
updatequery <-
302-
sprintf("UPDATE gpkg_metadata_reference SET timestamp = '%s'",
303-
timestamp)
304-
rows <- RSQLite::dbExecute(con, updatequery)
305-
if (verbose) {
306-
message(
307-
rows,
308-
" row(s) of the gpkg_metadata_reference table have ",
309-
"been set with timestamp ",
310-
timestamp)
311-
}
293+
if (has_metadata) {
294+
updatequery <-
295+
sprintf(
296+
"UPDATE gpkg_metadata_reference SET timestamp = '%s'",
297+
timestamp
298+
)
299+
rows <- RSQLite::dbExecute(con, updatequery)
300+
if (verbose) {
301+
message(
302+
rows,
303+
" row(s) of the gpkg_metadata_reference table have ",
304+
"been set with timestamp ",
305+
timestamp
306+
)
312307
}
308+
}
313309

314-
RSQLite::dbDisconnect(con)
315-
return(invisible(NULL))
310+
RSQLite::dbDisconnect(con)
311+
return(invisible(NULL))
316312
}

0 commit comments

Comments
 (0)