minu_teed_loomine.R 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #' Teeregistri WFS ja maaameti oma teeandmete 'minu_teed' loomine
  2. #'
  3. #' 1. Põhi-, kõrval ja tugimaanteedest ning rambid moodustatakse uus andmebaas 'riigiteedevork'.
  4. #' 2. Ilma teenumbrita kohalike teede andmebaasi 'numbrita_kohalik_tee' loomine.
  5. #' 3. Eesti kaart ainult suurte saartega (üle 10km2).
  6. #' Eesti avaandmete \url{https://avaandmed.eesti.ee/datasets?emsId=35} teabevärava kaudu teederegistri WFS (Web Feature Service) andmete eksportimine postgisandmebaasi. WFS andmestiku kirjeldus: \url{https://teeregister-api.mnt.ee/teenus/wfs?request=GetCapabilities&service=WFS}
  7. #'
  8. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  9. #' @return No output.
  10. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()], [estmap::teeregister_wfs()]
  11. #' @keywords postgis, maps, WFS, OSM
  12. #' @export
  13. #' @examples
  14. #' \dontrun{
  15. #'
  16. #' conf <- ruut::get_config()
  17. #' minu_teed_loomine(conf = conf)
  18. #' }
  19. minu_teed_loomine <- function(conf = NULL) {
  20. ## ------------- muutujad ja teisendused ---------------
  21. if (is.null(conf)) {
  22. conf <- ruut::get_config()
  23. conf$schema <- "teeregister_wfs"
  24. }
  25. ## Defineerime ajutised failid ja muutujad.
  26. tmp_dir <- tempdir() # ajutine kataloog
  27. obj <- "demo"
  28. tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj) # ajutine gpkg fail
  29. conf$gpkg_home <- tmp_dir
  30. conf$gpkg_file <- obj
  31. conf$primary_key <- "id"
  32. ## Loome lingid gpkg ajutistest tabelitest lugemiseks ja kirjutamiseks
  33. conf$gpkg_table <- "test_layer_3"
  34. tmp_gpkg_file_output_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  35. tmp_gpkg_file_input_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  36. conf$gpkg_table <- "test_layer_2"
  37. tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  38. tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  39. conf$gpkg_table <- "test_layer_1"
  40. tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  41. tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  42. ## =================== 1. 'riigiteedevõrk' =======================
  43. ## Teeregistri avaandmete kihtide lugemine
  44. # Loe instaleeritud tabelid andmebaasis
  45. installed_tables <- ruut::db_schema_tablenames(conf = conf)
  46. ## Kontrollime ühendatavate andmebaaside olemasolu.
  47. if (all(c("pohimaantee", "ramp", "tugimaantee", "korvalmaantee") %in% installed_tables)) {
  48. ## 1. Ühendame riigiteed üheks võrgustikuks
  49. # ruut::qgis_algorithm_search_by_word("merge")
  50. algorithm <- "native:mergevectorlayers"
  51. # ruut::qgis_show_help(algorithm = algorithm)
  52. conf$table <- "pohimaantee"
  53. layer_1 <- ruut::construct_to_gpkg_output_postgres_str(
  54. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  55. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
  56. )
  57. conf$table <- "ramp"
  58. layer_2 <- ruut::construct_to_gpkg_output_postgres_str(
  59. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  60. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
  61. )
  62. conf$table <- "tugimaantee"
  63. layer_3 <- ruut::construct_to_gpkg_output_postgres_str(
  64. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  65. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
  66. )
  67. conf$table <- "korvalmaantee"
  68. layer_4 <- ruut::construct_to_gpkg_output_postgres_str(
  69. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  70. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry"
  71. )
  72. conf$schema <- "minu_teed"
  73. # New schema
  74. ruut::db_create_new_schema(conf = conf)
  75. conf$table <- "riigiteedevork"
  76. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "fid")
  77. cmd <- sprintf(
  78. "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ",
  79. layer_1, layer_2, layer_3, layer_4, output
  80. )
  81. system(cmd)
  82. cat(sprintf("\n%s\n-------------\n", "Loodi teedev\U00F5rgu andmebaas 'riigiteedevork'.\n"))
  83. }
  84. ## =================== 2. 'numbrita_kohalik_tee' =======================
  85. ## 1. Sorteerime välja ETAK andmebaasist "e_501_tee_j" kojalikud teed (s.o tyyp IN (50,60,))
  86. ## 1.1 Esmalt sorteerime >= 50
  87. # ruut::qgis_algorithm_search_by_word("extract")
  88. algorithm <- "native:extractbyattribute"
  89. # ruut::qgis_show_help(algorithm = algorithm)
  90. conf$schema <- "eesti"
  91. conf$table <- "e_501_tee_j"
  92. input <- ruut::construct_to_gpkg_output_postgres_str(
  93. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  94. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  95. )
  96. output <- tmp_gpkg_file_output_1
  97. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 3, 'OUTPUT' : '%s', 'VALUE' : '50' }", input, output)
  98. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  99. system(cmd)
  100. ## 1.2 Seejärel sorteerime <= 60
  101. input <- tmp_gpkg_file_input_1
  102. output <- tmp_gpkg_file_output_2
  103. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output)
  104. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  105. system(cmd)
  106. ## 1.3 Seejärel sorteerime '<= 60'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
  107. ## Järelejäävad teed on tegelikult 'muud teed'.
  108. input <- tmp_gpkg_file_input_2
  109. conf$schema <- "minu_teed"
  110. # New schema
  111. ruut::db_create_new_schema(conf = conf)
  112. conf$table <- "numbrita_kohalik_tee"
  113. output <- ruut::construct_to_gpkg_output_postgres_str(
  114. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  115. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  116. )
  117. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 8, 'OUTPUT' : '%s' }", input, output)
  118. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  119. system(cmd)
  120. cat(sprintf("\n%s\n-------------\n", "Loodi andmebaas 'numbrita_kohalik_tee'.\n"))
  121. ## =================== 3. 'eesti' =======================
  122. ## 3.1 Maakondadest Eesti maismaapiir
  123. # ruut::qgis_algorithm_search_by_word("dissolve")
  124. algorithm <- "native:dissolve"
  125. # ruut::qgis_show_help(algorithm = algorithm)
  126. conf$schema <- "maaamet"
  127. conf$table <- "maakond"
  128. input <- ruut::construct_to_gpkg_output_postgres_str(
  129. conf = conf, geometry_type = "MultiPolygon", srid = 3301,
  130. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  131. )
  132. output <- tmp_gpkg_file_output_1
  133. str <- sprintf("{ 'FIELD' : [], 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  134. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  135. system(cmd)
  136. ## 3.2 Kustutame ebaolulised väljad
  137. # ruut::qgis_algorithm_search_by_word("delete")
  138. algorithm <- "native:deletecolumn"
  139. # ruut::qgis_show_help(algorithm = algorithm)
  140. input <- tmp_gpkg_file_input_1
  141. output <- tmp_gpkg_file_output_2
  142. cmd <- sprintf("qgis_process run %s --COLUMN='mnimi' --COLUMN='mkood' --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  143. system(cmd)
  144. ## 3.3 Jaotame multipart polügooni singlepart polügoonideks.
  145. # ruut::qgis_algorithm_search_by_word("multipart")
  146. algorithm <- "native:multiparttosingleparts"
  147. # ruut::qgis_show_help(algorithm = algorithm)
  148. input <- tmp_gpkg_file_input_2
  149. output <- tmp_gpkg_file_output_3
  150. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  151. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  152. system(cmd)
  153. ## 3.4 Lisame pindade pindalad ja perimeetri
  154. # ruut::qgis_algorithm_search_by_word("columns")
  155. algorithm <- "qgis:exportaddgeometrycolumns"
  156. # ruut::qgis_show_help(algorithm = algorithm)
  157. input <- tmp_gpkg_file_input_3
  158. output <- tmp_gpkg_file_output_1
  159. str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  160. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  161. system(cmd)
  162. ## 3.5 Eemaldame objektid mille pindala on väiksem kui 10km2 (väikesaared)
  163. # ruut::qgis_algorithm_search_by_word("extract")
  164. algorithm <- "native:extractbyattribute"
  165. # ruut::qgis_show_help(algorithm = algorithm)
  166. input <- tmp_gpkg_file_input_1
  167. conf$schema <- "minu_teed"
  168. conf$table <- "eesti"
  169. output <- ruut::construct_to_gpkg_output_postgres_str(
  170. conf = conf, key = "fid", geometry_field = "geom"
  171. )
  172. str <- sprintf("{ 'FIELD' : 'area', 'OPERATOR' : 2, 'VALUE' : '10000000', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  173. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  174. system(cmd)
  175. }