pk_lisame_jooned.R 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #' Piirkonnale andmebaasides olevate joonte lisamine
  2. #'
  3. #' Etteantud piirkonna geomeetrilise piirjoone ('piir') ja selle joone piirikasti ('bb') järele leitakse nende aladega kaetud polügoonid. Andmed salvestatakse postgis andmebaasi.
  4. #'
  5. #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
  6. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  7. #' @return Uute andmebaasi kihtide 'piir_...' ja 'bb_...' loomine.
  8. #' @seealso [sf::st_read()], [sf::write_sf()],[sf::st_transform()],[ruut::pk_sellest_alustame_db_loomist()],[ruut::pk_lisame_ruudustikud()] ,[ruut::pk_lisame_polygoonid()],[ruut::pk_lisame_jooned()],[ruut::pk_lisame_punktid()],[ruut::pk_teisendame_polygoone()],[ruut::pk_teisendame_jooni()],[ruut::pk_teisendame_punkte()]
  9. #' @keywords postgis, boundary box, EPSG:3301
  10. #' @export
  11. #' @examples
  12. #' \dontrun{
  13. #'
  14. #' obj <- "marja"
  15. #' pk_lisame_jooned(obj = obj, conf = NULL)
  16. #'
  17. #' # Layers list.
  18. #' ruut::db_schema_tablenames(conf = conf)
  19. #' }
  20. pk_lisame_jooned <- function(obj = NULL, conf = NULL) {
  21. ## ------------- muutujad ja teisendused ---------------
  22. vars <- ajutised_muutujad(pk = NULL, obj, conf)
  23. obj <- vars$obj
  24. piir <- vars$pk
  25. conf <- vars$conf
  26. ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
  27. if (!any("a00_piir" %in% ruut::db_schema_tablenames(conf = conf))) {
  28. cat(
  29. "\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n"
  30. )
  31. return(NULL)
  32. }
  33. ## Algorithm
  34. # ruut::qgis_algorithm_search_by_word(str = "extract")
  35. # algorithm <- "native:extractbylocation"
  36. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  37. ## -------------------- Loop -----------------------
  38. intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
  39. andmed <-
  40. data.frame("schema" = character(0), "table" = character(0))
  41. andmed <-
  42. rbind(
  43. andmed,
  44. data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud")
  45. )
  46. andmed <-
  47. rbind(andmed,
  48. data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
  49. andmed <-
  50. rbind(andmed,
  51. data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
  52. andmed <-
  53. rbind(andmed,
  54. data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
  55. andmed <-
  56. rbind(andmed,
  57. data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
  58. andmed <-
  59. rbind(andmed,
  60. data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
  61. andmed <-
  62. rbind(andmed,
  63. data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
  64. andmed <-
  65. rbind(andmed,
  66. data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
  67. andmed <-
  68. rbind(andmed,
  69. data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
  70. andmed <-
  71. rbind(andmed,
  72. data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
  73. andmed <-
  74. rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
  75. andmed <-
  76. rbind(andmed,
  77. data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_j"))
  78. andmed <-
  79. rbind(andmed,
  80. data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_j"))
  81. andmed <-
  82. rbind(andmed,
  83. data.frame("schema" = "eesti", "table" = "e_405_piire_j"))
  84. andmed <-
  85. rbind(andmed,
  86. data.frame("schema" = "eesti", "table" = "e_501_tee_j"))
  87. andmed <-
  88. rbind(andmed,
  89. data.frame("schema" = "eesti", "table" = "e_502_roobastee_j"))
  90. andmed <-
  91. rbind(
  92. andmed,
  93. data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_j")
  94. )
  95. andmed <-
  96. rbind(andmed,
  97. data.frame("schema" = "eesti", "table" = "e_601_elektriliin_j"))
  98. andmed <-
  99. rbind(andmed, data.frame("schema" = "eesti", "table" = "korgus_j"))
  100. andmed <-
  101. rbind(andmed, data.frame("schema" = "eesti", "table" = "sild_j"))
  102. andmed <-
  103. rbind(andmed, data.frame("schema" = "eesti", "table" = "sygavus_j"))
  104. andmed <-
  105. rbind(andmed, data.frame("schema" = "minu_teed", "table" = "links"))
  106. andmed <-
  107. rbind(andmed,
  108. data.frame("schema" = "minu_teed", "table" = "teedevork"))
  109. andmed <-
  110. rbind(andmed,
  111. data.frame("schema" = "minu_teed", "table" = "gtfs_lines"))
  112. ## Tabelid mis on juba andmebaasis ja mida ümber ei kirjutata.
  113. olemasolevad_tabelid <- ruut::db_schema_tablenames(conf = conf)
  114. for (intersect in intersect_layers) {
  115. conf$table <- intersect
  116. conf$schema <- obj
  117. intersect_layer <-
  118. ruut::construct_to_gpkg_output_postgres_str(
  119. conf = conf,
  120. geometry_type = "Polygon",
  121. srid = 3301,
  122. checkPrimaryKeyUnicity = TRUE,
  123. key = "id"
  124. )
  125. for (i in 1:nrow(andmed)) {
  126. cat("\n\n\n==========================================\n\n")
  127. cat(sprintf("Tabel: %s.%s", andmed[i, "schema"], andmed[i, "table"]))
  128. cat("\n==========================================\n\n")
  129. ## Kui olemasolev tabel eksisteerib andmebaasis, siis jätame arvutused
  130. ## selle tabeliga vahele.
  131. uus_tabel <- sprintf("data_l_%s_%s",
  132. strtrim(as.character(andmed$schema[i]), 2),
  133. andmed$table[i])
  134. if (uus_tabel %in% olemasolevad_tabelid) {
  135. cat("\nTabel on juba andmebaasis olemas.\n")
  136. next
  137. }
  138. ## Osadel tabelitel andmebaasisvõib olla geomeetria välja tähistus 'geometry'.
  139. geom <- "geom"
  140. # if (andmed$schema[i] %in% c("gtfs")) geom <- "geometry"
  141. conf$table <- andmed$table[i]
  142. conf$schema <- andmed$schema[i]
  143. input <- ruut::construct_to_gpkg_output_postgres_str(
  144. conf = conf,
  145. geometry_type = "Linestring",
  146. srid = 3301,
  147. checkPrimaryKeyUnicity = TRUE,
  148. key = "id",
  149. geometry_field = geom
  150. )
  151. ## Andmebaasi tabeli nimi: 2 esimest tähte tuleb schema nimest millest on andmed võetud, siis '_l_' s.o 'line' mis tähistab jooni.
  152. conf$table <-
  153. sprintf("data_l_%s_%s",
  154. strtrim(as.character(andmed$schema[i]), 2),
  155. andmed$table[i])
  156. conf$schema <- obj
  157. output <-
  158. ruut::construct_to_gpkg_output_postgres_str(
  159. conf = conf,
  160. geometry_field = "geom",
  161. geometry_type = NULL,
  162. srid = 3301,
  163. checkPrimaryKeyUnicity = FALSE,
  164. key = "id"
  165. )
  166. str <-
  167. paste0(
  168. "{ 'INPUT' : '",
  169. input,
  170. "', 'INPUT_FIELDS' : [], 'OUTPUT' : '",
  171. output,
  172. "', 'OVERLAY' : '",
  173. intersect_layer,
  174. "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }"
  175. )
  176. algorithm <- "native:intersection"
  177. cmd <-
  178. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  179. cat(sprintf("\n%s\n\n", cmd))
  180. system(cmd)
  181. }
  182. }
  183. ## Layers list
  184. conf$schema <- obj
  185. ruut::db_schema_tablenames(conf = conf)
  186. sf::st_layers(dsn = vars$tmp_gpkg_file)
  187. }