Kaynağa Gözat

Muudetud 'gpkg_piirkonnale' objektide lisamised.

Ardo Kubjas 5 yıl önce
ebeveyn
işleme
487d56689f

+ 29 - 27
R/gpkg_piirkonnale_joonte_lisamine.R

@@ -27,13 +27,16 @@ gpkg_piirkonnale_joonte_lisamine <- function(obj = NULL, gpkg_home = "/tmp") {
   }
   ## Konfiguratsiooni muutujale väärtuste omistamine
   conf <- ruut::get_config()
+  tmp_gpkg_file <- tempfile(fileext = ".gpkg")
+  conf$gpkg_home <- dirname(tmp_gpkg_file)
+  conf$gpkg_file <- gsub(paste0(conf$gpkg_home, "/"), "", tmp_gpkg_file)
+  conf$gpkg_file <- gsub(".gpkg", "", conf$gpkg_file)
+
   conf$gpkg_home <- gpkg_home
   conf$gpkg_file <- obj
-  postgres <- sprintf(
-    "postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'id\' srid=3301 type=Linestring checkPrimaryKeyUnicity=\'1\' ",
-    conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password
-  )
-  tmp_gpkg_file <- tempfile(fileext = ".gpkg")
+  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)
+
   ## Algorithm
   # ruut::qgis_algorithm_search_by_word(str = "extract")
   # algorithm <- "native:extractbylocation"
