Kaynağa Gözat

Muudetud 'pk_lisame_punktid'.

Ardo Kubjas 5 yıl önce
ebeveyn
işleme
4733543b31
1 değiştirilmiş dosya ile 12 ekleme ve 26 silme
  1. 12 26
      R/pk_lisame_punktid.R

+ 12 - 26
R/pk_lisame_punktid.R

@@ -18,39 +18,24 @@
 #' ruut::db_schema_tablenames(conf = conf)
 #' }
 pk_lisame_punktid <- 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(qgisprocess::qgis_show_help(algorithm = algorithm))
 
   ## -------------------- Loop -----------------------
-  intersect_layers <- c("bb2") # c("piir", "bb", "bb2")
+  intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
   andmed <- data.frame("schema" = character(0), "table" = character(0))
   andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
   andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw"))
@@ -88,7 +73,7 @@ pk_lisame_punktid <- function(obj = NULL, conf = NULL) {
         conf = conf, geometry_type = "Point", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
       )
-      conf$table <- sprintf("%s", andmed$table[i])
+      conf$table <- sprintf("%s_p_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
       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 ----------------------
@@ -99,8 +84,9 @@ pk_lisame_punktid <- function(obj = NULL, conf = NULL) {
       system(cmd)
       ## Filtreerime aadressandmete kihi eraldi alamkihtideks
       if (andmed$table[i] == "aadressandmed") {
-        aadressandmed <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("adob_liik"), drop = FALSE])
-        parent_table <- as.character(andmed$table[i])
+        parent_table <- sprintf("%s_p_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
+        conn <- ruut::db_connect(conf = conf)
+        aadressandmed <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("adob_liik"), drop = FALSE])
         for (k in 1:nrow(aadressandmed)) {
           table_suffix <- aadressandmed$adob_liik[k]
           conf$table <- parent_table
@@ -121,5 +107,5 @@ pk_lisame_punktid <- 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)
 }