pk_lisame_polygoonid.R 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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" = "osm_shp", "table" = "landuse_a"))
  39. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  40. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  41. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  42. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
  43. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
  44. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
  45. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
  46. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a_dissolved"))
  47. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a_dissolved"))
  48. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a_dissolved"))
  49. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_a"))
  50. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_ka"))
  51. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_302_ou_a"))
  52. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_303_haritav_maa_a"))
  53. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_304_lage_a"))
  54. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a_dissolved"))
  55. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_a_dissolved"))
  56. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_ka"))
  57. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_307_turbavali_a"))
  58. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_401_hoone_ka"))
  59. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_ka"))
  60. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_404_maaalune_hoone_ka"))
  61. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_a"))
  62. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_ka"))
  63. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "mullakaart"))
  64. for (intersect in intersect_layers) {
  65. conf$table <- intersect
  66. conf$schema <- obj
  67. intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  68. for (i in 1:nrow(andmed)) {
  69. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  70. conf$table <- andmed$table[i]
  71. conf$schema <- andmed$schema[i]
  72. input <- ruut::construct_to_gpkg_output_postgres_str(
  73. conf = conf, geometry_type = "Polygon", srid = 3301,
  74. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  75. )
  76. ## 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.
  77. conf$table <- sprintf("%s_a_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  78. conf$schema <- obj
  79. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  80. str <- paste0("{ 'INPUT' : '", input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
  81. algorithm <- "native:intersection"
  82. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  83. cat(sprintf("\n%s\n\n", cmd))
  84. system(cmd)
  85. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  86. if (andmed$table[i] == "landuse_a") {
  87. parent_table <- sprintf("%s_a_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  88. conn <- ruut::db_connect(conf = conf)
  89. landuse_a <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("code", "fclass")])
  90. for (k in 1:nrow(landuse_a)) {
  91. table_suffix <- landuse_a$fclass[k]
  92. conf$table <- parent_table
  93. conf$schema <- obj
  94. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  95. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  96. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  97. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  98. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'code', OPERATOR : 0, VALUE : '", landuse_a$code[k], "' }")
  99. algorithm <- "native:extractbyattribute"
  100. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  101. cat(sprintf("\n%s\n", cmd))
  102. system(cmd)
  103. }
  104. }
  105. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  106. if (andmed$table[i] == "e_401_hoone_ka") {
  107. parent_table <- sprintf("%s_a_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  108. conn <- ruut::db_connect(conf = conf)
  109. alamobjektid <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("kood", "tyyp")])
  110. for (k in 1:nrow(alamobjektid)) {
  111. table_suffix <- alamobjektid$tyyp[k]
  112. conf$table <- parent_table
  113. conf$schema <- obj
  114. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  115. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  116. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  117. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  118. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '", alamobjektid$tyyp[k], "' }")
  119. algorithm <- "native:extractbyattribute"
  120. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  121. cat(sprintf("\n%s\n", cmd))
  122. system(cmd)
  123. ## ----------- lisame tsentroidid -----------
  124. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  125. conf$schema <- obj
  126. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  127. conf$table <- sprintf("%s_%s_p", 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 <- sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  130. # ruut::qgis_algorithm_search_by_word("centroid")
  131. algorithm <- "native:centroids"
  132. # ruut::qgis_show_help(algorithm = algorithm)
  133. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  134. cat(sprintf("\n%s\n", cmd))
  135. system(cmd)
  136. }
  137. }
  138. }
  139. }
  140. ## Layers list
  141. conf$schema <- obj
  142. ruut::db_schema_tablenames(conf = conf)
  143. sf::st_layers(dsn = vars$tmp_gpkg_file)
  144. }