ソースを参照

Alustatud salvestamisel andmebaasi nimede muutmist. Nimesid muudame nii, et QGIS avades jääksid alumised kihid andmebaasis kõige ülesse. Lisaks polügoonid allapoole kui jooned ja jooned allapoole kui punktid. Muudetud 'pk_sellest_alustame_db_loomist'.

Ardo Kubjas 5 年 前
コミット
768f45f5c3
2 ファイル変更67 行追加36 行削除
  1. 46 0
      R/ajutised_muutujad.R
  2. 21 36
      R/pk_sellest_alustame_db_loomist.R

+ 46 - 0
R/ajutised_muutujad.R

@@ -0,0 +1,46 @@
+ajutised_muutujad <- function(pk = NULL, obj = NULL, conf = NULL) {
+  ## Tühi list muutuja, mida funktsioon tagastab.
+  l <- list()
+  ## Objekti muutujale väärtuste omistamine, kui seda pole antud.
+  if (is.null(obj)) {
+    obj <- "demo"
+  }
+  obj <- gsub(" ", "_", tolower(obj))
+  ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
+  if (is.null(conf)) {
+    conf <- ruut::get_config()
+    conf$schema <- obj
+    l$conf <- conf
+  }
+  ## Piirkonna muutujale väärtuste omistamine, kui seda pole antud.
+  if (is.null(pk)) {
+    # geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658300,6474800],[658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],[658300,6474800]]}')
+    geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658400,6474800],[658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],[659200,6474950],[658400,6474800]]}')
+    pk <- sf::read_sf(geojson_txt)
+    pk <- sf::st_cast(pk, "POLYGON")
+    sf::st_crs(pk) <- 3301
+    attributes(pk)$names <- attributes(pk)$sf_column <- "geom"
+  }
+  ## ------------- muutujad ja teisendused ---------------
+  tmp_dir <- tempdir() # ajutine kataloog
+  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)
+  # 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))
+  sf::st_layers(dsn = tmp_gpkg_file)
+  # unlink(l$tmp_gpkg_file)
+  l$obj <- obj
+  l$tmp_gpkg_file <- tmp_gpkg_file
+  l$tmp_dir <- tmp_dir
+  l$pk <- pk
+  return(l)
+}

+ 21 - 36
R/pk_sellest_alustame_db_loomist.R

@@ -33,50 +33,35 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
   ## ----------- argumentide vastavuse kontroll -----------
   if (is.null(pk) || !sf::st_is_valid(pk)) {
     cat("\nPalun kontrolli geomeetrilise kujundi \u00F%igsust.\n")
-    return(NULL)
+    # return(NULL)
   }
-  attributes(pk)$names <- attributes(pk)$sf_column <- "geom"
   if (is.null(obj)) {
     cat("\nPalun sisesta objekti nimi.\n")
-    return(NULL)
-  }
-  obj <- gsub(" ", "_", tolower(obj))
-  ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
-  if (is.null(conf)) {
-    conf <- ruut::get_config()
-    conf$table <- obj
+    # return(NULL)
   }
-  ## ------------- muutujad ja teisendused ---------------
-  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(pk, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
-  # unlink(tmp_gpkg_file)
+  vars <- ajutised_muutujad(pk, obj, conf)
+  obj <- vars$obj
+  pk <- vars$pk
+  conf <- vars$conf
+  attributes(pk)$names <- attributes(pk)$sf_column <- "geom"
+
 
-  ## ------------------- programmi osa -----------------
+  ## --------------------- programmi osa ------------------------
 
   ## 1. Piirjoone salvestamine
   pk <- sf::st_transform(pk, 3301)
   # write to postgis
-  conf$schema <- conf$gpkg_file
   ruut::db_create_new_schema(conf = conf)
   conn <- ruut::db_connect(conf = conf)
-  sf::write_sf(pk, dsn = conn, layer = c(conf$schema, "piir"), driver = "PostgreSQL", fid_column_name = "id", delete_dsn = F)
-
+  sf::write_sf(pk, dsn = conn, layer = c(conf$schema, "a120_piir"), driver = "PostgreSQL", fid_column_name = "id", delete_dsn = F)
 
   ## 2. Piirikasti salvestamine
   ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
-  pk_attributes <- attributes(pk$geometry)
+  pk_attributes <- attributes(pk$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)
   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 <- "bb"
+  conf$table <- "a110_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"
@@ -85,11 +70,10 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
   cat(sprintf("\n%s\n", cmd))
   system(cmd)
 
-
   ## 3. epk2t piirikasti konstrueerimine ja salvestamine s.t kuhu mahuvad sisse
   ## meie piirkonna epk2t (1x1km) ruudud.
+
   ## 3.1 Leiame piirikastiga seotud epk2t ruudud
-  # str <- "{ 'INPUT' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable  password=\'osm\' key=\'id\' srid=3301 type=MultiPolygon checkPrimaryKeyUnicity=\'1\' table=\"maaamet\".\"epk2t_grid\" (geom)', 'INTERSECT' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/marja.gpkg|layername=bb', 'OUTPUT' : 'TEMPORARY_OUTPUT', 'PREDICATE' : [5,6] }"
   # ruut::qgis_algorithm_search_by_word(str = "extract")
   algorithm <- "native:extractbylocation"
   # cat(ruut::qgis_show_help(algorithm = algorithm))
@@ -99,21 +83,21 @@ 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 <- "bb"
+  conf$table <- "a110_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 <- tmp_gpkg_file_output # ajutine fail
+  output <- vars$tmp_gpkg_file_output_1 # ajutine fail
   str <- sprintf("{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }", input, intersect, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str, algorithm)
   system(cmd)
 
-  ## Ajutise faili abil piirikasti konstrueerimine
-  test_layer_obj <- sf::read_sf(dsn = tmp_gpkg_file, layer = conf$gpkg_table)
+  ## 3.2 Ajutise faili abil epk2t piirikasti konstrueerimine
+  test_layer_obj <- sf::read_sf(dsn = vars$tmp_gpkg_file, layer = "test_layer_1")
   pk_attributes <- attributes(test_layer_obj$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)
   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 <- "bb2"
+  conf$table <- "a100_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"
@@ -121,8 +105,9 @@ pk_sellest_alustame_db_loomist <- function(pk = NULL, obj = NULL, conf = NULL) {
   cmd <- sprintf("qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"", algorithm, extent, hspacing, vspacing, output)
   cat(sprintf("\n%s\n", cmd))
   system(cmd)
-  # Layers list
+
+  ## 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)
 }