50
50
# '
51
51
# ' # A rewrite changes the checksum:
52
52
# ' filepath_notimeset <- file.path(tempdir(), "b_pump_notimeset.gpkg")
53
- # ' # write 1:
53
+ # ' # write 1:
54
54
# ' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
55
55
# ' (md5_notimeset1 <- md5sum(filepath_notimeset))
56
- # ' # write 2:
56
+ # ' # write 2:
57
57
# ' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
58
58
# ' (md5_notimeset2 <- md5sum(filepath_notimeset))
59
- # ' # compare:
59
+ # ' # compare:
60
60
# ' md5_notimeset1 == md5_notimeset2
61
61
# '
62
62
# ' # Setting a fixed date
63
63
# ' filepath_timeset <- file.path(tempdir(), "b_pump_timeset.gpkg")
64
64
# ' (fixed_date <- as.Date("2020-12-25"))
65
65
# ' preset_timestamp(fixed_date)
66
- # ' # write 1 (date):
66
+ # ' # write 1 (date):
67
67
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
68
68
# ' md5_timeset1 <- md5sum(filepath_timeset)
69
- # ' # write 2 (date):
69
+ # ' # write 2 (date):
70
70
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
71
71
# ' md5_timeset2 <- md5sum(filepath_timeset)
72
- # ' # compare:
72
+ # ' # compare:
73
73
# ' all.equal(md5_timeset1, md5_timeset2)
74
74
# '
75
75
# ' # Setting a fixed time
76
76
# ' (fixed_time <- as.POSIXct("2020-12-25 12:00:00", tz = "CET"))
77
77
# ' preset_timestamp(fixed_time)
78
- # ' # write 3 (time):
78
+ # ' # write 3 (time):
79
79
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
80
80
# ' md5_timeset3 <- md5sum(filepath_timeset)
81
- # ' # write 4 (time):
81
+ # ' # write 4 (time):
82
82
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
83
83
# ' md5_timeset4 <- md5sum(filepath_timeset)
84
- # ' # compare:
84
+ # ' # compare:
85
85
# ' all.equal(md5_timeset3, md5_timeset4)
86
86
# '
87
87
# ' # Also works for GPKG 2D gridded coverage (with stars):
93
93
# ' preset_timestamp(fixed_time)
94
94
# '
95
95
# ' 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:
100
100
# ' stars_2d %>%
101
- # ' write_stars(filepath_stars, driver = "GPKG")
101
+ # ' write_stars(filepath_stars, driver = "GPKG")
102
102
# ' md5_stars1 <- md5sum(filepath_stars)
103
- # ' # write 2:
103
+ # ' # write 2:
104
104
# ' stars_2d %>%
105
- # ' write_stars(filepath_stars, driver = "GPKG")
105
+ # ' write_stars(filepath_stars, driver = "GPKG")
106
106
# ' md5_stars2 <- md5sum(filepath_stars)
107
- # ' # compare:
107
+ # ' # compare:
108
108
# ' all.equal(md5_stars1, md5_stars2)
109
109
# '
110
110
# ' @author Floris Vanderhaeghe, \url{https://github.com/florisvdh}
111
111
# '
112
112
# ' @export
113
113
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
+ }
117
117
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
+ )
121
122
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 )
124
125
125
- return (invisible (old ))
126
+ return (invisible (old ))
126
127
}
127
128
128
129
@@ -133,19 +134,6 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
133
134
134
135
135
136
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
137
# ' Amend the timestamp(s) in a GeoPackage file
150
138
# '
151
139
# ' Overwrites all timestamps (column \code{last_change}) of the
@@ -198,40 +186,40 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
198
186
# '
199
187
# ' # A rewrite changes the checksum:
200
188
# ' filepath_notimeset <- file.path(tempdir(), "b_pump_notimeset.gpkg")
201
- # ' # write 1:
189
+ # ' # write 1:
202
190
# ' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
203
191
# ' (md5_notimeset1 <- md5sum(filepath_notimeset))
204
- # ' # write 2:
192
+ # ' # write 2:
205
193
# ' st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
206
194
# ' (md5_notimeset2 <- md5sum(filepath_notimeset))
207
- # ' # compare:
195
+ # ' # compare:
208
196
# ' md5_notimeset1 == md5_notimeset2
209
197
# '
210
198
# ' # Setting a fixed date
211
199
# ' filepath_timeset <- file.path(tempdir(), "b_pump_timeset.gpkg")
212
200
# ' (fixed_date <- as.Date("2020-12-25"))
213
- # ' # write 1 (date):
201
+ # ' # write 1 (date):
214
202
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
215
203
# ' amend_timestamp(filepath_timeset, fixed_date)
216
204
# ' md5_timeset1 <- md5sum(filepath_timeset)
217
- # ' # write 2 (date):
205
+ # ' # write 2 (date):
218
206
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
219
207
# ' amend_timestamp(filepath_timeset, fixed_date)
220
208
# ' md5_timeset2 <- md5sum(filepath_timeset)
221
- # ' # compare:
209
+ # ' # compare:
222
210
# ' all.equal(md5_timeset1, md5_timeset2)
223
211
# '
224
212
# ' # Setting a fixed time
225
213
# ' (fixed_time <- as.POSIXct("2020-12-25 12:00:00", tz = "CET"))
226
- # ' # write 3 (time):
214
+ # ' # write 3 (time):
227
215
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
228
216
# ' amend_timestamp(filepath_timeset, fixed_time)
229
217
# ' md5_timeset3 <- md5sum(filepath_timeset)
230
- # ' # write 4 (time):
218
+ # ' # write 4 (time):
231
219
# ' st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
232
220
# ' amend_timestamp(filepath_timeset, fixed_time)
233
221
# ' md5_timeset4 <- md5sum(filepath_timeset)
234
- # ' # compare:
222
+ # ' # compare:
235
223
# ' all.equal(md5_timeset3, md5_timeset4)
236
224
# '
237
225
# ' # Also works for GPKG 2D gridded coverage (with stars):
@@ -241,20 +229,20 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
241
229
# ' filepath_stars <- file.path(tempdir(), "stars_2d.gpkg")
242
230
# '
243
231
# ' 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:
248
236
# ' stars_2d %>%
249
- # ' write_stars(filepath_stars, driver = "GPKG")
237
+ # ' write_stars(filepath_stars, driver = "GPKG")
250
238
# ' amend_timestamp(filepath_stars, fixed_time)
251
239
# ' md5_stars1 <- md5sum(filepath_stars)
252
- # ' # write 2:
240
+ # ' # write 2:
253
241
# ' stars_2d %>%
254
- # ' write_stars(filepath_stars, driver = "GPKG")
242
+ # ' write_stars(filepath_stars, driver = "GPKG")
255
243
# ' amend_timestamp(filepath_stars, fixed_time)
256
244
# ' md5_stars2 <- md5sum(filepath_stars)
257
- # ' # compare:
245
+ # ' # compare:
258
246
# ' all.equal(md5_stars1, md5_stars2)
259
247
# '
260
248
# ' @author Floris Vanderhaeghe, \url{https://github.com/florisvdh}
@@ -263,54 +251,62 @@ unset_timestamp <- function() Sys.unsetenv("OGR_CURRENT_DATE")
263
251
amend_timestamp <- function (dsn ,
264
252
timestamp = Sys.time(),
265
253
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
+ }
275
263
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
+ }
281
270
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
+ )
285
275
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
+ }
296
289
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
299
292
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
+ )
312
307
}
308
+ }
313
309
314
- RSQLite :: dbDisconnect(con )
315
- return (invisible (NULL ))
310
+ RSQLite :: dbDisconnect(con )
311
+ return (invisible (NULL ))
316
312
}
0 commit comments