minu_teed_loomine.R 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #' Teeregistri WFS ja maaameti andmete 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. #' 4. Polügoniseerime riigiteedevõrgu.
  7. #' 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}
  8. #'
  9. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  10. #' @return No output.
  11. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()], [estmap::teeregister_wfs()]
  12. #' @keywords postgis
  13. #' @export
  14. #' @examples
  15. #' \dontrun{
  16. #'
  17. #' conf <- ruut::get_config()
  18. #' minu_teed_loomine(conf = conf)
  19. #' }
  20. minu_teed_loomine <- function(conf = NULL) {
  21. ## ------------- muutujad ja teisendused ---------------
  22. vars <- ajutised_muutujad(conf = conf)
  23. conf <- vars$conf
  24. conf$schema <- "minu_teed"
  25. conf$table <- "zzz"
  26. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom")
  27. cat(paste(paste0('"', ruut::db_table_colnames(conf = conf)$column_name, '"'), collapse = ","))
  28. ## --------------------- 1.2 CSV tabeli import -------------------------------
  29. # ruut::qgis_algorithm_search_by_word("csvtools")
  30. algorithm <- "csvtools:loadcsvfile"
  31. # ruut::qgis_show_help(algorithm = algorithm)
  32. input <- vars$tmp_gpkg_file_input_1
  33. output <- vars$tmp_gpkg_file_output_2
  34. cmd <- sprintf("qgis_process run %s --FIELD_NAME='rahvaarv' --FIELD_TYPE=1 --FIELD_LENGTH=0 --FIELD_PRECISION=0 --GLOBAL=\"\" --FORMULA='if ( \"VAARTUS\" >= 1000000, 3, \"VAARTUS\")' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output) # Ma ei tea miks nii töötab, aga muud moodi ei saanud ühe teisendusega tööle. Veider.
  35. system(cmd)
  36. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  37. # Processing algorithm…
  38. # Algorithm 'Create vector layer from CSV file' starting…
  39. # Input parameters:
  40. # { 'CRS' : None, 'DECIMAL_POINT' : 0, 'DELIMITER' : 1, 'GEOMETRY_DATA' : 2, 'INPUT' : '/data/rahvaarv_ruutudes/GRID1000_MK12/Tabel_1_rahvaarv_GRD_1000_REL_2011.csv', 'OUTPUT' : 'TEMPORARY_OUTPUT', 'QUOTE_CHAR' : '\"', 'USE_HEADER' : True, 'WKT_FIELD' : '', 'X_FIELD' : '', 'Y_FIELD' : '' }
  41. ## =================== 3. 'eesti' =======================
  42. ## ------------------ Teedevõrgu polügoniseerimine ------------------
  43. ## 4.1 Teisendame eesti polügooni joonteks.
  44. # ruut::qgis_algorithm_search_by_word("lines")
  45. algorithm <- "native:polygonstolines"
  46. # ruut::qgis_show_help(algorithm = algorithm)
  47. conf$schema <- "minu_teed"
  48. conf$table <- "eesti"
  49. input <- ruut::construct_to_gpkg_output_postgres_str(
  50. conf = conf, geometry_type = "Polygon", srid = 3301,
  51. checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  52. )
  53. output <- vars$tmp_gpkg_file_output_1
  54. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  55. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  56. system(cmd)
  57. ## 4.2 Eemaldame riigiteedest rambid.
  58. algorithm <- "native:extractbyattribute"
  59. # ruut::qgis_show_help(algorithm = algorithm)
  60. conf$schema <- "minu_teed"
  61. conf$table <- "teedevork_riigiteed"
  62. input <- ruut::construct_to_gpkg_output_postgres_str(
  63. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  64. checkPrimaryKeyUnicity = TRUE, key = "id_0", geometry_field = "geom"
  65. )
  66. output <- vars$tmp_gpkg_file_output_2
  67. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '30' }", input, output)
  68. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  69. system(cmd)
  70. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  71. ## 4.3 Enne polügoniseerimist ühendame eesti kontuuri ja teedevõrgu üheks.
  72. # ruut::qgis_algorithm_search_by_word("merge")
  73. algorithm <- "native:mergevectorlayers"
  74. # ruut::qgis_show_help(algorithm = algorithm)
  75. vars$layer_1 <- vars$tmp_gpkg_file_input_1
  76. vars$layer_2 <- vars$tmp_gpkg_file_input_2
  77. output <- vars$tmp_gpkg_file_output_3
  78. cmd <- sprintf(
  79. "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ",
  80. vars$layer_1, vars$layer_2, output
  81. )
  82. system(cmd)
  83. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  84. ## 4.4 Polügoniseerime saadud riigiteedevõrgu. Tekib teedega piiratud polügoonid.
  85. # ruut::qgis_algorithm_search_by_word("Polygonize")
  86. algorithm <- "native:polygonize"
  87. # ruut::qgis_show_help(algorithm = algorithm)
  88. input <- vars$tmp_gpkg_file_input_3
  89. output <- vars$tmp_gpkg_file_output_4
  90. str <- sprintf("{ 'INPUT' : '%s', 'KEEP_FIELDS' : False, 'OUTPUT' : '%s' }", input, output)
  91. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  92. system(cmd)
  93. ## 4.5 Copy temp gpkg file to postgis.
  94. conf$schema <- "minu_teed"
  95. conf$table <- "teedevork_riigiteed_a"
  96. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  97. 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, "MULTIPOLYGON", conf$schema, conf$table)
  98. system(cmd)
  99. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  100. # ## =================== !!!!!!!!!!!!!! POOLELI !!!!!!!!!!!! =======================
  101. #
  102. # ## 4.2 Lõikame Eesti kaardist polügoniseerime riigiteedevõrgu välja.
  103. # ## Tekib ümber Eesti raam, mis tuleb jagada kuidagi transporditsoonideks.
  104. # # ruut::qgis_algorithm_search_by_word("Difference")
  105. # algorithm <- "native:difference"
  106. # # ruut::qgis_show_help(algorithm = algorithm)
  107. # conf$schema <- "minu_teed"
  108. # conf$table <- "eesti"
  109. # input <- ruut::construct_to_gpkg_output_postgres_str(
  110. # conf = conf, geometry_type = "Polygon", srid = 3301,
  111. # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  112. # )
  113. # conf$schema <- "minu_teed"
  114. # conf$table <- "riigiteedevork_a"
  115. # overlay <- ruut::construct_to_gpkg_output_postgres_str(
  116. # conf = conf, geometry_type = "Polygon", srid = 3301,
  117. # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  118. # )
  119. # conf$schema <- "minu_teed"
  120. # conf$table <- "riigiteedevork_xxx"
  121. # output <- ruut::construct_to_gpkg_output_postgres_str(
  122. # conf = conf, geometry_field = "geom", key = "fid"
  123. # )
  124. # str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', 'OVERLAY' : '%s' }", input, output, overlay)
  125. # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  126. # system(cmd)
  127. #
  128. #
  129. # ## 4.3 Kuna polügoniseeritud pinnad ei olnud täiuslikud, siis teostame
  130. # ## polügoniseeriist veelkorra. Selleks lõikame esmalt teedevõrgust
  131. # ## tekkinud raamiga need teed mis jäävad raamis sisse.
  132. # # ruut::qgis_algorithm_search_by_word("Intersection")
  133. # algorithm <- "native:intersection"
  134. # # ruut::qgis_show_help(algorithm = algorithm)
  135. # conf$schema <- "minu_teed"
  136. # conf$table <- "riigiteedevork"
  137. # input <- ruut::construct_to_gpkg_output_postgres_str(
  138. # conf = conf, geometry_type = "MultiLineString", srid = 3301,
  139. # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  140. # )
  141. # conf$schema <- "minu_teed"
  142. # conf$table <- "riigiteedevork_xxx"
  143. # overlay <- ruut::construct_to_gpkg_output_postgres_str(
  144. # conf = conf, geometry_type = "MultiPolygon", srid = 3301,
  145. # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  146. # )
  147. # conf$schema <- "minu_teed"
  148. # conf$table <- "riigiteedevork_xxx1"
  149. # output <- ruut::construct_to_gpkg_output_postgres_str(
  150. # conf = conf, geometry_field = "geom"
  151. # )
  152. # str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : [], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }", input, output, overlay)
  153. # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  154. # system(cmd)
  155. }