minu_teed_riigiteed.R 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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::minu_teed_riigiteed()], [estmap::minu_teed_muud_teed()], [estmap::minu_teed_eesti()]
  8. #' @keywords postgis
  9. #' @export
  10. #' @examples
  11. #' \dontrun{
  12. #'
  13. #' conf <- ruut::get_config()
  14. #' minu_teed_riigiteed(conf = conf)
  15. #' }
  16. minu_teed_riigiteed <- function(conf = NULL) {
  17. ## --------------------- muutujad ja teisendused -----------------------------
  18. vars <- ajutised_muutujad(conf = conf)
  19. conf <- vars$conf
  20. ## ------------------------- 1.1 Riigiteed osadena ---------------------------
  21. ## Kasutame ETAK andmestikku 'e_501_tee_j'.
  22. ## s.o (ilma rampideta) <= 30, koos rampide ja muude riigiteedega <= 45.
  23. # ruut::qgis_algorithm_search_by_word("extract")
  24. algorithm <- "native:extractbyattribute"
  25. # ruut::qgis_show_help(algorithm = algorithm)
  26. conf$schema <- "eesti"
  27. conf$table <- "e_501_tee_j"
  28. input <- ruut::construct_to_gpkg_output_postgres_str(
  29. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  30. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  31. )
  32. output <- vars$tmp_gpkg_file_output_1
  33. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '45' }", input, output)
  34. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  35. system(cmd)
  36. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  37. ## ------------------ 1.2 Copy temp gpkg file to postgis ---------------------
  38. conf$schema <- "minu_teed"
  39. conf$table <- "teedevork_riigiteed_osadena"
  40. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  41. 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_1, "LINESTRING", conf$schema, conf$table)
  42. system(cmd)
  43. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  44. ## ----------------------- 2.1 Riigiteed tervikuna ---------------------------
  45. # ruut::qgis_algorithm_search_by_word("dissolve")
  46. algorithm <- "native:dissolve"
  47. # ruut::qgis_show_help(algorithm = algorithm)
  48. input <- vars$tmp_gpkg_file_input_1
  49. output <- vars$tmp_gpkg_file_output_2
  50. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', '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. ## ------------------- 2.2 Eemaldame ebavajalikud veerud ---------------------
  55. # ruut::qgis_algorithm_search_by_word("retain")
  56. algorithm <- "native:retainfields"
  57. # ruut::qgis_show_help(algorithm = algorithm)
  58. input <- vars$tmp_gpkg_file_input_2
  59. output <- vars$tmp_gpkg_file_output_3
  60. cmd <- sprintf("qgis_process run %s --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  61. system(cmd)
  62. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  63. ## ------------------ 2.3 Copy temp gpkg file to postgis ---------------------
  64. conf$schema <- "minu_teed"
  65. conf$table <- "teedevork_riigiteed"
  66. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  67. 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, "MULTILINESTRING", conf$schema, conf$table)
  68. system(cmd)
  69. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  70. ## ----------------- 3.1 Riigiteede alg- ja lõpppunkt -----------------------
  71. # ruut::qgis_algorithm_search_by_word("vertices")
  72. algorithm <- "native:extractspecificvertices"
  73. # ruut::qgis_show_help(algorithm = algorithm)
  74. input <- vars$tmp_gpkg_file_input_3
  75. output <- vars$tmp_gpkg_file_output_4
  76. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
  77. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  78. system(cmd)
  79. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  80. ## ------------------ 3.2 Copy temp gpkg file to postgis ---------------------
  81. conf$schema <- "minu_teed"
  82. conf$table <- "teedevork_riigiteed_p"
  83. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  84. 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_4, "POINT", conf$schema, conf$table)
  85. system(cmd)
  86. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  87. }