pk_lisame_polygoonid.R 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_lisame_piirkonnale_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. obj <- gsub(" ", "_", tolower(obj))
  22. ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
  23. if (is.null(conf)) {
  24. conf <- ruut::get_config()
  25. conf$schema <- obj
  26. }
  27. if (!any("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. ## ------------- muutujad ja teisendused ---------------
  32. conn <- ruut::db_connect(conf = conf)
  33. try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
  34. tmp_dir <- tempdir()
  35. tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
  36. # system(sprintf("touch %s", tmp_gpkg_file))
  37. conf$gpkg_home <- tmp_dir
  38. conf$gpkg_file <- obj
  39. conf$gpkg_table <- "test_layer"
  40. tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  41. tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  42. # write to gpkg
  43. sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
  44. # unlink(tmp_gpkg_file)
  45. ## Algorithm
  46. # ruut::qgis_algorithm_search_by_word(str = "extract")
  47. # algorithm <- "native:extractbylocation"
  48. # cat(ruut::qgis_show_help(algorithm = algorithm))
  49. ## -------------------- Loop -----------------------
  50. intersect_layers <- c("bb2") # c("piir", "bb", "bb2")
  51. andmed <- data.frame("schema" = character(0), "table" = character(0))
  52. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  53. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  54. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  55. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  56. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
  57. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
  58. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
  59. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
  60. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a_dissolved"))
  61. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a_dissolved"))
  62. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a_dissolved"))
  63. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_a"))
  64. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_ka"))
  65. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_302_ou_a"))
  66. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_303_haritav_maa_a"))
  67. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_304_lage_a"))
  68. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a_dissolved"))
  69. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_a_dissolved"))
  70. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_ka"))
  71. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_307_turbavali_a"))
  72. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_401_hoone_ka"))
  73. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_ka"))
  74. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_404_maaalune_hoone_ka"))
  75. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_a"))
  76. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_ka"))
  77. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "mullakaart"))
  78. for (intersect in intersect_layers) {
  79. conf$table <- intersect
  80. conf$schema <- obj
  81. intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  82. for (i in 1:nrow(andmed)) {
  83. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  84. conf$table <- andmed$table[i]
  85. conf$schema <- andmed$schema[i]
  86. input <- ruut::construct_to_gpkg_output_postgres_str(
  87. conf = conf, geometry_type = "Polygon", srid = 3301,
  88. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  89. )
  90. conf$table <- sprintf("%s", andmed$table[i])
  91. conf$schema <- obj
  92. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  93. str <- paste0("{ 'INPUT' : '", 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::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("code", "fclass")])
  101. parent_table <- as.character(andmed$table[i])
  102. for (k in 1:nrow(landuse_a)) {
  103. table_suffix <- landuse_a$fclass[k]
  104. conf$table <- parent_table
  105. conf$schema <- obj
  106. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  107. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  108. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  109. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  110. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'code', OPERATOR : 0, VALUE : '", landuse_a$code[k], "' }")
  111. algorithm <- "native:extractbyattribute"
  112. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  113. cat(sprintf("\n%s\n", cmd))
  114. system(cmd)
  115. }
  116. }
  117. ## Filtreerime hoonete kihi 'landuse-a' eraldi tüübi järele alamkihtideks
  118. if (andmed$table[i] == "e_401_hoone_ka") {
  119. alamobjektid <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("kood", "tyyp")])
  120. parent_table <- as.character(andmed$table[i])
  121. for (k in 1:nrow(alamobjektid)) {
  122. table_suffix <- alamobjektid$tyyp[k]
  123. conf$table <- parent_table
  124. conf$schema <- obj
  125. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  126. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  127. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  128. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  129. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '", alamobjektid$tyyp[k], "' }")
  130. algorithm <- "native:extractbyattribute"
  131. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  132. cat(sprintf("\n%s\n", cmd))
  133. system(cmd)
  134. ## ----------- lisame tsentroidid -----------
  135. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  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. conf$table <- sprintf("%s_%s_p", parent_table, table_suffix)
  139. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  140. str <- sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  141. # ruut::qgis_algorithm_search_by_word("centroid")
  142. algorithm <- "native:centroids"
  143. # ruut::qgis_show_help(algorithm = algorithm)
  144. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  145. cat(sprintf("\n%s\n", cmd))
  146. system(cmd)
  147. }
  148. }
  149. }
  150. }
  151. ## Layers list
  152. conf$schema <- obj
  153. ruut::db_schema_tablenames(conf = conf)
  154. sf::st_layers(dsn = tmp_gpkg_file)
  155. }