Ardo Kubjas 3 лет назад
Родитель
Сommit
a7f8d17553

+ 3 - 2
R/pk_lisame_jooned.R

@@ -47,7 +47,7 @@ pk_lisame_jooned <- function(obj = NULL, conf = NULL) {
   andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
   andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
   andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
-  andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
+  # andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
   andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_j"))
   # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_j"))
   # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_405_piire_j"))
@@ -64,7 +64,8 @@ pk_lisame_jooned <- function(obj = NULL, conf = NULL) {
     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)) {
       ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
-      if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
+      # if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
+      if (andmed$schema[i] %in% c("gtfs")) geom <- "geometry" else geom <- "geom"
       conf$table <- andmed$table[i]
       conf$schema <- andmed$schema[i]
       input <- ruut::construct_to_gpkg_output_postgres_str(

+ 2 - 0
R/pk_lisame_polygoonid.R

@@ -222,6 +222,7 @@ pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
             layer = c(conf$schema, parent_table),
             as_tibble = T
           ))[, c("code", "fclass")])
+        DBI::dbDisconnect(conn)
         for (k in 1:nrow(landuse_a)) {
           table_suffix <- landuse_a$fclass[k]
           conf$table <- parent_table
@@ -278,6 +279,7 @@ pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
             layer = c(conf$schema, parent_table),
             as_tibble = T
           ))[, c("kood", "tyyp")])
+        DBI::dbDisconnect(conn)
         for (k in 1:nrow(alamobjektid)) {
           table_suffix <- alamobjektid$tyyp[k]
           conf$table <- parent_table

+ 155 - 39
R/pk_lisame_punktid.R

@@ -25,7 +25,9 @@ pk_lisame_punktid <- function(obj = NULL, conf = NULL) {
   conf <- vars$conf
   ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
   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")
+    cat(
+      "\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n"
+    )
     return(NULL)
   }
 
@@ -36,68 +38,182 @@ pk_lisame_punktid <- function(obj = NULL, conf = NULL) {
 
   ## -------------------- Loop -----------------------
   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"))
-  andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
-  andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_402_korgrajatis_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_602_tehnopaigaldis_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kirik_p"))
-  andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kohanimi"))
+  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"))
+  andmed <-
+    rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_p"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_p"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_p"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "eesti", "table" = "e_402_korgrajatis_p"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_p"))
+  andmed <-
+    rbind(andmed,
+          data.frame("schema" = "eesti", "table" = "e_602_tehnopaigaldis_p"))
+  andmed <-
+    rbind(andmed, data.frame("schema" = "eesti", "table" = "kirik_p"))
+  andmed <-
+    rbind(andmed, data.frame("schema" = "eesti", "table" = "kohanimi"))
   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")
+    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)) {
       ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
-      if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
+      if (andmed$schema[i] %in% c("gtfs"))
+        geom <- "geometry"
+      else
+        geom <- "geom"
       conf$table <- andmed$table[i]
       conf$schema <- andmed$schema[i]
       input <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "Point", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
+        conf = conf,
+        geometry_type = "Point",
+        srid = 3301,
+        checkPrimaryKeyUnicity = TRUE,
+        key = "id",
+        geometry_field = geom
       )
