minu_teed_muud_teed.R 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #' Riigiteedevõrgu loomine
  2. #'
  3. #' ETAK (Eesti topograafia andmekogu) \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Eesti-topograafia-andmekogu/Laadi-ETAK-andmed-alla-p609.html} andmete põhjal
  4. #'
  5. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  6. #' @return No output.
  7. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()], [estmap::teeregister_wfs()]
  8. #' @keywords postgis
  9. #' @export
  10. #' @examples
  11. #' \dontrun{
  12. #'
  13. #' conf <- ruut::get_config()
  14. #' minu_teed_muud_teed(conf = conf)
  15. #' }
  16. minu_teed_muud_teed <- function(conf = NULL) {
  17. ## --------------------- muutujad ja teisendused -----------------------------
  18. vars <- ajutised_muutujad(conf = conf)
  19. conf <- vars$conf
  20. ## ------------------------- 1.1 Muud teed osadena ---------------------------
  21. ## Kasutame ETAK andmestikku 'e_501_tee_j'.
  22. ## s.o tyyp IN (50,60,).
  23. ## Esmalt sorteerime >= 50
  24. # ruut::qgis_algorithm_search_by_word("extract")
  25. algorithm <- "native:extractbyattribute"
  26. # ruut::qgis_show_help(algorithm = algorithm)
  27. conf$schema <- "eesti"
  28. conf$table <- "e_501_tee_j"
  29. input <- ruut::construct_to_gpkg_output_postgres_str(
  30. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  31. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  32. )
  33. output <- vars$tmp_gpkg_file_output_1
  34. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 3, 'OUTPUT' : '%s', 'VALUE' : '50' }", input, output)
  35. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  36. system(cmd)
  37. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  38. ## Seejärel sorteerime <= 60
  39. algorithm <- "native:extractbyattribute"
  40. input <- vars$tmp_gpkg_file_input_1
  41. output <- vars$tmp_gpkg_file_output_2
  42. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output)
  43. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  44. system(cmd)
  45. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  46. ## Eemaldame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
  47. algorithm <- "native:extractbyattribute"
  48. input <- vars$tmp_gpkg_file_input_2
  49. output <- vars$tmp_gpkg_file_output_3
  50. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 9, 'OUTPUT' : '%s' }", input, output)
  51. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  52. system(cmd)
  53. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  54. ## ------------------ 1.2 Copy temp gpkg file to postgis ---------------------
  55. conf$schema <- "minu_teed"
  56. conf$table <- "teedevork_kt_osadena"
  57. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  58. 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, vars$tmp_gpkg_file, vars$layer_3, "LINESTRING", conf$schema, conf$table)
  59. system(cmd)
  60. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  61. ## ----------------------- 2.1 Muud teed tervikuna ---------------------------
  62. ## Ühendame sama teenumbriga muud teed
  63. # ruut::qgis_algorithm_search_by_word("dissolve")
  64. algorithm <- "native:dissolve"
  65. # ruut::qgis_show_help(algorithm = algorithm)
  66. input <- vars$tmp_gpkg_file_input_3
  67. output <- vars$tmp_gpkg_file_output_4
  68. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  69. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  70. system(cmd)
  71. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  72. ## ------------------- 2.2 Eemaldame ebavajalikud veerud ---------------------
  73. # ruut::qgis_algorithm_search_by_word("retain")
  74. algorithm <- "native:retainfields"
  75. # ruut::qgis_show_help(algorithm = algorithm)
  76. input <- vars$tmp_gpkg_file_input_4
  77. output <- vars$tmp_gpkg_file_output_5
  78. cmd <- sprintf("qgis_process run %s --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  79. system(cmd)
  80. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  81. ## ------------------ 2.3 Copy temp gpkg file to postgis ---------------------
  82. conf$schema <- "minu_teed"
  83. conf$table <- "teedevork_kt"
  84. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  85. 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, vars$tmp_gpkg_file, vars$layer_5, "MULTILINESTRING", conf$schema, conf$table)
  86. system(cmd)
  87. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  88. ## ----------------- 3.1 Riigiteede alg- ja lõpppunkt -----------------------
  89. # ruut::qgis_algorithm_search_by_word("vertices")
  90. algorithm <- "native:extractspecificvertices"
  91. # ruut::qgis_show_help(algorithm = algorithm)
  92. input <- vars$tmp_gpkg_file_input_5
  93. output <- vars$tmp_gpkg_file_output_6
  94. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
  95. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  96. system(cmd)
  97. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  98. ## ------------------ 3.2 Copy temp gpkg file to postgis ---------------------
  99. conf$schema <- "minu_teed"
  100. conf$table <- "teedevork_kt_p"
  101. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  102. 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, vars$tmp_gpkg_file, vars$layer_6, "POINT", conf$schema, conf$table)
  103. system(cmd)
  104. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  105. ## ------------------- 4.1 Ilma tee numbrita muud teed -----------------------
  106. ## Leiame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
  107. algorithm <- "native:extractbyattribute"
  108. input <- vars$tmp_gpkg_file_input_2
  109. output <- vars$tmp_gpkg_file_output_3
  110. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 8, 'OUTPUT' : '%s' }", input, output)
  111. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  112. system(cmd)
  113. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  114. ## ................... 4.2 Eemaldame ebavajalikud veerud ---------------------
  115. # ruut::qgis_algorithm_search_by_word("retain")
  116. algorithm <- "native:retainfields"
  117. # ruut::qgis_show_help(algorithm = algorithm)
  118. input <- vars$tmp_gpkg_file_input_3
  119. output <- vars$tmp_gpkg_file_output_4
  120. cmd <- sprintf("qgis_process run %s --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  121. system(cmd)
  122. ## ------------------ 4.3 Copy temp gpkg file to postgis ---------------------
  123. conf$schema <- "minu_teed"
  124. conf$table <- "teedevork_kt_numbrita"
  125. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  126. 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, vars$tmp_gpkg_file, vars$layer_4, "LINESTRING", conf$schema, conf$table)
  127. system(cmd)
  128. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  129. ## ----------------- 4.4 Nimeta teede alg- ja lõpppunkt ----------------------
  130. # ruut::qgis_algorithm_search_by_word("vertices")
  131. algorithm <- "native:extractspecificvertices"
  132. # ruut::qgis_show_help(algorithm = algorithm)
  133. input <- vars$tmp_gpkg_file_input_4
  134. output <- vars$tmp_gpkg_file_output_5
  135. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
  136. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  137. system(cmd)
  138. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  139. ## ------------------ 4.5 Copy temp gpkg file to postgis ---------------------
  140. conf$schema <- "minu_teed"
  141. conf$table <- "teedevork_kt_numbrita_p"
  142. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  143. 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, vars$tmp_gpkg_file, vars$layer_5, "POINT", conf$schema, conf$table)
  144. system(cmd)
  145. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  146. }