Skip to content

Commit

Permalink
autoincrement primary key (bump v2.0.2)
Browse files Browse the repository at this point in the history
no longer need `tibble` package
  • Loading branch information
achubaty committed Mar 13, 2024
1 parent a5fd5d8 commit 8e09514
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 106 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description: Utilities for working with North American climate data from
'ClimateNA' <https://climatena.ca/>.
URL: https://github.com/PredictiveEcology/climateData
Date: 2024-03-09
Version: 2.0.1
Version: 2.0.2
Authors@R: c(
person("Alex M", "Chubaty", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7146-8135")),
Expand All @@ -29,7 +29,6 @@ Imports:
stats,
sf,
terra,
tibble,
tools,
utils
Suggests:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ importFrom(terra,trim)
importFrom(terra,values)
importFrom(terra,vect)
importFrom(terra,writeRaster)
importFrom(tibble,rowid_to_column)
importFrom(tools,file_path_sans_ext)
importFrom(utils,head)
importFrom(utils,write.table)
161 changes: 78 additions & 83 deletions R/ClimateNA_createTiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,64 +164,61 @@ sqlite_connect_db <- function(dbfile) {
#' @importFrom DBI dbCreateTable dbExecute dbExistsTable
#' @importFrom dplyr tbl
#' @importFrom RSQLite dbConnect SQLite
#' @importFrom tibble rowid_to_column
#' @rdname ClimateNA_sql
ClimateNA_sql <- function(dbfile, type) {
db <- sqlite_connect_db(dbfile)

df_template <- switch(
type,
historic_normals = data.frame(
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
period = NA_character_, ## climate period
tileid = NA_integer_, ## tile ID
created = Sys.time(), ## timestamp of when tile set was created using ClimateNA
zipfile = NA_character_, ## relative file path
archived = Sys.time(), ## timestamp of when tile set archive was created (zipped)
gid = NA_character_, ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = Sys.time(), ## timestamp of when archive uploaded to google drive
stringsAsFactors = FALSE
historic_normals = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
period = "CHARACTER", ## climate period
tileid = "INTEGER", ## tile ID
created = "DATETIME", ## timestamp of when tile set was created using ClimateNA
zipfile = "CHARACTER", ## relative file path
archived = "DATETIME", ## timestamp of when tile set archive was created (zipped)
gid = "CHARACTER", ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = "DATETIME" ## timestamp of when archive uploaded to google drive
),
future_normals = data.frame(
gcm = NA_character_, ## climate scenario GCM
ssp = NA_character_, ## climate scenario SSP
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
period = NA_character_, ## climate period
tileid = NA_integer_, ## tile ID
created = Sys.time(), ## timestamp of when tile set was created using ClimateNA
zipfile = NA_character_, ## relative file path
archived = Sys.time(), ## timestamp of when tile set archive was created (zipped)
gid = NA_character_, ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = Sys.time(), ## timestamp of when archive uploaded to google drive
stringsAsFactors = FALSE
future_normals = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
gcm = "CHARACTER", ## climate scenario GCM
ssp = "CHARACTER", ## climate scenario SSP
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
period = "CHARACTER", ## climate period
tileid = "INTEGER", ## tile ID
created = "DATETIME", ## timestamp of when tile set was created using ClimateNA
zipfile = "CHARACTER", ## relative file path
archived = "DATETIME", ## timestamp of when tile set archive was created (zipped)
gid = "CHARACTER", ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = "DATETIME" ## timestamp of when archive uploaded to google drive
),
historic = data.frame(
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
year = NA_character_, ## climate year
tileid = NA_integer_, ## tile ID
created = Sys.time(), ## timestamp of when tile set was created using ClimateNA
zipfile = NA_character_, ## relative file path
archived = Sys.time(), ## timestamp of when tile set archive was created (zipped)
gid = NA_character_, ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = Sys.time(), ## timestamp of when archive uploaded to google drive
stringsAsFactors = FALSE
historic = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
year = "CHARACTER", ## climate year
tileid = "INTEGER", ## tile ID
created = "DATETIME", ## timestamp of when tile set was created using ClimateNA
zipfile = "CHARACTER", ## relative file path
archived = "DATETIME", ## timestamp of when tile set archive was created (zipped)
gid = "CHARACTER", ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = "DATETIME" ## timestamp of when archive uploaded to google drive
),
future = data.frame(
gcm = NA_character_, ## climate scenario GCM
ssp = NA_character_, ## climate scenario SSP
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
year = NA_character_, ## climate year (or period)
tileid = NA_integer_, ## tile ID
created = Sys.time(), ## timestamp of when tile set was created using ClimateNA
zipfile = NA_character_, ## relative file path
archived = Sys.time(), ## timestamp of when tile set archive was created (zipped)
gid = NA_character_, ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = Sys.time(), ## timestamp of when archive uploaded to google drive
stringsAsFactors = FALSE
future = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
gcm = "CHARACTER", ## climate scenario GCM
ssp = "CHARACTER", ## climate scenario SSP
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
year = "CHARACTER", ## climate year (or period)
tileid = "INTEGER", ## tile ID
created = "DATETIME", ## timestamp of when tile set was created using ClimateNA
zipfile = "CHARACTER", ## relative file path
archived = "DATETIME", ## timestamp of when tile set archive was created (zipped)
gid = "CHARACTER", ## google drive file id; archives built by decade, so there will be dupe gids
uploaded = "DATETIME" ## timestamp of when archive uploaded to google drive
)
) |>
tibble::rowid_to_column() |> ## add rowid column to use as table primary key
na.omit()
)

tbl <- type

Expand All @@ -241,45 +238,43 @@ checksums_sql <- function(dbfile, type) {

df_template <- switch(
type,
historic_normals = data.frame(
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
period = NA_character_, ## climate period
tileid = NA_integer_, ## tile ID
filename = NA_character_, ## file name
filehash = NA_character_, ## file hash (checksum)
stringsAsFactors = FALSE
historic_normals = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
period = "CHARACTER", ## climate period
tileid = "ISNTEGER", ## tile ID
filename = "CHARACTER", ## file name
filehash = "CHARACTER" ## file hash (checksum)
),
future_normals = data.frame(
gcm = NA_character_, ## climate scenario GCM
ssp = NA_character_, ## climate scenario SSP
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
period = NA_character_, ## climate period
tileid = NA_integer_, ## tile ID
filename = NA_character_, ## file name
filehash = NA_character_, ## file hash (checksum)
stringsAsFactors = FALSE
future_normals = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
gcm = "CHARACTER", ## climate scenario GCM
ssp = "CHARACTER", ## climate scenario SSP
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
period = "CHARACTER", ## climate period
tileid = "INTEGER", ## tile ID
filename = "CHARACTER", ## file name
filehash = "CHARACTER" ## file hash (checksum)
),
historic = data.frame(
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
year = NA_character_, ## climate year
tileid = NA_integer_, ## tile ID
filename = NA_character_, ## file name
filehash = NA_character_, ## file hash (checksum)
stringsAsFactors = FALSE
historic = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
year = "CHARACTER", ## climate year
tileid = "INTEGER", ## tile ID
filename = "CHARACTER", ## file name
filehash = "CHARACTER" ## file hash (checksum)
),
future = data.frame(
gcm = NA_character_, ## climate scenario GCM
ssp = NA_character_, ## climate scenario SSP
msy = NA_character_, ## one of: 'M', 'S', 'Y', 'MSY'
year = NA_character_, ## climate year (or period)
tileid = NA_integer_, ## tile ID
filename = NA_character_, ## file name
filehash = NA_character_, ## file hash (checksum)
stringsAsFactors = FALSE
future = c(
id = "INTEGER PRIMARY KEY AUTOINCREMENT",
gcm = "CHARACTER", ## climate scenario GCM
ssp = "CHARACTER", ## climate scenario SSP
msy = "CHARACTER", ## one of: 'M', 'S', 'Y', 'MSY'
year = "CHARACTER", ## climate year (or period)
tileid = "INTEGER", ## tile ID
filename = "CHARACTER", ## file name
filehash = "CHARACTER" ## file hash (checksum)
)
) |>
tibble::rowid_to_column() |> ## add rowid column to use as table primary key
na.omit()
)

tbl <- paste0("checksums_", type)

Expand Down
5 changes: 5 additions & 0 deletions data-raw/01-ClimateNA_setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ library(reproducible)
library(rprojroot)

prjDir <- find_root(has_file("climateData.Rproj"))

devtools::load_all(prjDir)

# setup ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -52,6 +53,10 @@ pkgDBfile <- file.path(prjDir, "inst", "extdata", dbfile)
addlDBfile <- file.path(ClimateNAdata, dbfile)
wrkngDBfile <- file.path(prjDir, "tmp", dbfile) ## local copy in current project

if (!dir.exists(dirname(wrkngDBfile))) {
dir.create(dirname(wrkngDBfile))
}

if (!file.exists(wrkngDBfile)) {
file.copy(pkgDBfile, wrkngDBfile) ## always work on a copy
}
10 changes: 5 additions & 5 deletions data-raw/03-ClimateNA_future.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ checksums_future <- future_lapply(dem_ff, function(f) {
}, future.seed = NULL) |>
dplyr::bind_rows()

if (!"rowid" %in% colnames(checksums_future)) {
checksums_future <- tibble::rowid_to_column(checksums_future)
rows_append(checksums_future_df, checksums_future, copy = TRUE, in_place = TRUE)
if (!"id" %in% colnames(new_rows_future)) {
rows_append(future_climate_df, new_rows_future, copy = TRUE, in_place = TRUE)
} else {
rows_update(checksums_future_df, checksums_future, copy = TRUE, in_place = TRUE, unmatched = "ignore")
rows_update(future_climate_df, new_rows_future, copy = TRUE, in_place = TRUE, unmatched = "ignore")
}

DBI::dbDisconnect(checksums_db)
DBI::dbDisconnect(checksums_db)
DBI::dbDisconnect(climate_db)

file.copy(wrkngDBfile, addlDBfile, overwrite = TRUE)

Expand Down
12 changes: 6 additions & 6 deletions data-raw/03-ClimateNA_future_normals.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ checksums_futu_normals <- future_lapply(dem_ff, function(f) {
}, future.seed = NULL) |>
dplyr::bind_rows()

if (!"rowid" %in% colnames(checksums_futu_normals)) {
checksums_futu_normals <- tibble::rowid_to_column(checksums_futu_normals)
rows_append(checksums_futu_normals_df, checksums_futu_normals, copy = TRUE, in_place = TRUE)
if (!"id" %in% colnames(new_rows_futu_normals)) {
rows_append(climate_futu_normals_df, new_rows_futu_normals, copy = TRUE, in_place = TRUE)
} else {
rows_update(checksums_futu_normals_df, checksums_futu_normals, copy = TRUE, in_place = TRUE, unmatched = "ignore")
rows_update(climate_futu_normals_df, new_rows_futu_normals, copy = TRUE, in_place = TRUE, unmatched = "ignore")
}

DBI::dbDisconnect(checksums_db)
DBI::dbDisconnect(climate_db)

file.copy(wrkngDBfile, addlDBfile, overwrite = TRUE)

Expand All @@ -180,7 +180,7 @@ if (createZips) {

row <- dplyr::filter(
climate_futu_normals_df,
msy == !!msy & tileid == !!tile ## all periods put into same zipfile
gcm == !!gcm & ssp == !!ssp & msy == !!msy & period %in% !!period & tileid == !!tile
) |>
collect()

Expand Down Expand Up @@ -242,7 +242,7 @@ if (uploadArchives) {

row <- dplyr::filter(
climate_futu_normals_df,
msy == !!msy & tileid == !!tile ## all periods put into same zipfile
gcm == !!gcm & ssp == !!ssp & msy == !!msy & period %in% !!period & tileid == !!tile
) |>
collect()

Expand Down
10 changes: 5 additions & 5 deletions data-raw/03-ClimateNA_historic.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ new_rows_historic <- future_lapply(dem_ff, function(f) {
}

new_row <- data.frame(
## rowid will be filled automatically
## id will be filled automatically
msy = msy,
year = yr,
tileid = tile,
Expand Down Expand Up @@ -144,14 +144,14 @@ checksums_historic <- future_lapply(dem_ff, function(f) {
}, future.seed = NULL) |>
dplyr::bind_rows()

if (!"rowid" %in% colnames(checksums_historic)) {
checksums_historic <- tibble::rowid_to_column(checksums_historic)
rows_append(checksums_historic_df, checksums_historic, copy = TRUE, in_place = TRUE)
if (!"id" %in% colnames(new_rows_historic)) {
rows_append(climate_historic_df, new_rows_historic, copy = TRUE, in_place = TRUE)
} else {
rows_update(checksums_historic_df, checksums_historic, copy = TRUE, in_place = TRUE, unmatched = "ignore")
rows_update(climate_historic_df, new_rows_historic, copy = TRUE, in_place = TRUE, unmatched = "ignore")
}

DBI::dbDisconnect(checksums_db)
DBI::dbDisconnect(climate_db)

file.copy(wrkngDBfile, addlDBfile, overwrite = TRUE)

Expand Down
8 changes: 4 additions & 4 deletions data-raw/03-ClimateNA_historic_normals.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ checksums_hist_normals <- future_lapply(dem_ff, function(f) {
}, future.seed = NULL) |>
dplyr::bind_rows()

if (!"rowid" %in% colnames(checksums_hist_normals)) {
checksums_hist_normals <- tibble::rowid_to_column(checksums_hist_normals)
rows_append(checksums_hist_normals_df, checksums_hist_normals, copy = TRUE, in_place = TRUE)
if (!"id" %in% colnames(new_rows_hist_normals)) {
rows_append(climate_hist_normals_df, new_rows_hist_normals, copy = TRUE, in_place = TRUE)
} else {
rows_update(checksums_hist_normals_df, checksums_hist_normals, copy = TRUE, in_place = TRUE, unmatched = "ignore")
rows_update(climate_hist_normals_df, new_rows_hist_normals, copy = TRUE, in_place = TRUE, unmatched = "ignore")
}

DBI::dbDisconnect(checksums_db)
DBI::dbDisconnect(climate_db)

file.copy(wrkngDBfile, addlDBfile, overwrite = TRUE)

Expand Down

0 comments on commit 8e09514

Please sign in to comment.