Bladeren bron

Muudetud 'pk_lisame_ruudustikud'.

Ardo Kubjas 5 jaren geleden
bovenliggende
commit
19a6915173
3 gewijzigde bestanden met toevoegingen van 28 en 32 verwijderingen
  1. 15 4
      R/ajutised_muutujad.R
  2. 9 24
      R/pk_lisame_ruudustikud.R
  3. 4 4
      R/pk_sellest_alustame_db_loomist.R

+ 15 - 4
R/ajutised_muutujad.R

@@ -10,7 +10,17 @@ ajutised_muutujad <- function(pk = NULL, obj = NULL, conf = NULL) {
   if (is.null(conf)) {
     conf <- ruut::get_config()
     conf$schema <- obj
-    l$conf <- conf
+  }
+  ## Kontrollime kas objektil on andmebaas ja kas andmebaasis on piirjoon.
+  if (ruut::db_is_shema_exist(schema = conf$schema)) {
+    piir_table_name <- "a00_piir"
+    pk <- NULL
+    if (piir_table_name %in% ruut::db_schema_tablenames(conf = conf)) {
+      conn <- ruut::db_connect(conf = conf)
+      try(pk <- sf::st_read(dsn = conn, layer = c(conf$schema, piir_table_name)))
+    }
+  } else {
+    pk <- NULL
   }
   ## Piirkonna muutujale väärtuste omistamine, kui seda pole antud.
   if (is.null(pk)) {
@@ -26,13 +36,13 @@ ajutised_muutujad <- function(pk = NULL, obj = NULL, conf = NULL) {
   tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj) # ajutine gpkg fail
   conf$gpkg_home <- tmp_dir
   conf$gpkg_file <- obj
-  conf$gpkg_table <- "test_layer_1"
   ## Loome lingid gpkg ajutistest tabelitest lugemiseks ja kirjutamiseks
-  l$tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
-  l$tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
   conf$gpkg_table <- "test_layer_2"
   l$tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
   l$tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
+  conf$gpkg_table <- "test_layer_1"
+  l$tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
+  l$tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
   # Loome ajutise gpkg faili koos ajutise tabeliga.
   sf::write_sf(pk, dsn = tmp_gpkg_file, layer = "test_layer_0", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
   cat(sprintf("\n------------------\nAjutise gpkg faili asukoht: %s\n", tmp_gpkg_file))
@@ -42,5 +52,6 @@ ajutised_muutujad <- function(pk = NULL, obj = NULL, conf = NULL) {
   l$tmp_gpkg_file <- tmp_gpkg_file
   l$tmp_dir <- tmp_dir
   l$pk <- pk
+  l$conf <- conf
   return(l)
 }

+ 9 - 24
R/pk_lisame_ruudustikud.R

@@ -18,38 +18,23 @@
 #' ruut::db_schema_tablenames(conf = conf)
 #' }
 pk_lisame_ruudustikud <- function(obj = NULL, conf = NULL) {
-  obj <- gsub(" ", "_", tolower(obj))
+  ## ------------- muutujad ja teisendused ---------------
+  vars <- ajutised_muutujad(pk = NULL, obj, conf)
+  obj <- vars$obj
+  piir <- vars$pk
+  conf <- vars$conf
   ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
-  if (is.null(conf)) {
-    conf <- ruut::get_config()
-    conf$schema <- obj
-  }
-  if (!any("piir" %in% ruut::db_schema_tablenames(conf = conf))) {
+  if (!any("a00_piir" %in% ruut::db_schema_tablenames(conf = conf))) {
     cat("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
     return(NULL)
   }
 
-  ## ------------- muutujad ja teisendused ---------------
-  conn <- ruut::db_connect(conf = conf)
-  try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
-  tmp_dir <- tempdir()
-  tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
-  # system(sprintf("touch %s", tmp_gpkg_file))
-  conf$gpkg_home <- tmp_dir
-  conf$gpkg_file <- obj
-  conf$gpkg_table <- "test_layer"
-  tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
-  tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  # write to gpkg
-  sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
-  # unlink(tmp_gpkg_file)
-
   ## Algorithm
   # ruut::qgis_algorithm_search_by_word(str = "extract")
   algorithm <- "native:extractbylocation"
   # cat(ruut::qgis_show_help(algorithm = algorithm))
   ## -------------------- Loop -----------------------
-  intersect_layers <- c("bb2") # c("piir", "bb", "bb2")
+  intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
   ruudud <- c("epk10t_grid", "epk2t_grid", "epk02t_grid") # "epk10t", "epk2t"
   conf$schema <- "maaamet"
   # ruudud <- c("epk02t_grid")
@@ -61,7 +46,7 @@ pk_lisame_ruudustikud <- function(obj = NULL, conf = NULL) {
       conf$table <- ruut
       conf$schema <- "maaamet"
       input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", checkPrimaryKeyUnicity = T, key = "id", srid = 3301)
-      conf$table <- sprintf("%s", ruut)
+      conf$table <- sprintf("bb2_%s", ruut)
       conf$schema <- obj
       output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
       ## ------------------- QGIS: qgisprocess ----------------------
@@ -74,5 +59,5 @@ pk_lisame_ruudustikud <- function(obj = NULL, conf = NULL) {
   ## Layers list
   conf$schema <- obj
   ruut::db_schema_tablenames(conf = conf)
-  sf::st_layers(dsn = tmp_gpkg_file)
+  sf::st_layers(dsn = vars$tmp_gpkg_file)
 }

+ 4 - 4
R/pk_sellest_alustame_db_loomist.R

@@ -53,7 +53,7 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
   # write to postgis
   ruut::db_create_new_schema(conf = conf)
   conn <- ruut::db_connect(conf = conf)
-  sf::write_sf(pk, dsn = conn, layer = c(conf$schema, "a120_piir"), driver = "PostgreSQL", fid_column_name = "id", delete_dsn = F)
+  sf::write_sf(pk, dsn = conn, layer = c(conf$schema, "a00_piir"), driver = "PostgreSQL", fid_column_name = "id", delete_dsn = F)
 
   ## 2. Piirikasti salvestamine
   ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
@@ -61,7 +61,7 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
   extent <- sprintf("%s,%s,%s,%s [EPSG:3301]", round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["xmax"] / 100)) * 100, round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["ymax"] / 100)) * 100)
   hspacing <- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
   vspacing <- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
-  conf$table <- "a110_piir_bb"
+  conf$table <- "a00_piir_bb"
   output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
   # ruut::qgis_algorithm_search_by_word(str = "grid")
   algorithm <- "native:creategrid"
@@ -83,7 +83,7 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
     conf = conf, geometry_type = "Polygon", srid = 3301,
     checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
   )
-  conf$table <- "a110_piir_bb"
+  conf$table <- "a00_piir_bb"
   conf$schema <- obj
   intersect <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
   output <- vars$tmp_gpkg_file_output_1 # ajutine fail
@@ -97,7 +97,7 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
   extent <- sprintf("%s,%s,%s,%s [EPSG:3301]", round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["xmax"] / 100)) * 100, round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["ymax"] / 100)) * 100)
   hspacing <- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
   vspacing <- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
-  conf$table <- "a100_bb2"
+  conf$table <- "a00_bb2"
   output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
   # ruut::qgis_algorithm_search_by_word(str = "grid")
   algorithm <- "native:creategrid"