@@ -54,32 +57,31 @@ gpkg_piirkonnale_joonte_lisamine <- function(obj = NULL, gpkg_home = "/tmp") {
   andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
   andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
   for (intersect in intersect_layers) {
+    intersect_layer <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
     for (i in 1:nrow(andmed)) {
       conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
-      output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
-      ## !!! Trikk: alguses leiame ühisosaga piirkonnad
-      if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
-      result <- qgisprocess::qgis_run_algorithm(
-        algorithm = "native:extractbylocation",
-        INPUT = sprintf(
-          '%s table=\"%s\".\"%s\" (%s)',
-          postgres, andmed$schema[i], andmed$table[i], geom
-        ),
-        INTERSECT = sprintf("%s|layername=%s", dsn, intersect),
-        OUTPUT = tmp_gpkg_file,
-        PREDICATE = c(0)
+      conf$table <- andmed$table[i]
+      conf$schema <- andmed$schema[i]
+      output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
+      input <- ruut::construct_to_gpkg_output_postgres_str(
+        conf = conf, geometry_type = "Linestring", srid = 3301,
+        checkPrimaryKeyUnicity = TRUE, key = "id"
       )
+      ## !!! Trikk: alguses leiame ühisosaga piirkonnad
+      ## !!! Trikk: alguses leiame ühisosaga piirkonnad
+      ## ------------------- QGIS: qgisprocess ----------------------
+      str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", tmp_gpkg_file_output, "', 'PREDICATE' : [0] }")
+      algorithm <- "native:extractbylocation"
+      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      cat(sprintf("\n%s\n\n", cmd))
+      system(cmd)
       ## !!! Trikk jätkub: edasi leiame alles ühisosa
-      result <- qgisprocess::qgis_run_algorithm(
-        algorithm = "native:intersection",
-        INPUT = tmp_gpkg_file,
-        INPUT_FIELDS = "",
-        OVERLAY = sprintf("%s|layername=%s", dsn, intersect),
-        OVERLAY_FIELDS = "",
-        OVERLAY_FIELDS_PREFIX = "",
-        OUTPUT = output
-        # .quiet = TRUE
-      )
+      str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'OVERLAY' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'INPUT_FIELDS' : '', 'OVERLAY_FIELDS' : '', 'OVERLAY_FIELDS_PREFIX' : '' }")
+      str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
+      algorithm <- "native:intersection"
+      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      cat(sprintf("\n%s\n\n", cmd))
+      system(cmd)
     }
   }
   ## Layers list

+ 5 - 7
R/gpkg_piirkonnale_polygoonide_lisamine.R

@@ -34,7 +34,6 @@ gpkg_piirkonnale_polygoonide_lisamine <- function(obj = NULL, gpkg_home = "/tmp"
 
   conf$gpkg_home <- gpkg_home
   conf$gpkg_file <- obj
-  conf$gpkg_table <- "tmp_table"
   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)
 
@@ -55,7 +54,6 @@ gpkg_piirkonnale_polygoonide_lisamine <- function(obj = NULL, gpkg_home = "/tmp"
   andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
   andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
   for (intersect in intersect_layers) {
-    conf$gpkg_table <- intersect
     intersect_layer <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
     for (i in 1:nrow(andmed)) {
       conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
@@ -68,17 +66,17 @@ gpkg_piirkonnale_polygoonide_lisamine <- function(obj = NULL, gpkg_home = "/tmp"
       )
       ## !!! Trikk: alguses leiame ühisosaga piirkonnad
       ## ------------------- QGIS: qgisprocess ----------------------
-      str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", tmp_gpkg_file, "', 'PREDICATE' : [0] }")
+      str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", tmp_gpkg_file_output, "', 'PREDICATE' : [0] }")
       algorithm <- "native:extractbylocation"
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
-      cat(sprintf("\n%s\n", cmd))
+      cat(sprintf("\n%s\n\n", cmd))
       system(cmd)
       ## !!! Trikk jätkub: edasi leiame alles ühisosa
-      str <- paste0("{ 'INPUT' : '", tmp_gpkg_file, "', 'OVERLAY' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'INPUT_FIELDS' : '', 'OVERLAY_FIELDS' : '', 'OVERLAY_FIELDS_PREFIX' : '' }")
+      str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'OVERLAY' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'INPUT_FIELDS' : '', 'OVERLAY_FIELDS' : '', 'OVERLAY_FIELDS_PREFIX' : '' }")
       str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
       algorithm <- "native:intersection"
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
-      cat(sprintf("\n%s\n", cmd))
+      cat(sprintf("\n%s\n\n", cmd))
       system(cmd)
       ## Filtreerime maakasutuse kihi 'landuse-a' eraldi alamkihtideks
       if (andmed$table[i] == "landuse_a") {
@@ -88,7 +86,7 @@ gpkg_piirkonnale_polygoonide_lisamine <- function(obj = NULL, gpkg_home = "/tmp"
           table_suffix <- landuse_a$fclass[k]
           conf$gpkg_table <- parent_table
           input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
-          conf$gpkg_table <- sprintf("%s_%s",conf$gpkg_table, table_suffix)
+          conf$gpkg_table <- sprintf("%s_%s", conf$gpkg_table, table_suffix)
           output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = FALSE)
           str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'code', OPERATOR : 0, VALUE : '", landuse_a$code[k], "' }")
           algorithm <- "native:extractbyattribute"

+ 32 - 40
R/gpkg_piirkonnale_punktide_lisamine.R

@@ -27,13 +27,16 @@ gpkg_piirkonnale_punktide_lisamine <- function(obj = NULL, gpkg_home = "/tmp") {
   }
   ## Konfiguratsiooni muutujale väärtuste omistamine
   conf <- ruut::get_config()
+  tmp_gpkg_file <- tempfile(fileext = ".gpkg")
+  conf$gpkg_home <- dirname(tmp_gpkg_file)
+  conf$gpkg_file <- gsub(paste0(conf$gpkg_home, "/"), "", tmp_gpkg_file)
+  conf$gpkg_file <- gsub(".gpkg", "", conf$gpkg_file)
+
   conf$gpkg_home <- gpkg_home
   conf$gpkg_file <- obj
-  postgres <- sprintf(
-    "postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'id\' srid=3301 type=Point checkPrimaryKeyUnicity=\'1\' ",
-    conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password
-  )
-  tmp_gpkg_file <- tempfile(fileext = ".gpkg")
+  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)
+
   ## Algorithm
   # ruut::qgis_algorithm_search_by_word(str = "extract")
   # algorithm <- "native:extractbylocation"
@@ -58,50 +61,39 @@ gpkg_piirkonnale_punktide_lisamine <- function(obj = NULL, gpkg_home = "/tmp") {
   andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
   andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
   for (intersect in intersect_layers) {
+    intersect_layer <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
     for (i in 1:nrow(andmed)) {
-      conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
-      output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
-      ## !!! Trikk: alguses leiame ühisosaga piirkonnad
+      ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
       if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
-      result <- qgisprocess::qgis_run_algorithm(
-        algorithm = "native:extractbylocation",
-        INPUT = sprintf(
-          '%s table=\"%s\".\"%s\" (%s)',
-          postgres, andmed$schema[i], andmed$table[i], geom
-        ),
-        INTERSECT = sprintf("%s|layername=%s", dsn, intersect),
-        OUTPUT = tmp_gpkg_file,
-        PREDICATE = c(0)
-      )
-      ## !!! Trikk jätkub: edasi leiame alles ühisosa
-      result <- qgisprocess::qgis_run_algorithm(
-        algorithm = "native:intersection",
-        INPUT = tmp_gpkg_file,
-        INPUT_FIELDS = "",
-        OVERLAY = sprintf("%s|layername=%s", dsn, intersect),
-        OVERLAY_FIELDS = "",
-        OVERLAY_FIELDS_PREFIX = "",
-        OUTPUT = output
-        # .quiet = TRUE
+      conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
+      conf$table <- andmed$table[i]
+      conf$schema <- andmed$schema[i]
+      output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
+      input <- ruut::construct_to_gpkg_output_postgres_str(
+        conf = conf, geometry_type = "Point", srid = 3301,
+        checkPrimaryKeyUnicity = TRUE, key = "id"
       )
+      ## ------------------- QGIS: qgisprocess ----------------------
+      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)
+      cat(sprintf("\n%s\n\n", cmd))
+      system(cmd)
       ## Filtreerime aadressandmete kihi eraldi alamkihtideks
       if (andmed$table[i] == "aadressandmed") {
         aadressandmed <- unique(as.data.frame(sf::read_sf(dsn = dsn, layer = sprintf("%s_aadressandmed", intersect), as_tibble = T))[, c("adob_liik"), drop = FALSE])
         parent_table <- conf$gpkg_table
         for (k in 1:nrow(aadressandmed)) {
           table_suffix <- aadressandmed$adob_liik[k]
-          conf$gpkg_table <- sprintf("%s_%s", parent_table, table_suffix)
-          output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
-          result <- qgisprocess::qgis_run_algorithm(
-            algorithm = "native:extractbyattribute",
-            FIELD = "adob_liik",
-            OPERATOR = 0, # 0 s.o '='
-            VALUE = aadressandmed$adob_liik[k],
-            INPUT = sprintf("%s|layername=%s_%s", dsn, intersect, andmed$table[i]),
-            OUTPUT = output,
-            FAIL_OUTPUT = qgisprocess::qgis_tmp_vector()
-            # .quiet = TRUE
-          )
+          conf$gpkg_table <- parent_table
+          input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
+          conf$gpkg_table <- sprintf("%s_%s", conf$gpkg_table, table_suffix)
+          output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = FALSE)
+          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)
+          cat(sprintf("\n%s\n", cmd))
+          system(cmd)
         }
       }
     }