pk_lisame_jooned.R 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. for (intersect in intersect_layers) {
  105. conf$table <- intersect
  106. conf$schema <- obj
  107. intersect_layer <-
  108. ruut::construct_to_gpkg_output_postgres_str(
  109. conf = conf,
  110. geometry_type = "Polygon",
  111. srid = 3301,
  112. checkPrimaryKeyUnicity = TRUE,
  113. key = "id"
  114. )
  115. for (i in 1:nrow(andmed)) {
  116. cat("\n\n\n==========================================\n\n")
  117. cat(sprintf("Tabel: %s.%s", andmed[i, "schema"], andmed[i, "table"]))
  118. cat("\n==========================================\n\n")
  119. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  120. geom <- "geom"
  121. # if (andmed$schema[i] %in% c("gtfs")) geom <- "geometry"
  122. conf$table <- andmed$table[i]
  123. conf$schema <- andmed$schema[i]
  124. input <- ruut::construct_to_gpkg_output_postgres_str(
  125. conf = conf,
  126. geometry_type = "Linestring",
  127. srid = 3301,
  128. checkPrimaryKeyUnicity = TRUE,
  129. key = "id",
  130. geometry_field = geom
  131. )
  132. ## Andmebaasi tabeli nimi: 2 esimest tähte tuleb schema nimest millest on andmed võetud, siis '_l_' s.o 'line' mis tähistab jooni.
  133. conf$table <-
  134. sprintf("data_l_%s_%s",
  135. strtrim(as.character(andmed$schema[i]), 2),
  136. andmed$table[i])
  137. conf$schema <- obj
  138. output <-
  139. ruut::construct_to_gpkg_output_postgres_str(
  140. conf = conf,
  141. geometry_field = "geom",
  142. geometry_type = NULL,
  143. srid = 3301,
  144. checkPrimaryKeyUnicity = FALSE,
  145. key = "id"
  146. )
  147. str <-
  148. paste0(
  149. "{ 'INPUT' : '",
  150. input,
  151. "', 'INPUT_FIELDS' : [], 'OUTPUT' : '",
  152. output,
  153. "', 'OVERLAY' : '",
  154. intersect_layer,
  155. "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }"
  156. )
  157. algorithm <- "native:intersection"
  158. cmd <-
  159. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  160. cat(sprintf("\n%s\n\n", cmd))
  161. system(cmd)
  162. }
  163. }
  164. ## Layers list
  165. conf$schema <- obj
  166. ruut::db_schema_tablenames(conf = conf)
  167. sf::st_layers(dsn = vars$tmp_gpkg_file)
  168. }