-      conf$table <- sprintf("data_p_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
+      conf$table <-
+        sprintf("data_p_%s_%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")
+      output <-
+        ruut::construct_to_gpkg_output_postgres_str(
+          conf = conf,
+          geometry_field = "geom",
+          geometry_type = NULL,
+          srid = 3301,
+          checkPrimaryKeyUnicity = FALSE,
+          key = "id"
+        )
       ## ------------------- QGIS: qgisprocess ----------------------
-      str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'PREDICATE' : [0] }")
+      str <-
+        paste0(
+          "{ 'INPUT' : '",
+          input,
+          "', 'INTERSECT' : '",
+          intersect_layer,
+          "', 'OUTPUT' : '",
+          output,
+          "', 'PREDICATE' : [0] }"
+        )
       algorithm <- "native:extractbylocation"
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       cat(sprintf("\n%s\n\n", cmd))
       system(cmd)
       ## Filtreerime aadressandmete kihi eraldi alamkihtideks
       if (andmed$table[i] == "aadressandmed") {
-        parent_table <- sprintf("data_p_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
+        parent_table <-
+          sprintf("data_p_%s_%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])
+        aadressandmed <-
+          unique(as.data.frame(sf::st_read(
+            dsn = conn,
+            layer = c(conf$schema, parent_table),
+            as_tibble = T
+          ))[, c("adob_liik"), drop = FALSE])
+        DBI::dbDisconnect(conn)
         for (k in 1:nrow(aadressandmed)) {
           table_suffix <- aadressandmed$adob_liik[k]
           conf$table <- parent_table
           conf$schema <- obj
-          input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Point", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
+          input <-
+            ruut::construct_to_gpkg_output_postgres_str(
+              conf = conf,
+              geometry_type = "Point",
+              srid = 3301,
+              checkPrimaryKeyUnicity = TRUE,
+              key = "id"
+            )
           # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
           conf$table <- sprintf("%s_%s", parent_table, table_suffix)
-          output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
-          str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'adob_liik', OPERATOR : 0, VALUE : ", aadressandmed$adob_liik[k], " }")
+          output <-
+            ruut::construct_to_gpkg_output_postgres_str(
+              conf = conf,
+              geometry_field = "geom",
+              geometry_type = "Polygon",
+              srid = 3301,
+              checkPrimaryKeyUnicity = FALSE,
+              key = "id"
+            )
+          str <-
+            paste0(
+              "{ 'INPUT' : '",
+              input,
+              "', 'INTERSECT' : '",
+              intersect_layer,
+              "', 'OUTPUT' : '",
+              output,
+              "', FIELD : 'adob_liik', OPERATOR : 0, VALUE : ",
+              aadressandmed$adob_liik[k],
+              " }"
+            )
           algorithm <- "native:extractbyattribute"
-          cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+          cmd <-
+            ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
           cat(sprintf("\n%s\n", cmd))
           system(cmd)
         }

+ 2 - 1
R/pk_sellest_alustame_db_loomist.R

@@ -86,7 +86,8 @@ pk_sellest_alustame_db_loomist <-
     system(cmd)
     # Write to postgis
     ruut::db_create_new_schema(conf = conf)
-    conn <- ruut::db_connect(conf = conf)
+    # conn <- ruut::db_connect(conf = conf)
+    # DBI::dbDisconnect(conn)
     # Eemaldame üleliigsed veerud
     # ruut::qgis_algorithm_search_by_word("Drop")
     algorithm <- "native:deletecolumn"

+ 172 - 56
R/pk_teisendame_jooni.R

@@ -25,120 +25,236 @@ pk_teisendame_jooni <- function(obj = NULL, conf = NULL) {
   conf <- vars$conf
   ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
   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")
+    cat(
+      "\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n"
+    )
     return(NULL)
   }
 
   ## ====================== teisendused =====================
-  conn <- ruut::db_connect(conf = conf)
   # Layers list
   layer_names <- ruut::db_schema_tablenames(conf = conf)
-  layer_names <- layer_names[!layer_names %in% c("a00_bb2", "a00_piir_bb", "bb2_epk10t_grid", "bb2_epk02t_grid", "bb2_epk2t_grid")]
-  layer_names <- c("a00_piir", layer_names[grepl("^data_", layer_names)])
+  layer_names <-
+    layer_names[!layer_names %in% c(
+      "a00_piir",
+      "a00_bb2",
+      "a00_piir_bb",
+      "bb2_epk10t_grid",
+      "bb2_epk02t_grid",
+      "bb2_epk2t_grid"
+    )]
+  layer_names <- layer_names[grepl("^data_l_", layer_names)]
   for (layer_name in layer_names) {
-    try(ruumiline_obj <- sf::st_read(dsn = conn, layer = c(conf$schema, layer_name)))
+    conn <- ruut::db_connect(conf = conf)
+    try(ruumiline_obj <-
+          sf::st_read(dsn = conn,
+                      layer = c(conf$schema, layer_name)))
+    DBI::dbDisconnect(conn)
     ## Kontlrollime kas geomeetriline objekt on polügoon.
     ## Arvutame sel juhul pindala.
-    is_line <- any(grepl("line", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE))
+    is_line <-
+      any(grepl("line", tolower(
+        attributes(ruumiline_obj$geom)$class
+      ), fixed = TRUE))
     if (is_line) {
-
       ## 1. Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
       ## Peame need pärast ühendama
       # ruut::qgis_algorithm_search_by_word("Intersection")
       algorithm <- "native:intersection"
       conf$table <- sprintf("%s", layer_name)
       input <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "MultiLineString", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
+        conf = conf,
+        geometry_type = "MultiLineString",
+        srid = 3301,
+        checkPrimaryKeyUnicity = TRUE,
+        key = "id",
+        geometry_field = "geom"
       )
       conf$table <- sprintf("%s", "bb2_epk02t_grid")
       overlay <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "Polygon", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
+        conf = conf,
+        geometry_type = "Polygon",
+        srid = 3301,
+        checkPrimaryKeyUnicity = TRUE,
+        key = "id",
+        geometry_field = "geom"
       )
       output <- vars$tmp_gpkg_file_output_1 # ajutine fail
-      str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : ['fid'], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }", input, output, overlay)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      str <-
+        sprintf(
+          "{ 'INPUT' : '%s', 'INPUT_FIELDS' : ['fid'], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }",
+          input,
+          output,
+          overlay
+        )
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
-      ## 2. Ühendame ruudus olevad pinnad
+      ## 2. Ühendame ruudus olevad jooned
       # ruut::qgis_algorithm_search_by_word("Dissolve")
       algorithm <- "native:dissolve"
       input <- vars$tmp_gpkg_file_input_1 # ajutine fail
       output <- vars$tmp_gpkg_file_output_2 # ajutine fail
-      str <- sprintf("{ 'FIELD' : ['%s'], 'INPUT' : '%s', 'OUTPUT' : '%s' }", "id", input, output)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      str <-
+        sprintf("{ 'FIELD' : ['%s'], 'INPUT' : '%s', 'OUTPUT' : '%s' }",
+                "id",
+                input,
+                output)
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
       ## 3. Lisame joonte pikkused
       # ruut::qgis_algorithm_search_by_word("attributes")
       algorithm <- "qgis:exportaddgeometrycolumns"
       input <- vars$tmp_gpkg_file_input_2 # ajutine fail
-      epk02t_table_name <- sprintf("epk02t_%s", gsub("^data_", "", layer_name))
-      conf$table <- epk02t_table_name
-      output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
-      str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      output <- vars$tmp_gpkg_file_output_1 # ajutine fail
+      str <-
+        sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }",
+                input,
+                output)
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
       ## ------------- TIFF ----------------
