pk_lisame_polygoonid.R 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #' Piirkonnale andmebaasides olevate polügoonide 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_polygoonid(obj = obj, conf = NULL)
  16. #'
  17. #' # Layers list.
  18. #' ruut::db_schema_tablenames(conf = conf)
  19. #' }
  20. pk_lisame_polygoonid <- 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("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
  29. return(NULL)
  30. }
  31. ## Algorithm
  32. # ruut::qgis_algorithm_search_by_word(str = "extract")
  33. # algorithm <- "native:extractbylocation"
  34. # cat(ruut::qgis_show_help(algorithm = algorithm))
  35. ## -------------------- Loop -----------------------
  36. intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
  37. andmed <- data.frame("schema" = character(0), "table" = character(0))
  38. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  39. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  40. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  41. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  42. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
  43. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "places_a"))
  44. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
  45. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
  46. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "traffic_a"))
  47. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "transport_a"))
  48. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
  49. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a_dissolved"))
  50. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a_dissolved"))
  51. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a_dissolved"))
  52. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a_dissolved"))
  53. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_a_dissolved"))
  54. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a"))
  55. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a"))
  56. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a"))
  57. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_a"))
  58. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_ka"))
  59. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_302_ou_a"))
  60. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_303_haritav_maa_a"))
  61. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_304_lage_a"))
  62. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a"))
  63. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_ka"))
  64. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_307_turbavali_a"))
  65. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_401_hoone_ka"))
  66. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_ka"))
  67. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_404_maaalune_hoone_ka"))
  68. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_a"))
  69. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_ka"))
  70. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "mullakaart"))
  71. for (intersect in intersect_layers) {
  72. conf$table <- intersect
  73. conf$schema <- obj
  74. intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  75. for (i in 1:nrow(andmed)) {
  76. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  77. conf$table <- andmed$table[i]
  78. conf$schema <- andmed$schema[i]
  79. input <- ruut::construct_to_gpkg_output_postgres_str(
  80. conf = conf, geometry_type = "Polygon", srid = 3301,
  81. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  82. )
  83. # Geomeetria parandamine
  84. # ruut::qgis_algorithm_search_by_word("fix")
  85. algorithm <- "native:fixgeometries"
  86. output <- vars$tmp_gpkg_file_output_1 # ajutine fail
  87. str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  88. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  89. system(cmd)
  90. # Ühisosa leidmine ja salvestamine ajutiseks failiks.
  91. algorithm <- "native:intersection"
  92. input <- vars$tmp_gpkg_file_input_1
  93. output <- vars$tmp_gpkg_file_output_2 # ajutine fail
  94. str <- paste0("{ 'INPUT' : '", input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
  95. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  96. cat(sprintf("\n%s\n\n", cmd))
  97. system(cmd)
  98. ## Andmebaasi tabeli nimi: 2 esimest tähte tuleb schema nimest millest on andmed võetud, siis '_a_' s.o 'area' mis tähistab polügoone.
  99. # Eemaldame üleliigsed veerud ja salvestame postgis andmebaasi.
  100. # ruut::qgis_algorithm_search_by_word("Drop")
  101. algorithm <- "native:deletecolumn"
  102. input <- vars$tmp_gpkg_file_input_2
  103. conf$table <- sprintf("data_a_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  104. conf$schema <- obj
  105. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  106. cmd <- sprintf("qgis_process run %s --COLUMN='fid' --COLUMN='id_2' --COLUMN='left' --COLUMN='top' --COLUMN='right' --COLUMN='bottom' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  107. system(cmd)
  108. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  109. if (andmed$table[i] == "landuse_a") {
  110. parent_table <- sprintf("data_a_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  111. conn <- ruut::db_connect(conf = conf)
  112. landuse_a <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("code", "fclass")])
  113. for (k in 1:nrow(landuse_a)) {
  114. table_suffix <- landuse_a$fclass[k]
  115. conf$table <- parent_table
  116. conf$schema <- obj
  117. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  118. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  119. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  120. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  121. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'code', OPERATOR : 0, VALUE : '", landuse_a$code[k], "' }")
  122. algorithm <- "native:extractbyattribute"
  123. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  124. cat(sprintf("\n%s\n", cmd))
  125. system(cmd)
  126. }
  127. }
  128. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  129. if (andmed$table[i] == "e_401_hoone_ka") {
  130. parent_table <- sprintf("data_a_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  131. conn <- ruut::db_connect(conf = conf)
  132. alamobjektid <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("kood", "tyyp")])
  133. for (k in 1:nrow(alamobjektid)) {
  134. table_suffix <- alamobjektid$tyyp[k]
  135. conf$table <- parent_table
  136. conf$schema <- obj
  137. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  138. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  139. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  140. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  141. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '", alamobjektid$tyyp[k], "' }")
  142. algorithm <- "native:extractbyattribute"
  143. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  144. cat(sprintf("\n%s\n", cmd))
  145. system(cmd)
  146. ## ----------- lisame tsentroidid -----------
  147. parent_table <- sprintf("data_a_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  148. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  149. conf$schema <- obj
  150. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  151. conf$table <- sprintf("data_p_%s_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i], table_suffix)
  152. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Point", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  153. str <- sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  154. # ruut::qgis_algorithm_search_by_word("centroid")
  155. algorithm <- "native:centroids"
  156. # ruut::qgis_show_help(algorithm = algorithm)
  157. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  158. cat(sprintf("\n%s\n", cmd))
  159. system(cmd)
  160. }
  161. }
  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. }