minu_teed_loomine.R 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #' Teeregistri WFS ja maaameti andmete põhjal 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, maps, WFS, OSM
  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. if (is.null(conf)) {
  23. conf <- ruut::get_config()
  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 <- layer_6 <- "test_layer_6"
  34. tmp_gpkg_file_output_6 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  35. tmp_gpkg_file_input_6 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  36. conf$gpkg_table <- layer_5 <- "test_layer_5"
  37. tmp_gpkg_file_output_5 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  38. tmp_gpkg_file_input_5 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  39. conf$gpkg_table <- layer_4 <- "test_layer_4"
  40. tmp_gpkg_file_output_4 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  41. tmp_gpkg_file_input_4 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  42. conf$gpkg_table <- layer_3 <- "test_layer_3"
  43. tmp_gpkg_file_output_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  44. tmp_gpkg_file_input_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  45. conf$gpkg_table <- layer_2 <- "test_layer_2"
  46. tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  47. tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  48. conf$gpkg_table <- layer_1 <- "test_layer_1"
  49. tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  50. tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  51. ## ================= Schema 'minu_teed' ====================
  52. conf$schema <- "minu_teed"
  53. ruut::db_create_new_schema(conf = conf)
  54. ## =================== 2. teedevõrkude loomine =======================
  55. ## ------------------ 2.1.1 Riigiteed osadena -----------------------
  56. ## s.o (ilma rampideta) <= 30, koos rampide ja muude riigiteedega <= 45.
  57. # ruut::qgis_algorithm_search_by_word("extract")
  58. algorithm <- "native:extractbyattribute"
  59. # ruut::qgis_show_help(algorithm = algorithm)
  60. conf$schema <- "eesti"
  61. conf$table <- "e_501_tee_j"
  62. input <- ruut::construct_to_gpkg_output_postgres_str(
  63. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  64. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  65. )
  66. output <- tmp_gpkg_file_output_1
  67. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '45' }", 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. ## 2.1.2 Copy temp gpkg file to postgis.
  72. conf$schema <- "minu_teed"
  73. conf$table <- "teedevork_riigiteed_osadena"
  74. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  75. 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)
  76. system(cmd)
  77. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  78. ## ---------------- 2.2.1 Riigiteed tervikuna ------------------
  79. # ruut::qgis_algorithm_search_by_word("dissolve")
  80. algorithm <- "native:dissolve"
  81. # ruut::qgis_show_help(algorithm = algorithm)
  82. input <- tmp_gpkg_file_input_1
  83. output <- tmp_gpkg_file_output_2
  84. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  85. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  86. system(cmd)
  87. ## 2.2.2 Eemaldame ebavajalikud veerud
  88. # ruut::qgis_algorithm_search_by_word("retain")
  89. algorithm <- "native:retainfields"
  90. # ruut::qgis_show_help(algorithm = algorithm)
  91. input <- tmp_gpkg_file_input_2
  92. output <- tmp_gpkg_file_output_3
  93. cmd <- sprintf("qgis_process run %s --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  94. system(cmd)
  95. ## 2.2.2 Copy temp gpkg file to postgis.
  96. conf$schema <- "minu_teed"
  97. conf$table <- "teedevork_riigiteed"
  98. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  99. 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)
  100. system(cmd)
  101. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  102. ## 2.2.3 Riigiteede alg- ja lõpppunkt ---------------
  103. # ruut::qgis_algorithm_search_by_word("vertices")
  104. algorithm <- "native:extractspecificvertices"
  105. # ruut::qgis_show_help(algorithm = algorithm)
  106. input <- tmp_gpkg_file_input_3
  107. output <- tmp_gpkg_file_output_4
  108. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
  109. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  110. system(cmd)
  111. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  112. ## 2.2.4 Copy temp gpkg file to postgis.
  113. conf$schema <- "minu_teed"
  114. conf$table <- "teedevork_riigiteed_p"
  115. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  116. 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)
  117. system(cmd)
  118. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  119. ## ------------------- 2.4 Muud teed osadena ----------------------
  120. ## s.o tyyp IN (50,60,).
  121. ## 2.4.1 Esmalt sorteerime >= 50
  122. # ruut::qgis_algorithm_search_by_word("extract")
  123. algorithm <- "native:extractbyattribute"
  124. # ruut::qgis_show_help(algorithm = algorithm)
  125. conf$schema <- "eesti"
  126. conf$table <- "e_501_tee_j"
  127. input <- ruut::construct_to_gpkg_output_postgres_str(
  128. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  129. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  130. )
  131. output <- tmp_gpkg_file_output_1
  132. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 3, 'OUTPUT' : '%s', 'VALUE' : '50' }", input, output)
  133. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  134. system(cmd)
  135. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  136. ## 2.4.2 Seejärel sorteerime <= 60
  137. algorithm <- "native:extractbyattribute"
  138. input <- tmp_gpkg_file_input_1
  139. output <- tmp_gpkg_file_output_2
  140. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output)
  141. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  142. system(cmd)
  143. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  144. ## 2.4.3 Eemaldame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
  145. algorithm <- "native:extractbyattribute"
  146. input <- tmp_gpkg_file_input_2
  147. output <- tmp_gpkg_file_output_3
  148. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 9, 'OUTPUT' : '%s' }", input, output)
  149. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  150. system(cmd)
  151. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  152. ## 2.4.4 Copy temp gpkg file to postgis.
  153. conf$schema <- "minu_teed"
  154. conf$table <- "teedevork_kt_osadena"
  155. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  156. 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)
  157. system(cmd)
  158. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  159. ## 2.4.5 Ühendame sama teenumbriga muud teed
  160. # ruut::qgis_algorithm_search_by_word("dissolve")
  161. algorithm <- "native:dissolve"
  162. # ruut::qgis_show_help(algorithm = algorithm)
  163. input <- tmp_gpkg_file_input_3
  164. output <- tmp_gpkg_file_output_4
  165. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  166. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  167. system(cmd)
  168. ## 2.4.6 Eemaldame ebavajalikud veerud
  169. # ruut::qgis_algorithm_search_by_word("retain")
  170. algorithm <- "native:retainfields"
  171. # ruut::qgis_show_help(algorithm = algorithm)
  172. input <- tmp_gpkg_file_input_4
  173. output <- tmp_gpkg_file_output_5
  174. cmd <- sprintf("qgis_process run %s --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  175. system(cmd)
  176. ## 2.4.7 Copy temp gpkg file to postgis.
  177. conf$schema <- "minu_teed"
  178. conf$table <- "teedevork_kt"
  179. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  180. 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)
  181. system(cmd)
  182. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  183. ## 2.4.8 Muude teede alg- ja lõpppunkt
  184. # ruut::qgis_algorithm_search_by_word("vertices")
  185. algorithm <- "native:extractspecificvertices"
  186. # ruut::qgis_show_help(algorithm = algorithm)
  187. input <- tmp_gpkg_file_input_5
  188. output <- tmp_gpkg_file_output_6
  189. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
  190. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  191. system(cmd)
  192. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  193. ## 2.4.9 Copy temp gpkg file to postgis.
  194. conf$schema <- "minu_teed"
  195. conf$table <- "teedevork_kt_p"
  196. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  197. 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)
  198. system(cmd)
  199. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  200. ## 2.5.1 Leiame need muud teed kus 'tee'=NULL (ilma teenumbrita ehk kvartalissisesed).
  201. algorithm <- "native:extractbyattribute"
  202. input <- tmp_gpkg_file_input_2
  203. output <- tmp_gpkg_file_output_3
  204. str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 8, 'OUTPUT' : '%s' }", input, output)
  205. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  206. system(cmd)
  207. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  208. ## 2.5.2 Eemaldame ebavajalikud veerud
  209. # ruut::qgis_algorithm_search_by_word("retain")
  210. algorithm <- "native:retainfields"
  211. # ruut::qgis_show_help(algorithm = algorithm)
  212. input <- tmp_gpkg_file_input_3
  213. output <- tmp_gpkg_file_output_4
  214. cmd <- sprintf("qgis_process run %s --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  215. system(cmd)
  216. ## 2.5.3 Copy temp gpkg file to postgis.
  217. conf$schema <- "minu_teed"
  218. conf$table <- "teedevork_kt_numbrita"
  219. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  220. 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)
  221. system(cmd)
  222. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  223. ## 2.5.3 Nimeta teede alg- ja lõpppunkt
  224. # ruut::qgis_algorithm_search_by_word("vertices")
  225. algorithm <- "native:extractspecificvertices"
  226. # ruut::qgis_show_help(algorithm = algorithm)
  227. input <- tmp_gpkg_file_input_4
  228. output <- tmp_gpkg_file_output_5
  229. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', , 'VERTICES' : \"0,-1\" }", input, output)
  230. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  231. system(cmd)
  232. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  233. ## 2.4.9 Copy temp gpkg file to postgis.
  234. conf$schema <- "minu_teed"
  235. conf$table <- "teedevork_kt_numbrita_p"
  236. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  237. 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)
  238. system(cmd)
  239. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  240. ## =================== 3. 'eesti' =======================
  241. ## 3.1 Maakondadest Eesti maismaapiir
  242. # ruut::qgis_algorithm_search_by_word("dissolve")
  243. algorithm <- "native:dissolve"
  244. # ruut::qgis_show_help(algorithm = algorithm)
  245. conf$schema <- "maaamet"
  246. conf$table <- "maakond"
  247. input <- ruut::construct_to_gpkg_output_postgres_str(
  248. conf = conf, geometry_type = "MultiPolygon", srid = 3301,
  249. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  250. )
  251. output <- tmp_gpkg_file_output_1
  252. str <- sprintf("{ 'FIELD' : [], 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  253. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  254. system(cmd)
  255. ## 3.2 Kustutame ebaolulised väljad
  256. # ruut::qgis_algorithm_search_by_word("delete")
  257. algorithm <- "native:deletecolumn"
  258. # ruut::qgis_show_help(algorithm = algorithm)
  259. input <- tmp_gpkg_file_input_1
  260. output <- tmp_gpkg_file_output_2
  261. cmd <- sprintf("qgis_process run %s --COLUMN='mnimi' --COLUMN='mkood' --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  262. system(cmd)
  263. ## 3.3 Jaotame multipart polügooni singlepart polügoonideks.
  264. # ruut::qgis_algorithm_search_by_word("multipart")
  265. algorithm <- "native:multiparttosingleparts"
  266. # ruut::qgis_show_help(algorithm = algorithm)
  267. input <- tmp_gpkg_file_input_2
  268. output <- tmp_gpkg_file_output_3
  269. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  270. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  271. system(cmd)
  272. ## 3.4 Lisame pindade pindalad ja perimeetri
  273. # ruut::qgis_algorithm_search_by_word("columns")
  274. algorithm <- "qgis:exportaddgeometrycolumns"
  275. # ruut::qgis_show_help(algorithm = algorithm)
  276. input <- tmp_gpkg_file_input_3
  277. output <- tmp_gpkg_file_output_4
  278. str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  279. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  280. system(cmd)
  281. ## 3.5 Eemaldame objektid mille pindala on väiksem kui 10km2 (väikesaared)
  282. # ruut::qgis_algorithm_search_by_word("extract")
  283. algorithm <- "native:extractbyattribute"
  284. # ruut::qgis_show_help(algorithm = algorithm)
  285. input <- tmp_gpkg_file_input_4
  286. output <- tmp_gpkg_file_output_5
  287. str <- sprintf("{ 'FIELD' : 'area', 'OPERATOR' : 2, 'VALUE' : '10000000', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  288. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  289. system(cmd)
  290. ## 3.6 Eemaldame augud, mis on suuremad kui 1ha.
  291. # ruut::qgis_algorithm_search_by_word("holes")
  292. algorithm <- "native:deleteholes"
  293. # ruut::qgis_show_help(algorithm = algorithm)
  294. input <- tmp_gpkg_file_input_5
  295. output <- tmp_gpkg_file_output_6
  296. str <- sprintf("{ 'MIN_AREA' : '10000', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  297. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  298. system(cmd)
  299. ## 3.7 Copy temp gpkg file to postgis.
  300. conf$schema <- "minu_teed"
  301. conf$table <- "eesti"
  302. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  303. 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)
  304. system(cmd)
  305. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  306. ## ------------------ Teedevõrgu polügoniseerimine ------------------
  307. ## 4.1 Teisendame eesti polügooni joonteks.
  308. # ruut::qgis_algorithm_search_by_word("lines")
  309. algorithm <- "native:polygonstolines"
  310. # ruut::qgis_show_help(algorithm = algorithm)
  311. conf$schema <- "minu_teed"
  312. conf$table <- "eesti"
  313. input <- ruut::construct_to_gpkg_output_postgres_str(
  314. conf = conf, geometry_type = "Polygon", srid = 3301,
  315. checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  316. )
  317. output <- tmp_gpkg_file_output_1
  318. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  319. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  320. system(cmd)
  321. ## 4.2 Eemaldame riigiteedest rambid.
  322. algorithm <- "native:extractbyattribute"
  323. # ruut::qgis_show_help(algorithm = algorithm)
  324. conf$schema <- "minu_teed"
  325. conf$table <- "teedevork_riigiteed"
  326. input <- ruut::construct_to_gpkg_output_postgres_str(
  327. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  328. checkPrimaryKeyUnicity = TRUE, key = "id_0", geometry_field = "geom"
  329. )
  330. output <- tmp_gpkg_file_output_2
  331. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '30' }", input, output)
  332. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  333. system(cmd)
  334. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  335. ## 4.3 Enne polügoniseerimist ühendame eesti kontuuri ja teedevõrgu üheks.
  336. # ruut::qgis_algorithm_search_by_word("merge")
  337. algorithm <- "native:mergevectorlayers"
  338. # ruut::qgis_show_help(algorithm = algorithm)
  339. layer_1 <- tmp_gpkg_file_input_1
  340. layer_2 <- tmp_gpkg_file_input_2
  341. output <- tmp_gpkg_file_output_3
  342. cmd <- sprintf(
  343. "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ",
  344. layer_1, layer_2, output
  345. )
  346. system(cmd)
  347. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  348. ## 4.4 Polügoniseerime saadud riigiteedevõrgu. Tekib teedega piiratud polügoonid.
  349. # ruut::qgis_algorithm_search_by_word("Polygonize")
  350. algorithm <- "native:polygonize"
  351. # ruut::qgis_show_help(algorithm = algorithm)
  352. input <- tmp_gpkg_file_input_3
  353. output <- tmp_gpkg_file_output_4
  354. str <- sprintf("{ 'INPUT' : '%s', 'KEEP_FIELDS' : False, 'OUTPUT' : '%s' }", input, output)
  355. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  356. system(cmd)
  357. ## 4.5 Copy temp gpkg file to postgis.
  358. conf$schema <- "minu_teed"
  359. conf$table <- "teedevork_riigiteed_a"
  360. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  361. 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)
  362. system(cmd)
  363. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  364. ## =================== !!!!!!!!!!!!!! POOLELI !!!!!!!!!!!! =======================
  365. ## 4.2 Lõikame Eesti kaardist polügoniseerime riigiteedevõrgu välja.
  366. ## Tekib ümber Eesti raam, mis tuleb jagada kuidagi transporditsoonideks.
  367. # ruut::qgis_algorithm_search_by_word("Difference")
  368. algorithm <- "native:difference"
  369. # ruut::qgis_show_help(algorithm = algorithm)
  370. conf$schema <- "minu_teed"
  371. conf$table <- "eesti"
  372. input <- ruut::construct_to_gpkg_output_postgres_str(
  373. conf = conf, geometry_type = "Polygon", srid = 3301,
  374. checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  375. )
  376. conf$schema <- "minu_teed"
  377. conf$table <- "riigiteedevork_a"
  378. overlay <- ruut::construct_to_gpkg_output_postgres_str(
  379. conf = conf, geometry_type = "Polygon", srid = 3301,
  380. checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  381. )
  382. conf$schema <- "minu_teed"
  383. conf$table <- "riigiteedevork_xxx"
  384. output <- ruut::construct_to_gpkg_output_postgres_str(
  385. conf = conf, geometry_field = "geom", key = "fid"
  386. )
  387. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', 'OVERLAY' : '%s' }", input, output, overlay)
  388. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  389. system(cmd)
  390. "Processing algorithm…
  391. Algorithm 'Intersection' starting…
  392. Input parameters:
  393. { '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' : '' }"
  394. ## 4.3 Kuna polügoniseeritud pinnad ei olnud täiuslikud, siis teostame
  395. ## polügoniseeriist veelkorra. Selleks lõikame esmalt teedevõrgust
  396. ## tekkinud raamiga need teed mis jäävad raamis sisse.
  397. # ruut::qgis_algorithm_search_by_word("Intersection")
  398. algorithm <- "native:intersection"
  399. # ruut::qgis_show_help(algorithm = algorithm)
  400. conf$schema <- "minu_teed"
  401. conf$table <- "riigiteedevork"
  402. input <- ruut::construct_to_gpkg_output_postgres_str(
  403. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  404. checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  405. )
  406. conf$schema <- "minu_teed"
  407. conf$table <- "riigiteedevork_xxx"
  408. overlay <- ruut::construct_to_gpkg_output_postgres_str(
  409. conf = conf, geometry_type = "MultiPolygon", srid = 3301,
  410. checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
  411. )
  412. conf$schema <- "minu_teed"
  413. conf$table <- "riigiteedevork_xxx1"
  414. output <- ruut::construct_to_gpkg_output_postgres_str(
  415. conf = conf, geometry_field = "geom"
  416. )
  417. str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : [], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }", input, output, overlay)
  418. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  419. system(cmd)
  420. }