-      ## 4. Lisame pindade joonelementide pikkused
+      ## 4. Ühendame ruudud asukoha atribuutide järele.
       # ruut::qgis_algorithm_search_by_word("attributes")
       algorithm <- "native:joinattributesbylocation"
       conf$table <- "bb2_epk02t_grid"
-      input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
-      conf$table <- epk02t_table_name
-      join <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
-      output <- vars$tmp_gpkg_file_output_1
-      str <- sprintf("{ 'DISCARD_NONMATCHING' : False, 'INPUT' : '%s', 'JOIN' : '%s', 'JOIN_FIELDS' : ['length'], 'METHOD' : 0, 'OUTPUT' : '%s', 'PREDICATE' : [1], 'PREFIX' : '' }", input, join, output)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      input <-
+        ruut::construct_to_gpkg_output_postgres_str(
+          conf = conf,
+          geometry_field = "geom",
+          geometry_type = "MultiPolygon",
+          srid = 3301,
+          checkPrimaryKeyUnicity = TRUE,
+          key = "id"
+        )
+      join <- vars$tmp_gpkg_file_input_1 # ajutine fail
+      output <- vars$tmp_gpkg_file_output_2
+      str <-
+        sprintf(
+          "{ 'DISCARD_NONMATCHING' : False, 'INPUT' : '%s', 'JOIN' : '%s', 'JOIN_FIELDS' : ['length'], 'METHOD' : 0, 'OUTPUT' : '%s', 'PREDICATE' : [1], 'PREFIX' : '' }",
+          input,
+          join,
+          output
+        )
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
       ## 5. Asendame NULL väärtustega 0.
       algorithm <- "qgis:advancedpythonfieldcalculator"
