pk_lisame_punktid.R 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. for (intersect in intersect_layers) {
  107. conf$table <- intersect
  108. conf$schema <- obj
  109. intersect_layer <-
  110. ruut::construct_to_gpkg_output_postgres_str(
  111. conf = conf,
  112. geometry_type = "Polygon",
  113. srid = 3301,
  114. checkPrimaryKeyUnicity = TRUE,
  115. key = "id"
  116. )
  117. for (i in 1:nrow(andmed)) {
  118. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  119. if (andmed$schema[i] %in% c("gtfs"))
  120. geom <- "geometry"
  121. else
  122. geom <- "geom"
  123. conf$table <- andmed$table[i]
  124. conf$schema <- andmed$schema[i]
  125. input <- ruut::construct_to_gpkg_output_postgres_str(
  126. conf = conf,
  127. geometry_type = "Point",
  128. srid = 3301,
  129. checkPrimaryKeyUnicity = TRUE,
  130. key = "id",
  131. geometry_field = geom
  132. )
  133. conf$table <-
  134. sprintf("data_p_%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. ## ------------------- QGIS: qgisprocess ----------------------
  148. str <-
  149. paste0(
  150. "{ 'INPUT' : '",
  151. input,
  152. "', 'INTERSECT' : '",
  153. intersect_layer,
  154. "', 'OUTPUT' : '",
  155. output,
  156. "', 'PREDICATE' : [0] }"
  157. )
  158. algorithm <- "native:extractbylocation"
  159. cmd <-
  160. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  161. cat(sprintf("\n%s\n\n", cmd))
  162. system(cmd)
  163. ## Filtreerime aadressandmete kihi eraldi alamkihtideks
  164. if (andmed$table[i] == "aadressandmed") {
  165. parent_table <-
  166. sprintf("data_p_%s_%s",
  167. strtrim(as.character(andmed$schema[i]), 2),
  168. andmed$table[i])
  169. conn <- ruut::db_connect(conf = conf)
  170. aadressandmed <-
  171. unique(as.data.frame(sf::st_read(
  172. dsn = conn,
  173. layer = c(conf$schema, parent_table),
  174. as_tibble = T
  175. ))[, c("adob_liik"), drop = FALSE])
  176. DBI::dbDisconnect(conn)
  177. for (k in 1:nrow(aadressandmed)) {
  178. table_suffix <- aadressandmed$adob_liik[k]
  179. conf$table <- parent_table
  180. conf$schema <- obj
  181. input <-
  182. ruut::construct_to_gpkg_output_postgres_str(
  183. conf = conf,
  184. geometry_type = "Point",
  185. srid = 3301,
  186. checkPrimaryKeyUnicity = TRUE,
  187. key = "id"
  188. )
  189. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  190. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  191. output <-
  192. ruut::construct_to_gpkg_output_postgres_str(
  193. conf = conf,
  194. geometry_field = "geom",
  195. geometry_type = "Polygon",
  196. srid = 3301,
  197. checkPrimaryKeyUnicity = FALSE,
  198. key = "id"
  199. )
  200. str <-
  201. paste0(
  202. "{ 'INPUT' : '",
  203. input,
  204. "', 'INTERSECT' : '",
  205. intersect_layer,
  206. "', 'OUTPUT' : '",
  207. output,
  208. "', FIELD : 'adob_liik', OPERATOR : 0, VALUE : ",
  209. aadressandmed$adob_liik[k],
  210. " }"
  211. )
  212. algorithm <- "native:extractbyattribute"
  213. cmd <-
  214. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  215. cat(sprintf("\n%s\n", cmd))
  216. system(cmd)
  217. }
  218. }
  219. }
  220. }
  221. ## Layers list
  222. conf$schema <- obj
  223. ruut::db_schema_tablenames(conf = conf)
  224. sf::st_layers(dsn = vars$tmp_gpkg_file)
  225. }