Bläddra i källkod

Päevakoopia.

Ardo Kubjas 5 år sedan
förälder
incheckning
ab8b941e28
1 ändrade filer med 195 tillägg och 122 borttagningar
  1. 195 122
      R/minu_teed_loomine.R

+ 195 - 122
R/minu_teed_loomine.R

@@ -30,61 +30,26 @@ minu_teed_loomine <- function(conf = NULL) {
   conf$gpkg_file <- obj
   conf$primary_key <- "id"
   ## Loome lingid gpkg ajutistest tabelitest lugemiseks ja kirjutamiseks
+  conf$gpkg_table <- layer_6 <- "test_layer_6"
+  tmp_gpkg_file_output_6 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
+  tmp_gpkg_file_input_6 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
+  conf$gpkg_table <- layer_5 <- "test_layer_5"
+  tmp_gpkg_file_output_5 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
+  tmp_gpkg_file_input_5 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
+  conf$gpkg_table <- layer_4 <- "test_layer_4"
+  tmp_gpkg_file_output_4 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
+  tmp_gpkg_file_input_4 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
   conf$gpkg_table <- layer_3 <- "test_layer_3"
   tmp_gpkg_file_output_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
   tmp_gpkg_file_input_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  conf$gpkg_table <- layer_2  <- "test_layer_2"
+  conf$gpkg_table <- layer_2 <- "test_layer_2"
   tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
   tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  conf$gpkg_table <- layer_1  <- "test_layer_1"
+  conf$gpkg_table <- layer_1 <- "test_layer_1"
   tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
   tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
 
 
-  # ## =================== 1. 'riigiteedevõrk' =======================
-  # ## Teeregistri WFS avaandmete abil riigiteedevõrgu loomine
-  # # Loe instaleeritud tabelid andmebaasis
-  # installed_tables <- ruut::db_schema_tablenames(conf = conf)
-  # ## Kontrollime ühendatavate andmebaaside olemasolu.
-  # if (all(c("pohimaantee", "ramp", "tugimaantee", "korvalmaantee") %in% installed_tables)) {
-  #   ## 1. Ühendame riigiteed üheks võrgustikuks
-  #   # ruut::qgis_algorithm_search_by_word("merge")
-  #   algorithm <- "native:mergevectorlayers"
-  #   # ruut::qgis_show_help(algorithm = algorithm)
-  #   conf$schema <- "teeregister_wfs"
-  #   conf$table <- "pohimaantee"
-  #   layer_1 <- ruut::construct_to_gpkg_output_postgres_str(
-  #     conf = conf, geometry_type = "MultiLineString", srid = 3301,
-  #     checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
-  #   )
-  #   conf$table <- "tugimaantee"
-  #   layer_2 <- ruut::construct_to_gpkg_output_postgres_str(
-  #     conf = conf, geometry_type = "MultiLineString", srid = 3301,
-  #     checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
-  #   )
-  #   conf$table <- "korvalmaantee"
-  #   layer_3 <- ruut::construct_to_gpkg_output_postgres_str(
-  #     conf = conf, geometry_type = "MultiLineString", srid = 3301,
-  #     checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
-  #   )
-  #   # conf$table <- "ramp"
-  #   # layer_4 <- ruut::construct_to_gpkg_output_postgres_str(
-  #   #   conf = conf, geometry_type = "MultiLineString", srid = 3301,
-  #   #   checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
-  #   # )
-  #   conf$schema <- "minu_teed"
-  #   # New schema
-  #   ruut::db_create_new_schema(conf = conf)
-  #   conf$table <- "riigiteedevork"
-  #   output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = FALSE)
-  #   cmd <- sprintf(
-  #     "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ",
-  #     layer_1, layer_2, layer_3, output
-  #   )
-  #   system(cmd)
-  #   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
-  # }
-
 
   ## ================= Schema 'minu_teed' ====================
   conf$schema <- "minu_teed"
@@ -92,7 +57,8 @@ minu_teed_loomine <- function(conf = NULL) {
 
 
   ## =================== 2. teedevõrkude loomine =======================
-  ## 2.1 Riigiteedevõrk (ilma rampideta) <= 30, koos rampide ja muude riigiteedega <= 45.
+  ## ------------------ 2.1.1 Riigiteed osadena -----------------------
+  ## s.o (ilma rampideta) <= 30, koos rampide ja muude riigiteedega <= 45.
   # ruut::qgis_algorithm_search_by_word("extract")
   algorithm <- "native:extractbyattribute"
   # ruut::qgis_show_help(algorithm = algorithm)
@@ -107,16 +73,60 @@ minu_teed_loomine <- function(conf = NULL) {
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
-  ## 2.1.1 Copy temp gpkg file to postgis.
+  ## 2.1.2 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "teedevork_riigiteed_osadena"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_1, "LINESTRING", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+
+  ## ---------------- 2.2.1 Riigiteed tervikuna ------------------
+  # ruut::qgis_algorithm_search_by_word("dissolve")
+  algorithm <- "native:dissolve"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_1
+  output <- tmp_gpkg_file_output_2
+  str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  ## 2.2.2 Eemaldame ebavajalikud veerud
+  # ruut::qgis_algorithm_search_by_word("retain")
+  algorithm <- "native:retainfields"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_2
+  output <- tmp_gpkg_file_output_3
+  cmd <- sprintf("qgis_process run %s  --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
+  system(cmd)
+  ## 2.2.2 Copy temp gpkg file to postgis.
   conf$schema <- "minu_teed"
   conf$table <- "teedevork_riigiteed"
   PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
-  cmd <- sprintf('ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI', PG, tmp_gpkg_file, layer_1, "LINESTRING", conf$schema, conf$table)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_3, "MULTILINESTRING", conf$schema, conf$table)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+  ##  2.2.3 Riigiteede alg- ja lõpppunkt ---------------
+  # ruut::qgis_algorithm_search_by_word("vertices")
+  algorithm <- "native:extractspecificvertices"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_3
+  output <- tmp_gpkg_file_output_4
+  str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
+  ## 2.2.4 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "teedevork_riigiteed_p"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=fid -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_4, "POINT", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+
 
-  ## 2.2 Muud teed s.o tyyp IN (50,60,).
-  ## 2.2.1 Esmalt sorteerime >= 50
+  ## ------------------- 2.4 Muud teed osadena ----------------------
+  ## s.o tyyp IN (50,60,).
+  ## 2.4.1 Esmalt sorteerime >= 50
   # ruut::qgis_algorithm_search_by_word("extract")
   algorithm <- "native:extractbyattribute"
   # ruut::qgis_show_help(algorithm = algorithm)
@@ -131,35 +141,113 @@ minu_teed_loomine <- function(conf = NULL) {
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
-  ## 2.2.2 Seejärel sorteerime <= 60
+  ## 2.4.2 Seejärel sorteerime <= 60
+  algorithm <- "native:extractbyattribute"
   input <- tmp_gpkg_file_input_1
   output <- tmp_gpkg_file_output_2
   str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
-  ## 2.2.3 Copy temp gpkg file to postgis.
+
+  ## 2.4.3 Eemaldame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
+  algorithm <- "native:extractbyattribute"
+  input <- tmp_gpkg_file_input_2
+  output <- tmp_gpkg_file_output_3
+  str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 9, 'OUTPUT' : '%s' }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
+  ## 2.4.4 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "teedevork_kt_osadena"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_3, "LINESTRING", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+  ## 2.4.5 Ühendame sama teenumbriga muud teed
+  # ruut::qgis_algorithm_search_by_word("dissolve")
+  algorithm <- "native:dissolve"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_3
+  output <- tmp_gpkg_file_output_4
+  str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  ## 2.4.6 Eemaldame ebavajalikud veerud
+  # ruut::qgis_algorithm_search_by_word("retain")
+  algorithm <- "native:retainfields"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_4
+  output <- tmp_gpkg_file_output_5
+  cmd <- sprintf("qgis_process run %s  --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
+  system(cmd)
+  ## 2.4.7 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "teedevork_kt"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_5, "MULTILINESTRING", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+  ## 2.4.8 Muude teede alg- ja lõpppunkt
+  # ruut::qgis_algorithm_search_by_word("vertices")
+  algorithm <- "native:extractspecificvertices"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_5
+  output <- tmp_gpkg_file_output_6
+  str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
+  ## 2.4.9 Copy temp gpkg file to postgis.
   conf$schema <- "minu_teed"
-  conf$table <- "teedevork_muud_teed"
+  conf$table <- "teedevork_kt_p"
   PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
-  cmd <- sprintf('ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI', PG, tmp_gpkg_file, layer_2, "LINESTRING", conf$schema, conf$table)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=fid -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_6, "POINT", conf$schema, conf$table)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
-  ## 2.2.4 Leiame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
+
+
+  ## 2.5.1 Leiame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
+  algorithm <- "native:extractbyattribute"
   input <- tmp_gpkg_file_input_2
   output <- tmp_gpkg_file_output_3
   str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 8, 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
-  ## 2.2.5 Copy temp gpkg file to postgis.
+  ## 2.5.2 Eemaldame ebavajalikud veerud
+  # ruut::qgis_algorithm_search_by_word("retain")
+  algorithm <- "native:retainfields"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_3
+  output <- tmp_gpkg_file_output_4
+  cmd <- sprintf("qgis_process run %s  --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
+  system(cmd)
+  ## 2.5.3 Copy temp gpkg file to postgis.
   conf$schema <- "minu_teed"
-  conf$table <- "numbrita_kohalik_tee"
+  conf$table <- "teedevork_kt_numbrita"
   PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
-  cmd <- sprintf('ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI', PG, tmp_gpkg_file, layer_3, "LINESTRING", conf$schema, conf$table)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_4, "LINESTRING", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+  ## 2.5.3 Nimeta teede alg- ja lõpppunkt
+  # ruut::qgis_algorithm_search_by_word("vertices")
+  algorithm <- "native:extractspecificvertices"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  input <- tmp_gpkg_file_input_4
+  output <- tmp_gpkg_file_output_5
+  str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
+  ## 2.4.9 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "teedevork_kt_numbrita_p"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=fid -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_5, "POINT", conf$schema, conf$table)
   system(cmd)
   cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
-
 
 
   ## =================== 3. 'eesti' =======================
@@ -177,7 +265,6 @@ minu_teed_loomine <- function(conf = NULL) {
   str <- sprintf("{ 'FIELD' : [], 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
-
   ## 3.2 Kustutame ebaolulised väljad
   # ruut::qgis_algorithm_search_by_word("delete")
   algorithm <- "native:deletecolumn"
@@ -186,7 +273,6 @@ minu_teed_loomine <- function(conf = NULL) {
   output <- tmp_gpkg_file_output_2
   cmd <- sprintf("qgis_process run %s  --COLUMN='mnimi' --COLUMN='mkood' --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
   system(cmd)
-
   ## 3.3 Jaotame multipart polügooni singlepart polügoonideks.
   # ruut::qgis_algorithm_search_by_word("multipart")
   algorithm <- "native:multiparttosingleparts"
@@ -196,13 +282,12 @@ minu_teed_loomine <- function(conf = NULL) {
   str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
-
   ## 3.4 Lisame pindade pindalad ja perimeetri
   # ruut::qgis_algorithm_search_by_word("columns")
   algorithm <- "qgis:exportaddgeometrycolumns"
   # ruut::qgis_show_help(algorithm = algorithm)
   input <- tmp_gpkg_file_input_3
-  output <- tmp_gpkg_file_output_1
+  output <- tmp_gpkg_file_output_4
   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)
@@ -211,36 +296,31 @@ minu_teed_loomine <- function(conf = NULL) {
   # ruut::qgis_algorithm_search_by_word("extract")
   algorithm <- "native:extractbyattribute"
   # ruut::qgis_show_help(algorithm = algorithm)
-  input <- tmp_gpkg_file_input_1
-  output <- tmp_gpkg_file_output_2
-  # conf$schema <- "minu_teed"
-  # conf$table <- "eesti"
-  # output <- ruut::construct_to_gpkg_output_postgres_str(
-  #   conf = conf, key = "fid", geometry_field = "geom"
-  # )
+  input <- tmp_gpkg_file_input_4
+  output <- tmp_gpkg_file_output_5
   str <- sprintf("{ 'FIELD' : 'area', 'OPERATOR' : 2, 'VALUE' : '10000000', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
-
   ## 3.6 Eemaldame augud, mis on suuremad kui 1ha.
   # ruut::qgis_algorithm_search_by_word("holes")
   algorithm <- "native:deleteholes"
   # ruut::qgis_show_help(algorithm = algorithm)
-  input <- tmp_gpkg_file_input_2
-  conf$schema <- "minu_teed"
-  conf$table <- "eesti"
-  output <- ruut::construct_to_gpkg_output_postgres_str(
-    conf = conf, geometry_field = "geom"
-  )
+  input <- tmp_gpkg_file_input_5
+  output <- tmp_gpkg_file_output_6
   str <- sprintf("{ 'MIN_AREA' : '10000', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
+  ## 3.7 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "eesti"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=fid -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_6, "MULTIPOLYGON", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+
 
-  # Processing algorithm…
-  # Algorithm 'Polygons to lines' starting…
-  # Input parameters:
-  #   { 'INPUT' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable authcfg=0i30k14 key=\'fid\' srid=3301 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"minu_teed\".\"eesti\" (geom)', 'OUTPUT' : 'TEMPORARY_OUTPUT' }
-  ## 3.7 Salvestame eraldi eesti kontuurjooned.
+  ## ------------------ Teedevõrgu polügoniseerimine ------------------
+  ## 4.1 Teisendame eesti polügooni joonteks.
   # ruut::qgis_algorithm_search_by_word("lines")
   algorithm <- "native:polygonstolines"
   # ruut::qgis_show_help(algorithm = algorithm)
@@ -250,63 +330,58 @@ minu_teed_loomine <- function(conf = NULL) {
     conf = conf, geometry_type = "Polygon", srid = 3301,
     checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
   )
-  conf$schema <- "minu_teed"
-  conf$table <- "eesti_l"
-  output <- ruut::construct_to_gpkg_output_postgres_str(
-    conf = conf, geometry_field = "geom"
-  )
+  output <- tmp_gpkg_file_output_1
   str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
-
-
-  ## =================== 4. 'riigiteedevork_a' =======================
-  ## 4.0 Enne polügoniseerimist ühendame eesti kontuuri ja teedevõrgu üheks.
-  # ruut::qgis_algorithm_search_by_word("merge")
-  algorithm <- "native:mergevectorlayers"
+  ## 4.2 Eemaldame riigiteedest rambid.
+  algorithm <- "native:extractbyattribute"
   # ruut::qgis_show_help(algorithm = algorithm)
   conf$schema <- "minu_teed"
-  conf$table <- "eesti_l"
-  layer_1 <- ruut::construct_to_gpkg_output_postgres_str(
-    conf = conf, geometry_type = "MultiLineString", srid = 3301,
-    checkPrimaryKeyUnicity = TRUE, key = "id_1", geometry_field = "geom"
-  )
-  conf$schema <- "minu_teed"
-  conf$table <- "riigiteedevork"
-  layer_2 <- ruut::construct_to_gpkg_output_postgres_str(
+  conf$table <- "teedevork_riigiteed"
+  input <- ruut::construct_to_gpkg_output_postgres_str(
     conf = conf, geometry_type = "MultiLineString", srid = 3301,
     checkPrimaryKeyUnicity = TRUE, key = "id_0", geometry_field = "geom"
   )
+  output <- tmp_gpkg_file_output_2
+  str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '30' }", input, output)
+  cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
+  ## 4.3 Enne polügoniseerimist ühendame eesti kontuuri ja teedevõrgu üheks.
+  # ruut::qgis_algorithm_search_by_word("merge")
+  algorithm <- "native:mergevectorlayers"
+  # ruut::qgis_show_help(algorithm = algorithm)
+  layer_1 <- tmp_gpkg_file_input_1
+  layer_2 <- tmp_gpkg_file_input_2
   output <- tmp_gpkg_file_output_3
-  # conf$schema <- "minu_teed"
-  # conf$table <- "riigiteedevork_piiriga"
-  # output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom")
   cmd <- sprintf(
     "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ",
     layer_1, layer_2, output
   )
   system(cmd)
-  cat(sprintf("\n-------------\nLoodi teedev\U00F5rgu andmebaas '%s'.\n",conf$table))
-
-  ## 4.1 Polügoniseerime riigiteedevõrgu. Tekib teedega piiratud polügoonid.
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
+  ## 4.4 Polügoniseerime saadud riigiteedevõrgu. Tekib teedega piiratud polügoonid.
   # ruut::qgis_algorithm_search_by_word("Polygonize")
   algorithm <- "native:polygonize"
   # ruut::qgis_show_help(algorithm = algorithm)
   input <- tmp_gpkg_file_input_3
-  # conf$schema <- "minu_teed"
-  # conf$table <- "riigiteedevork_piiriga"
-  # input <- ruut::construct_to_gpkg_output_postgres_str(
-  #   conf = conf, geometry_type = "MultiLineString", srid = 3301,
-  #   checkPrimaryKeyUnicity = TRUE, key = "id_0", geometry_field = "geom"
-  # )
-  conf$schema <- "minu_teed"
-  conf$table <- "riigiteedevork_a"
-  output <- ruut::construct_to_gpkg_output_postgres_str(
-    conf = conf, geometry_field = "geom"
-  )
+  output <- tmp_gpkg_file_output_4
   str <- sprintf("{ 'INPUT' : '%s', 'KEEP_FIELDS' : False, 'OUTPUT' : '%s' }", input, output)
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
+  ## 4.5 Copy temp gpkg file to postgis.
+  conf$schema <- "minu_teed"
+  conf$table <- "teedevork_riigiteed_a"
+  PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
+  cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=fid -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, tmp_gpkg_file, layer_4, "MULTIPOLYGON", conf$schema, conf$table)
+  system(cmd)
+  cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
+
+
+
+
+  ## =================== !!!!!!!!!!!!!! POOLELI !!!!!!!!!!!! =======================
 
   ## 4.2 Lõikame Eesti kaardist polügoniseerime riigiteedevõrgu välja.
   ## Tekib ümber Eesti raam, mis tuleb jagada kuidagi transporditsoonideks.
@@ -334,7 +409,7 @@ minu_teed_loomine <- function(conf = NULL) {
   cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
   system(cmd)
 
-"Processing algorithm…
+  "Processing algorithm…
 Algorithm 'Intersection' starting…
 Input parameters:
 { 'INPUT' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable authcfg=0i30k14 key=\'fid\' srid=3301 type=MultiLineString checkPrimaryKeyUnicity=\'1\' table=\"minu_teed\".\"riigiteedevork\" (geom)', 'INPUT_FIELDS' : [], 'OUTPUT' : 'TEMPORARY_OUTPUT', 'OVERLAY' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable authcfg=0i30k14 key=\'id_0\' srid=3301 type=MultiPolygon checkPrimaryKeyUnicity=\'1\' table=\"minu_teed\".\"riigiteedevork_xxx\" (geom)', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }"
@@ -365,6 +440,4 @@ Input parameters:
   str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : [], '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)
-
-
 }