-      input <- vars$tmp_gpkg_file_input_1 # ajutine fail
-      output <- vars$tmp_gpkg_file_output_2
-      cmd <- sprintf("qgis_process run %s --FIELD_NAME='value' --FIELD_TYPE=1 --FIELD_LENGTH=4 --FIELD_PRECISION=2 --GLOBAL=\"def getValue(x):
+      input <- vars$tmp_gpkg_file_input_2 # ajutine fail
+      output <- vars$tmp_gpkg_file_output_1
+      cmd <-
+        sprintf(
+          "qgis_process run %s --FIELD_NAME='value' --FIELD_TYPE=1 --FIELD_LENGTH=4 --FIELD_PRECISION=2 --GLOBAL=\"def getValue(x):
     if not x:
         value = 0
     else:
         value = round(x,1)
-    return value\" --FORMULA='value = getValue( <length> )' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
-      system(cmd)
-
-      ## 6. Eemaldame üleliigsed veerud
-      # ruut::qgis_algorithm_search_by_word("Drop ")
-      algorithm <- "native:deletecolumn"
-      input <- vars$tmp_gpkg_file_input_2
-      conf$table <- sprintf("grid_%s", gsub("^data_", "", layer_name))
-      output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "LineString", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "fid")
-      cmd <- sprintf("qgis_process run %s --COLUMN='length' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
+    return value\" --FORMULA='value = getValue( <length> )' --INPUT='%s' --OUTPUT='%s' ",
+          algorithm,
+          input,
+          output
+        )
       system(cmd)
 
-      ## 7. tif salvestamine tmp kataloogi
-      grid_layer <- sf::read_sf(dsn = conn, layer = c(conf$schema, "bb2_epk02t_grid"))
+      ## 6. tif salvestamine tmp kataloogi
+      conn <- ruut::db_connect(conf = conf)
+      grid_layer <-
+        sf::read_sf(dsn = conn,
+                    layer = c(conf$schema, "bb2_epk02t_grid"))
+      DBI::dbDisconnect(conn)
       # sf::st_crs(grid_layer) <- 3301
       pk_attributes <- attributes(grid_layer$geom)
-      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)
+      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
+        )
       # ruut::qgis_algorithm_search_by_word("Rasterize")
       algorithm <- "gdal:rasterize"
       # ruut::qgis_show_help(algorithm = algorithm)
-      input <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "MultiLineString", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
-      )
-      tif_file_name <- sprintf("tif_%s", gsub("^data_", "", layer_name))
+      input <- vars$tmp_gpkg_file_input_1
+      tif_file_name <-
+        sprintf("tif_%s", gsub("^data_", "", layer_name))
       output <- sprintf("%s/%s.tif", vars$tmp_dir, tif_file_name)
-      str <- sprintf("{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'value', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }", extent, input, output)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      str <-
+        sprintf(
+          "{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'value', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }",
+          extent,
+          input,
+          output
+        )
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd) # Salvestame kataloogi
 
-      ## 8. ----------- TIF TO POSTGIS -------------
-      (cmd <- sprintf(
-        "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
-        conf$password, output, conf$schema, tif_file_name, conf$user, conf$dbname, conf$host, conf$port
-      ))
+      ## 7. ----------- TIF TO POSTGIS -------------
+      (
+        cmd <- sprintf(
+          "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
+          conf$password,
+          output,
+          conf$schema,
+          tif_file_name,
+          conf$user,
+          conf$dbname,
+          conf$host,
+          conf$port
+        )
+      )
+      system(cmd)
+
+      ## 8. Eemaldame üleliigsed veerud ja salvestame tiff alusfaili andmebaasi
+      # ruut::qgis_algorithm_search_by_word("Drop ")
+      algorithm <- "native:deletecolumn"
+      input <- vars$tmp_gpkg_file_input_1
+      conf$table <-
+        sprintf("grid_%s", gsub("^data_", "", layer_name))
+      output <-
+        ruut::construct_to_gpkg_output_postgres_str(
+          conf = conf,
+          geometry_field = "geom",
+          geometry_type = "LineString",
+          srid = 3301,
+          checkPrimaryKeyUnicity = FALSE,
+          key = "id"
+        )
+      cmd <-
+        sprintf(
+          "qgis_process run %s --COLUMN='fid' --COLUMN='left' --COLUMN='top' --COLUMN='right' --COLUMN='bottom' --COLUMN='length' --INPUT='%s' --OUTPUT='%s' ",
+          algorithm,
+          input,
+          output
+        )
       system(cmd)
