gpkg_piirkonnale_punktide_lisamine.R 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #' Piirkonnale andmebaasides olevate punktide 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_punktide_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_punktide_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=Point 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" = "maaamet", "table" = "aadressandmed")) # POINT
  45. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw"))
  46. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois"))
  47. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
  48. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
  49. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
  50. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
  51. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
  52. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
  53. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
  54. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
  55. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
  56. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
  57. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
  58. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
  59. for (intersect in intersect_layers) {
  60. for (i in 1:nrow(andmed)) {
  61. conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
  62. output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
  63. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  64. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  65. result <- qgisprocess::qgis_run_algorithm(
  66. algorithm = "native:extractbylocation",
  67. INPUT = sprintf(
  68. '%s table=\"%s\".\"%s\" (%s)',
  69. postgres, andmed$schema[i], andmed$table[i], geom
  70. ),
  71. INTERSECT = sprintf("%s|layername=%s", dsn, intersect),
  72. OUTPUT = tmp_gpkg_file,
  73. PREDICATE = c(0)
  74. )
  75. ## !!! Trikk jätkub: edasi leiame alles ühisosa
  76. result <- qgisprocess::qgis_run_algorithm(
  77. algorithm = "native:intersection",
  78. INPUT = tmp_gpkg_file,
  79. INPUT_FIELDS = "",
  80. OVERLAY = sprintf("%s|layername=%s", dsn, intersect),
  81. OVERLAY_FIELDS = "",
  82. OVERLAY_FIELDS_PREFIX = "",
  83. OUTPUT = output
  84. # .quiet = TRUE
  85. )
  86. ## Filtreerime aadressandmete kihi eraldi alamkihtideks
  87. if (andmed$table[i] == "aadressandmed") {
  88. aadressandmed <- unique(as.data.frame(sf::read_sf(dsn = dsn, layer = sprintf("%s_aadressandmed", intersect), as_tibble = T))[, c("adob_liik"), drop = FALSE])
  89. parent_table <- conf$gpkg_table
  90. for (k in 1:nrow(aadressandmed)) {
  91. table_suffix <- aadressandmed$adob_liik[k]
  92. conf$gpkg_table <- sprintf("%s_%s", parent_table, table_suffix)
  93. output <- ruut::construct_to_gpkg_output_file_str(conf = conf)
  94. result <- qgisprocess::qgis_run_algorithm(
  95. algorithm = "native:extractbyattribute",
  96. FIELD = "adob_liik",
  97. OPERATOR = 0, # 0 s.o '='
  98. VALUE = aadressandmed$adob_liik[k],
  99. INPUT = sprintf("%s|layername=%s_%s", dsn, intersect, andmed$table[i]),
  100. OUTPUT = output,
  101. FAIL_OUTPUT = qgisprocess::qgis_tmp_vector()
  102. # .quiet = TRUE
  103. )
  104. }
  105. }
  106. }
  107. }
  108. ## Layers list
  109. sf::st_layers(dsn = dsn)
  110. }