pk_lisame_punktid.R 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #' Piirkonnale andmebaasides olevate punktide 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_punktid(obj = obj, conf = NULL)
  16. #'
  17. #' # Layers list.
  18. #' ruut::db_schema_tablenames(conf = conf)
  19. #' }
  20. pk_lisame_punktid <- 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(andmed,
  43. data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
  44. andmed <-
  45. rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw"))
  46. andmed <-
  47. rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois"))
  48. andmed <-
  49. rbind(andmed,
  50. data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
  51. andmed <-
  52. rbind(andmed,
  53. data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
  54. andmed <-
  55. rbind(andmed,
  56. data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
  57. andmed <-
  58. rbind(andmed,
  59. data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
  60. andmed <-
  61. rbind(andmed,
  62. data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
  63. andmed <-
  64. rbind(andmed,
  65. data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
  66. andmed <-
  67. rbind(andmed,
  68. data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
  69. andmed <-
  70. rbind(andmed,
  71. data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
  72. andmed <-
  73. rbind(andmed,
  74. data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
  75. andmed <-
  76. rbind(andmed,
  77. data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
  78. andmed <-
  79. rbind(andmed,
  80. data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
  81. andmed <-
  82. rbind(andmed,
  83. data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
  84. andmed <-
  85. rbind(andmed,
  86. data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_p"))
  87. andmed <-
  88. rbind(andmed,
  89. data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_p"))
  90. andmed <-
  91. rbind(andmed,
  92. data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_p"))
  93. andmed <-
  94. rbind(andmed,
  95. data.frame("schema" = "eesti", "table" = "e_402_korgrajatis_p"))
  96. andmed <-
  97. rbind(andmed,
  98. data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_p"))
  99. andmed <-
  100. rbind(andmed,
  101. data.frame("schema" = "eesti", "table" = "e_602_tehnopaigaldis_p"))
  102. andmed <-
  103. rbind(andmed, data.frame("schema" = "eesti", "table" = "kirik_p"))
  104. andmed <-
  105. rbind(andmed, data.frame("schema" = "eesti", "table" = "kohanimi"))
  106. andmed <-
  107. rbind(andmed, data.frame("schema" = "minu_teed", "table" = "nodes"))
  108. andmed <-
  109. rbind(andmed,
  110. data.frame("schema" = "minu_teed", "table" = "gtfs_stops"))
  111. ## Tabelid mis on juba andmebaasis ja mida ümber ei kirjutata.
  112. olemasolevad_tabelid <- ruut::db_schema_tablenames(conf = conf)
  113. for (intersect in intersect_layers) {
  114. conf$table <- intersect
  115. conf$schema <- obj
  116. intersect_layer <-
  117. ruut::construct_to_gpkg_output_postgres_str(
  118. conf = conf,
  119. geometry_type = "Polygon",
  120. srid = 3301,
  121. checkPrimaryKeyUnicity = TRUE,
  122. key = "id"
  123. )
  124. for (i in 1:nrow(andmed)) {
  125. cat("\n\n\n==========================================\n\n")
  126. cat(sprintf("Tabel: %s.%s", andmed[i, "schema"], andmed[i, "table"]))
  127. cat("\n==========================================\n\n")
  128. ## Kui olemasolev tabel eksisteerib andmebaasis, siis jätame arvutused
  129. ## selle tabeliga vahele.
  130. uus_tabel <- sprintf("data_p_%s_%s",
  131. strtrim(as.character(andmed$schema[i]), 2),
  132. andmed$table[i])
  133. if (uus_tabel %in% olemasolevad_tabelid) {
  134. cat("\nTabel on juba andmebaasis olemas.\n")
  135. next
  136. }
  137. ## Osadel tabelitel andmebaasisvõib olla geomeetria välja tähistus 'geometry'.
  138. geom <- "geom"
  139. # if (andmed$schema[i] %in% c("gtfs")) geom <- "geometry"
  140. conf$table <- andmed$table[i]
  141. conf$schema <- andmed$schema[i]
  142. input <- ruut::construct_to_gpkg_output_postgres_str(
  143. conf = conf,
  144. geometry_type = "Point",
  145. srid = 3301,
  146. checkPrimaryKeyUnicity = TRUE,
  147. key = "id",
  148. geometry_field = geom
  149. )
  150. conf$table <-
  151. sprintf("data_p_%s_%s",
  152. strtrim(as.character(andmed$schema[i]), 2),
  153. andmed$table[i])
  154. conf$schema <- obj
  155. output <-
  156. ruut::construct_to_gpkg_output_postgres_str(
  157. conf = conf,
  158. geometry_field = "geom",
  159. geometry_type = NULL,
  160. srid = 3301,
  161. checkPrimaryKeyUnicity = FALSE,
  162. key = "id"
  163. )
  164. ## ------------------- QGIS: qgisprocess ----------------------
  165. str <-
  166. paste0(
  167. "{ 'INPUT' : '",
  168. input,
  169. "', 'INTERSECT' : '",
  170. intersect_layer,
  171. "', 'OUTPUT' : '",
  172. output,
  173. "', 'PREDICATE' : [0] }"
  174. )
  175. algorithm <- "native:extractbylocation"
  176. cmd <-
  177. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  178. cat(sprintf("\n%s\n\n", cmd))
  179. system(cmd)
  180. ## Filtreerime aadressandmete kihi eraldi alamkihtideks
  181. if (andmed$table[i] == "aadressandmed") {
  182. parent_table <-
  183. sprintf("data_p_%s_%s",
  184. strtrim(as.character(andmed$schema[i]), 2),
  185. andmed$table[i])
  186. conn <- ruut::db_connect(conf = conf)
  187. aadressandmed <-
  188. unique(as.data.frame(sf::st_read(
  189. dsn = conn,
  190. layer = c(conf$schema, parent_table),
  191. as_tibble = T
  192. ))[, c("adob_liik"), drop = FALSE])
  193. DBI::dbDisconnect(conn)
  194. for (k in 1:nrow(aadressandmed)) {
  195. table_suffix <- aadressandmed$adob_liik[k]
  196. conf$table <- parent_table
  197. conf$schema <- obj
  198. input <-
  199. ruut::construct_to_gpkg_output_postgres_str(
  200. conf = conf,
  201. geometry_type = "Point",
  202. srid = 3301,
  203. checkPrimaryKeyUnicity = TRUE,
  204. key = "id"
  205. )
  206. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  207. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  208. output <-
  209. ruut::construct_to_gpkg_output_postgres_str(
  210. conf = conf,
  211. geometry_field = "geom",
  212. geometry_type = "Polygon",
  213. srid = 3301,
  214. checkPrimaryKeyUnicity = FALSE,
  215. key = "id"
  216. )
  217. str <-
  218. paste0(
  219. "{ 'INPUT' : '",
  220. input,
  221. "', 'INTERSECT' : '",
  222. intersect_layer,
  223. "', 'OUTPUT' : '",
  224. output,
  225. "', FIELD : 'adob_liik', OPERATOR : 0, VALUE : ",
  226. aadressandmed$adob_liik[k],
  227. " }"
  228. )
  229. algorithm <- "native:extractbyattribute"
  230. cmd <-
  231. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  232. cat(sprintf("\n%s\n", cmd))
  233. system(cmd)
  234. }
  235. }
  236. }
  237. }
  238. ## Layers list
  239. conf$schema <- obj
  240. ruut::db_schema_tablenames(conf = conf)
  241. sf::st_layers(dsn = vars$tmp_gpkg_file)
  242. }