+
+      ## 9. Kustutame geom veeru.
+      conn <- ruut::db_connect(conf = conf)
+      q <-
+        sprintf("ALTER TABLE %s.%s DROP COLUMN geom;",
+                conf$schema,
+                conf$table)
+      cat(sprintf("\n-----------------\n%s\n\n", q))
+      DBI::dbSendQuery(conn, q)
+      ## 11. Vaakum.
+      q <-
+        sprintf(
+          "VACUUM (FULL, FREEZE, VERBOSE, ANALYZE, INDEX_CLEANUP) %s.%s;",
+          conf$schema,
+          conf$table
+        )
+      cat(sprintf("\n-----------------\n%s\n\n", q))
+      DBI::dbSendQuery(conn, q)
+      DBI::dbDisconnect(conn)
     }
   }
   ## Layers list

+ 3 - 5
R/pk_teisendame_polygoone.R

@@ -35,15 +35,13 @@ pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
   # Layers list
   layer_names <- ruut::db_schema_tablenames(conf = conf)
   layer_names <-
-    layer_names[!layer_names %in% c("a00_bb2",
+    layer_names[!layer_names %in% c("a00_piir",
+                                    "a00_bb2",
                                     "a00_piir_bb",
                                     "bb2_epk10t_grid",
                                     "bb2_epk02t_grid",
                                     "bb2_epk2t_grid")]
-  # layer_names <-
-  #   c("a00_piir", layer_names[grepl("^data_", layer_names)])
-  layer_names <-
-    c(layer_names[grepl("^data_", layer_names)])
+  layer_names <- layer_names[grepl("^data_a_", layer_names)]
   for (layer_name in layer_names) {
     conn <- ruut::db_connect(conf = conf)
     try(ruumiline_obj <-

+ 125 - 30
R/pk_teisendame_punkte.R

@@ -25,69 +25,164 @@ pk_teisendame_punkte <- function(obj = NULL, conf = NULL) {
   conf <- vars$conf
   ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
   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")
+    cat(
+      "\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n"
+    )
     return(NULL)
   }
 
   ## ====================== teisendused =====================
-  conn <- ruut::db_connect(conf = conf)
   # Layers list
   layer_names <- ruut::db_schema_tablenames(conf = conf)
-  layer_names <- layer_names[!layer_names %in% c("a00_bb2", "a00_piir_bb", "bb2_epk10t_grid", "bb2_epk02t_grid", "bb2_epk2t_grid")]
-  layer_names <- c("a00_piir", layer_names[grepl("^data_", layer_names)])
+  layer_names <-
+    layer_names[!layer_names %in% c(
+      "a00_piir",
+      "a00_bb2",
+      "a00_piir_bb",
+      "bb2_epk10t_grid",
+      "bb2_epk02t_grid",
+      "bb2_epk2t_grid"
+    )]
+  layer_names <- layer_names[grepl("^data_p_", layer_names)]
   for (layer_name in layer_names) {
-    try(ruumiline_obj <- sf::st_read(dsn = conn, layer = c(conf$schema, layer_name)))
+    conn <- ruut::db_connect(conf = conf)
+    try(ruumiline_obj <-
+          sf::st_read(dsn = conn,
+                      layer = c(conf$schema, layer_name)))
+    DBI::dbDisconnect(conn)
     ## ------------- Punktide arv ruudus ---------------
     ## Kontlrollime kas geomeetriline objekt on punkt.
-    (is_point <- any(grepl("point", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE)))
+    (is_point <-
+       any(grepl(
+         "point", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE
+       )))
     if (is_point) {
-
       ## 1. Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
       ## Peame need pärast ühendama
       # ruut::qgis_algorithm_search_by_word("Count")
       algorithm <- "native:countpointsinpolygon"
       conf$table <- sprintf("%s", layer_name)
       points <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "Point", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
+        conf = conf,
+        geometry_type = "Point",
+        srid = 3301,
+        checkPrimaryKeyUnicity = TRUE,
+        key = "id",
+        geometry_field = "geom"
       )
       conf$table <- sprintf("%s", "bb2_epk02t_grid")
       polygons <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "MultiPolygon", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
+        conf = conf,
+        geometry_type = "Polygon",
+        srid = 3301,
+        checkPrimaryKeyUnicity = TRUE,
+        key = "id",
+        geometry_field = "geom"
       )
-      epk02t_table_name <- sprintf("grid_%s", gsub("^data_", "", layer_name))
-      conf$table <- epk02t_table_name
-      output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Point", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
-      str <- sprintf("{ 'CLASSFIELD' : '', 'FIELD' : 'value', 'OUTPUT' : '%s', 'POINTS' : '%s', 'POLYGONS' : '%s', 'WEIGHT' : '' }", output, points, polygons)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      output <- vars$tmp_gpkg_file_output_1 # ajutine fail
+      str <-
+        sprintf(
+          "{ 'CLASSFIELD' : '', 'FIELD' : 'value', 'OUTPUT' : '%s', 'POINTS' : '%s', 'POLYGONS' : '%s', 'WEIGHT' : '' }",
+          output,
+          points,
+          polygons
+        )
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
       ## 2. ------------- TIFF ----------------
-      grid_layer <- sf::read_sf(dsn = conn, layer = c(conf$schema, "bb2_epk02t_grid"))
+      conn <- ruut::db_connect(conf = conf)
+      grid_layer <-
+        sf::read_sf(dsn = conn,
+                    layer = c(conf$schema, "bb2_epk02t_grid"))
+      DBI::dbDisconnect(conn)
       # sf::st_crs(grid_layer) <- 3301
       pk_attributes <- attributes(grid_layer$geom)
-      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)
+      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
+        )
       # ruut::qgis_algorithm_search_by_word("Rasterize")
       algorithm <- "gdal:rasterize"
       # ruut::qgis_show_help(algorithm = algorithm)
-      conf$table <- epk02t_table_name
-      input <- ruut::construct_to_gpkg_output_postgres_str(
-        conf = conf, geometry_type = "MultiPolygon", srid = 3301,
-        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
-      )
-      tif_file_name <- sprintf("tif_%s", gsub("^data_", "", layer_name))
+      input <- vars$tmp_gpkg_file_input_1
+      tif_file_name <-
+        sprintf("tif_%s", gsub("^data_", "", layer_name))
       output <- sprintf("%s/%s.tif", vars$tmp_dir, tif_file_name)
-      str <- sprintf("{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'value', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }", extent, input, output)
-      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      str <-
+        sprintf(
+          "{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'value', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }",
+          extent,
+          input,
+          output
+        )
+      cmd <-
+        ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd) # Salvestame kataloogi
 
       ## 3. ----------- TIF TO POSTGIS -------------
-      (cmd <- sprintf(
-        "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
-        conf$password, output, conf$schema, tif_file_name, conf$user, conf$dbname, conf$host, conf$port
-      ))
+      (
+        cmd <- sprintf(
+          "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
+          conf$password,
+          output,
+          conf$schema,
+          tif_file_name,
+          conf$user,
+          conf$dbname,
+          conf$host,
+          conf$port
+        )
+      )
       system(cmd)
+
+      ## 4. Eemaldame üleliigsed veerud ja salvestame tiff alusfaili andmebaasi
+      # ruut::qgis_algorithm_search_by_word("Drop ")
+      algorithm <- "native:deletecolumn"
+      input <- vars$tmp_gpkg_file_input_1
+      conf$table <-
+        sprintf("grid_%s", gsub("^data_", "", layer_name))
+      output <-
+        ruut::construct_to_gpkg_output_postgres_str(
+          conf = conf,
+          geometry_field = "geom",
+          geometry_type = "Point",
+          srid = 3301,
+          checkPrimaryKeyUnicity = FALSE,
+          key = "id"
+        )
+      cmd <-
+        sprintf(
+          "qgis_process run %s --COLUMN='fid' --COLUMN='left' --COLUMN='top' --COLUMN='right' --COLUMN='bottom'  --INPUT='%s' --OUTPUT='%s' ",
+          algorithm,
+          input,
+          output
+        )
+      system(cmd)
+
+      ## 5. Kustutame geom veeru.
+      conn <- ruut::db_connect(conf = conf)
+      q <-
+        sprintf("ALTER TABLE %s.%s DROP COLUMN geom;",
+                conf$schema,
+                conf$table)
+      cat(sprintf("\n-----------------\n%s\n\n", q))
+      DBI::dbSendQuery(conn, q)
+      ## 6. Vaakum.
+      q <-
+        sprintf(
+          "VACUUM (FULL, FREEZE, VERBOSE, ANALYZE, INDEX_CLEANUP) %s.%s;",
+          conf$schema,
+          conf$table
+        )
+      cat(sprintf("\n-----------------\n%s\n\n", q))
+      DBI::dbSendQuery(conn, q)
+      DBI::dbDisconnect(conn)
     }
   }
   ## Layers list