gpkg_piirkonnale_polygoonide_lisamine.R 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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()]
  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. conf$gpkg_home <- gpkg_home
  31. conf$gpkg_file <- obj
  32. postgres <- sprintf(
  33. "postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'id\' srid=3301 type=Polygon checkPrimaryKeyUnicity=\'1\' ",
  34. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password
  35. )
  36. tmp_gpkg_file <- tempfile(fileext = ".gpkg")
  37. ## Algorithm
  38. # ruut::qgis_algorithm_search_by_word(str = "extract")
  39. # algorithm <- "native:extractbylocation"
  40. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  41. ## -------------------- Loop -----------------------
  42. intersect_layers <- c("piir", "bb")
  43. andmed <- data.frame("schema" = character(0), "table" = character(0))
  44. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  45. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  46. andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  47. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  48. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
  49. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
  50. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
  51. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
  52. for (intersect in intersect_layers) {
  53. for (i in 1:nrow(andmed)) {
  54. conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
  55. output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
  56. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  57. result <- qgisprocess::qgis_run_algorithm(
  58. algorithm = "native:extractbylocation",
  59. INPUT = sprintf(
  60. '%s table=\"%s\".\"%s\" (geom)',
  61. postgres, andmed$schema[i], andmed$table[i]
  62. ),
  63. INTERSECT = sprintf("%s|layername=%s", dsn, intersect),
  64. OUTPUT = tmp_gpkg_file,
  65. PREDICATE = c(0)
  66. )
  67. ## !!! Trikk jätkub: edasi leiame alles ühisosa
  68. result <- qgisprocess::qgis_run_algorithm(
  69. algorithm = "native:intersection",
  70. INPUT = tmp_gpkg_file,
  71. INPUT_FIELDS = "",
  72. OVERLAY = sprintf("%s|layername=%s", dsn, intersect),
  73. OVERLAY_FIELDS = "",
  74. OVERLAY_FIELDS_PREFIX = "",
  75. OUTPUT = output
  76. # .quiet = TRUE
  77. )
  78. ## Filtreerime maakasutuse kihi 'landuse-a' eraldi alamkihtideks
  79. if (andmed$table[i] == "landuse_a") {
  80. landuse_a <- unique(as.data.frame(sf::read_sf(dsn = dsn, layer = sprintf("%s_landuse_a", intersect), as_tibble = T))[, c("code", "fclass")])
  81. parent_table <- conf$gpkg_table
  82. for (k in 1:nrow(landuse_a)) {
  83. table_suffix <- landuse_a$fclass[k]
  84. conf$gpkg_table <- sprintf("%s_%s", parent_table, table_suffix)
  85. output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
  86. result <- qgisprocess::qgis_run_algorithm(
  87. algorithm = "native:extractbyattribute",
  88. FIELD = "code",
  89. OPERATOR = 0, # 0 s.o '='
  90. VALUE = landuse_a$code[k],
  91. INPUT = sprintf("%s|layername=%s_%s", dsn, intersect, andmed$table[i]),
  92. OUTPUT = output,
  93. FAIL_OUTPUT = qgisprocess::qgis_tmp_vector()
  94. # .quiet = TRUE
  95. )
  96. }
  97. }
  98. }
  99. }
  100. ## Layers list
  101. sf::st_layers(dsn = dsn)
  102. }