gpkg_piirkonnale_polygoonide_lisamine.R 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 GPKG faili kihtidena.
  4. #'
  5. #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
  6. #' @param gpkg_home path Salvestatavate GPKG faili asukoht.
  7. #' @return Uute GPKG andmebaasi kihtide 'piir_...' ja 'bb_...' loomine.
  8. #' @seealso [sf::st_read()], [sf::write_sf()],[sf::st_transform()],[ruut::gpkg_piirkonnale_ruudustike_lisamine()] ,[ruut::gpkg_piirkonnale_polygoonide_lisamine()],[ruut::gpkg_sellest_alustame_gpkg_loomist()],[ruut::gpkg_piirkonnale_joonte_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_teisendame_polygoone()],[ruut::gpkg_teisendame_jooni()],[ruut::gpkg_teisendame_punkte()]
  9. #' @keywords GPKG, boundary box, EPSG:3301
  10. #' @export
  11. #' @examples
  12. #' \dontrun{
  13. #'
  14. #' gpkg_home <- "/tmp"
  15. #' obj <- "marja"
  16. #' gpkg_piirkonnale_polygoonide_lisamine(obj = obj, gpkg_home = gpkg_home)
  17. #'
  18. #' # Layers list.
  19. #' dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  20. #' sf::st_layers(dsn = dsn)
  21. #' }
  22. gpkg_piirkonnale_polygoonide_lisamine <- function(obj = NULL, gpkg_home = "/tmp") {
  23. dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  24. if (!file.exists(dsn)) {
  25. cat(sprintf("\nSellist faili \"%s\" ei leitud.\n", dsn))
  26. return(NULL)
  27. }
  28. ## Konfiguratsiooni muutujale väärtuste omistamine
  29. conf <- ruut::get_config()
  30. tmp_gpkg_file <- tempfile(fileext = ".gpkg")
  31. conf$gpkg_home <- dirname(tmp_gpkg_file)
  32. conf$gpkg_file <- gsub(paste0(conf$gpkg_home, "/"), "", tmp_gpkg_file)
  33. conf$gpkg_file <- gsub(".gpkg", "", conf$gpkg_file)
  34. conf$gpkg_home <- gpkg_home
  35. conf$gpkg_file <- obj
  36. tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  37. tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  38. ## Algorithm
  39. # ruut::qgis_algorithm_search_by_word(str = "extract")
  40. # algorithm <- "native:extractbylocation"
  41. # cat(ruut::qgis_show_help(algorithm = algorithm))
  42. ## -------------------- Loop -----------------------
  43. intersect_layers <- c("bb2") # c("piir", "bb", "bb2")
  44. andmed <- data.frame("schema" = character(0), "table" = character(0))
  45. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  46. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  47. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  48. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  49. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
  50. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
  51. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
  52. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
  53. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a_dissolved"))
  54. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a_dissolved"))
  55. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a_dissolved"))
  56. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_a"))
  57. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_ka"))
  58. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_302_ou_a"))
  59. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_303_haritav_maa_a"))
  60. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_304_lage_a"))
  61. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a_dissolved"))
  62. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_a_dissolved"))
  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$gpkg_table <- intersect
  73. intersect_layer <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  74. for (i in 1:nrow(andmed)) {
  75. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  76. conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
  77. conf$table <- andmed$table[i]
  78. conf$schema <- andmed$schema[i]
  79. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  80. input <- ruut::construct_to_gpkg_output_postgres_str(
  81. conf = conf, geometry_type = "Polygon", srid = 3301,
  82. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  83. )
  84. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  85. ## ------------------- QGIS: qgisprocess ----------------------
  86. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", tmp_gpkg_file_output, "', 'PREDICATE' : [0] }")
  87. algorithm <- "native:extractbylocation"
  88. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  89. cat(sprintf("\n%s\n\n", cmd))
  90. system(cmd)
  91. ## !!! Trikk jätkub: edasi leiame alles ühisosa
  92. str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'OVERLAY' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'INPUT_FIELDS' : '', 'OVERLAY_FIELDS' : '', 'OVERLAY_FIELDS_PREFIX' : '' }")
  93. str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
  94. algorithm <- "native:intersection"
  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. ## Filtreerime maakasutuse kihi 'landuse-a' eraldi alamkihtideks
  99. if (andmed$table[i] == "landuse_a") {
  100. landuse_a <- unique(as.data.frame(sf::read_sf(dsn = dsn, layer = sprintf("%s_landuse_a", intersect), as_tibble = T))[, c("code", "fclass")])
  101. parent_table <- conf$gpkg_table
  102. for (k in 1:nrow(landuse_a)) {
  103. table_suffix <- landuse_a$fclass[k]
  104. conf$gpkg_table <- parent_table
  105. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
  106. conf$gpkg_table <- sprintf("%s_%s", conf$gpkg_table, table_suffix)
  107. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = FALSE)
  108. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'code', OPERATOR : 0, VALUE : '", landuse_a$code[k], "' }")
  109. algorithm <- "native:extractbyattribute"
  110. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  111. cat(sprintf("\n%s\n", cmd))
  112. system(cmd)
  113. }
  114. }
  115. ## Filtreerime hoonete kihi 'landuse-a' eraldi tüübi järele alamkihtideks
  116. if (andmed$table[i] == "e_401_hoone_ka") {
  117. alamobjektid <- unique(as.data.frame(sf::read_sf(dsn = dsn, layer = sprintf("%s_e_401_hoone_ka", intersect), as_tibble = T), drop = FALSE)[, c("kood", "tyyp")])
  118. parent_table <- conf$gpkg_table
  119. for (k in 1:nrow(alamobjektid)) {
  120. table_suffix <- alamobjektid$tyyp[k]
  121. conf$gpkg_table <- parent_table
  122. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
  123. conf$gpkg_table <- sprintf("%s_%s", conf$gpkg_table, table_suffix)
  124. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = FALSE)
  125. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '", alamobjektid$tyyp[k], "' }")
  126. algorithm <- "native:extractbyattribute"
  127. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  128. cat(sprintf("\n%s\n", cmd))
  129. system(cmd)
  130. ## ----------- lisame tsentroidid -----------
  131. conf$gpkg_table <- sprintf("%s_%s", parent_table, table_suffix)
  132. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
  133. conf$gpkg_table <- sprintf("%s_%s_p", parent_table, table_suffix)
  134. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = FALSE)
  135. str <- sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  136. # ruut::qgis_algorithm_search_by_word("centroid")
  137. algorithm <- "native:centroids"
  138. # ruut::qgis_show_help(algorithm = algorithm)
  139. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  140. cat(sprintf("\n%s\n", cmd))
  141. system(cmd)
  142. }
  143. }
  144. }
  145. }
  146. ## Layers list
  147. sf::st_layers(dsn = dsn)
  148. }