gpkg_piirkonnale_joonte_lisamine.R 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #' Piirkonnale andmebaasides olevate joonte 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_joonte_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_joonte_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(qgisprocess::qgis_show_help(algorithm = algorithm))
  42. ## -------------------- Loop -----------------------
  43. intersect_layers <- c("piir", "bb")
  44. intersect_layers <- c("bb")
  45. andmed <- data.frame("schema" = character(0), "table" = character(0))
  46. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud"))
  47. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
  48. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
  49. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
  50. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
  51. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
  52. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
  53. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
  54. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
  55. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
  56. andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
  57. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_102_nolv_j"))
  58. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_103_pinnavorm_j"))
  59. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_j"))
  60. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_204_kaldajoon_j"))
  61. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_205_hudrotehniline_rajatis_j"))
  62. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_206_truup_j"))
  63. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_j"))
  64. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_405_piire_j"))
  65. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_j"))
  66. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_502_roobastee_j"))
  67. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_503_siht_j"))
  68. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_j"))
  69. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_601_elektriliin_j"))
  70. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_603_torujuhe_j"))
  71. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kolvikupiir_j"))
  72. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "korgus_j"))
  73. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "langujoon_j"))
  74. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "piir_j"))
  75. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sild_j"))
  76. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sygavus_j"))
  77. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "voolusuund_j"))
  78. for (intersect in intersect_layers) {
  79. conf$gpkg_table <- intersect
  80. intersect_layer <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  81. for (i in 1:nrow(andmed)) {
  82. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  83. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  84. conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
  85. conf$table <- andmed$table[i]
  86. conf$schema <- andmed$schema[i]
  87. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  88. input <- ruut::construct_to_gpkg_output_postgres_str(
  89. conf = conf, geometry_type = "Linestring", srid = 3301,
  90. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  91. )
  92. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  93. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  94. ## ------------------- QGIS: qgisprocess ----------------------
  95. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", tmp_gpkg_file_output, "', 'PREDICATE' : [0] }")
  96. algorithm <- "native:extractbylocation"
  97. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  98. cat(sprintf("\n%s\n\n", cmd))
  99. system(cmd)
  100. ## !!! Trikk jätkub: edasi leiame alles ühisosa
  101. str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
  102. algorithm <- "native:intersection"
  103. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  104. cat(sprintf("\n%s\n\n", cmd))
  105. system(cmd)
  106. }
  107. }
  108. ## Layers list
  109. sf::st_layers(dsn = dsn)
  110. }