Procházet zdrojové kódy

Muudetud 'pk_lisame_polygoonid'.

Ardo Kubjas před 5 roky
rodič
revize
97e40333f2
1 změnil soubory, kde provedl 22 přidání a 30 odebrání
  1. 22 30
      R/pk_lisame_polygoonid.R

+ 22 - 30
R/pk_lisame_polygoonid.R

@@ -18,39 +18,24 @@
 #' ruut::db_schema_tablenames(conf = conf)
 #' }
 pk_lisame_polygoonid <- 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")
   andmed <- data.frame("schema" = character(0), "table" = character(0))
   andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
   andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
@@ -78,10 +63,13 @@ pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
   andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_a"))
   andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_ka"))
   andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "mullakaart"))
+
+
   for (intersect in intersect_layers) {
     conf$table <- intersect
     conf$schema <- obj
     intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
+
     for (i in 1:nrow(andmed)) {
       if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
       conf$table <- andmed$table[i]
@@ -90,7 +78,8 @@ pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
         conf = conf, geometry_type = "Polygon", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
       )
-      conf$table <- sprintf("%s", andmed$table[i])
+      ## Andmebaasi tabeli nimi: 2 esimest tähte tuleb schema nimest millest on andmed võetud, siis '_a_' s.o 'area' mis tähistab polügoone.
+      conf$table <- sprintf("%s_a_%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")
       str <- paste0("{ 'INPUT' : '", input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
@@ -98,10 +87,12 @@ pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       cat(sprintf("\n%s\n\n", cmd))
       system(cmd)
-      ## Filtreerime maakasutuse kihi 'landuse-a' eraldi alamkihtideks
+
+      ## Filtreerime mõned andmetabelid eraldi alamkihtideks
       if (andmed$table[i] == "landuse_a") {
-        landuse_a <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("code", "fclass")])
-        parent_table <- as.character(andmed$table[i])
+        parent_table <- sprintf("%s_a_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
+        conn <- ruut::db_connect(conf = conf)
+        landuse_a <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("code", "fclass")])
         for (k in 1:nrow(landuse_a)) {
           table_suffix <- landuse_a$fclass[k]
           conf$table <- parent_table
@@ -117,10 +108,11 @@ pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
           system(cmd)
         }
       }
-      ## Filtreerime hoonete kihi 'landuse-a' eraldi tüübi järele alamkihtideks
+      ## Filtreerime mõned andmetabelid eraldi alamkihtideks
       if (andmed$table[i] == "e_401_hoone_ka") {
-        alamobjektid <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("kood", "tyyp")])
-        parent_table <- as.character(andmed$table[i])
+        parent_table <- sprintf("%s_a_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
+        conn <- ruut::db_connect(conf = conf)
+        alamobjektid <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("kood", "tyyp")])
         for (k in 1:nrow(alamobjektid)) {
           table_suffix <- alamobjektid$tyyp[k]
           conf$table <- parent_table
@@ -154,5 +146,5 @@ pk_lisame_polygoonid <- 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)
 }