| preset_timestamp | R Documentation |
Presets the timestamp for usage by GDAL by setting the environment variable
OGR_CURRENT_DATE.
After this, newly written GeoPackage files
created by the GDAL vector or raster driver (e.g. through
sf::st_write() or stars::write_stars())
will carry this timestamp.
As such preset_timestamp() assists in making a binary-reproducible
GeoPackage file.
unset_timestamp() removes OGR_CURRENT_DATE from the
environment.
preset_timestamp(timestamp) unset_timestamp()
timestamp |
a |
The function converts the timestamp to a very specific ISO 8601 format
that is required by the GeoPackage standard, including conversion to UTC.
Cf. Requirement 15 in
version 1.3.
GDAL uses the timestamp to set the last_change column of the
gpkg_contents table in newly written GeoPackage files.
The timestamp set by preset_timestamp() is adopted by GDAL during
the entire session, unless unset_timestamp() is called.
Previous value of environment variable OGR_CURRENT_DATE is returned
invisibly.
Floris Vanderhaeghe, https://github.com/florisvdh
Other functions to control the GeoPackage timestamp(s):
amend_timestamp,
sf::st_write
library(sf)
library(openssl)
md5sum <- function(x) paste(md5(file(x)))
# Using existing geopackage with vector layer:
filepath <- system.file("gpkg/b_pump.gpkg", package = "sf")
(md5_original <- md5sum(filepath))
sf_layer <- read_sf(system.file("gpkg/b_pump.gpkg", package = "sf"))
# A rewrite changes the checksum:
filepath_notimeset <- file.path(tempdir(), "b_pump_notimeset.gpkg")
# write 1:
st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
(md5_notimeset1 <- md5sum(filepath_notimeset))
# write 2:
st_write(sf_layer, dsn = filepath_notimeset, delete_dsn = TRUE)
(md5_notimeset2 <- md5sum(filepath_notimeset))
# compare:
md5_notimeset1 == md5_notimeset2
# Setting a fixed date
filepath_timeset <- file.path(tempdir(), "b_pump_timeset.gpkg")
(fixed_date <- as.Date("2020-12-25"))
preset_timestamp(fixed_date)
# write 1 (date):
st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
md5_timeset1 <- md5sum(filepath_timeset)
# write 2 (date):
st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
md5_timeset2 <- md5sum(filepath_timeset)
# compare:
all.equal(md5_timeset1, md5_timeset2)
# Setting a fixed time
(fixed_time <- as.POSIXct("2020-12-25 12:00:00", tz = "CET"))
preset_timestamp(fixed_time)
# write 3 (time):
st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
md5_timeset3 <- md5sum(filepath_timeset)
# write 4 (time):
st_write(sf_layer, dsn = filepath_timeset, delete_dsn = TRUE)
md5_timeset4 <- md5sum(filepath_timeset)
# compare:
all.equal(md5_timeset3, md5_timeset4)
# Also works for GPKG 2D gridded coverage (with stars):
library(stars)
library(dplyr)
filepath_stars <- file.path(tempdir(), "stars_2d.gpkg")
(fixed_time <- as.POSIXct("2010-02-14 12:00:00", tz = "CET"))
preset_timestamp(fixed_time)
stars_2d <-
system.file("tif/L7_ETMs.tif", package = "stars") %>%
read_stars() %>%
slice(band, 1)
# write 1:
stars_2d %>%
write_stars(filepath_stars, driver = "GPKG")
md5_stars1 <- md5sum(filepath_stars)
# write 2:
stars_2d %>%
write_stars(filepath_stars, driver = "GPKG")
md5_stars2 <- md5sum(filepath_stars)
# compare:
all.equal(md5_stars1, md5_stars2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.