-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinsert_mines_to_db.R
88 lines (75 loc) · 3.8 KB
/
insert_mines_to_db.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
library(sf)
library(DBI)
library(tidyverse)
# this fixes a bug when the db insert of mine_point would unexpectedly crash
sf_use_s2(FALSE)
# --------------------------------------------------------------------------------------
# connect PostGIS mine vector database -------------------------------------------------
conn <- DBI::dbConnect(
drv = RPostgreSQL::PostgreSQL(),
host = Sys.getenv("db_host"),
port = Sys.getenv("db_port"),
dbname = Sys.getenv("db_name"),
user = Sys.getenv("db_admin_user"),
password = Sys.getenv("db_admin_password"),
)
# --------------------------------------------------------------------------------------
# read mines from files ----------------------------------------------------------------
mine_point_tbl <- sf::st_read(dsn = "./db/input/snl_mines_selection_v2_tbl.gpkg",
stringsAsFactors = FALSE) %>%
# rename columns to fit db nomenclature
dplyr::rename("geometry" = "geom",
"mine_name" = "mine",
"fp_id" = "id") %>%
dplyr::select(-snl_id, -primary_commodity)
# --------------------------------------------------------------------------------------
# sort mines by country and commodities ------------------------------------------------
mine_point_tbl <- mine_point_tbl %>%
dplyr::arrange(id_app_user, country, list_of_commodities)
# --------------------------------------------------------------------------------------
# create cluster table -----------------------------------------------------------------
mine_cluster_tbl <- mine_point_tbl %>%
sf::st_drop_geometry() %>%
dplyr::select(id = id_mine_cluster, id_app_user) %>%
tibble::as_tibble() %>%
dplyr::distinct()
# --------------------------------------------------------------------------------------
# create user table --------------------------------------------------------------------
app_user_tbl <- mine_point_tbl %>%
sf::st_drop_geometry() %>%
dplyr::select(id = id_app_user) %>%
tibble::as_tibble() %>%
dplyr::distinct()
# --------------------------------------------------------------------------------------
# feed users table to database ---------------------------------------------------------
DBI::dbWriteTable(conn = conn,
name = "app_user",
value = app_user_tbl,
append = TRUE,
row.names = FALSE)
# --------------------------------------------------------------------------------------
# feed clusters id table to database ---------------------------------------------------
DBI::dbWriteTable(conn = conn,
name = "mine_cluster",
value = mine_cluster_tbl,
append = TRUE,
row.names = FALSE)
# --------------------------------------------------------------------------------------
# feed mine points to database ---------------------------------------------------------
mine_point_tbl <- mine_point_tbl %>%
dplyr::select(-id_app_user)
sf::st_write(obj = mine_point_tbl,
dsn = conn,
layer = "mine_point",
append = TRUE,
factorsAsCharacter = TRUE)
# --------------------------------------------------------------------------------------
# run vacuun analyze -------------------------------------------------------------------
DBI::dbSendQuery(conn, statement = "VACUUM ANALYZE mine_cluster;")
DBI::dbSendQuery(conn, statement = "VACUUM ANALYZE mine_point;")
# --------------------------------------------------------------------------------------
# close database connection ------------------------------------------------------------
DBI::dbDisconnect(conn)
# --------------------------------------------------------------------------------------
# remove all variables from environment ------------------------------------------------
rm(list=ls())