gpkg_piirkonnale_joonte_lisamine.R 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. andmed <- data.frame("schema" = character(0), "table" = character(0))
  45. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud"))
  46. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
  47. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
  48. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
  49. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
  50. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
  51. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
  52. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
  53. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
  54. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
  55. andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
  56. for (intersect in intersect_layers) {
  57. conf$gpkg_table <- intersect
  58. intersect_layer <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  59. for (i in 1:nrow(andmed)) {
  60. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  61. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  62. conf$gpkg_table <- sprintf("%s_%s", intersect, andmed$table[i])
  63. conf$table <- andmed$table[i]
  64. conf$schema <- andmed$schema[i]
  65. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  66. input <- ruut::construct_to_gpkg_output_postgres_str(
  67. conf = conf, geometry_type = "Linestring", srid = 3301,
  68. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  69. )
  70. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  71. ## !!! Trikk: alguses leiame ühisosaga piirkonnad
  72. ## ------------------- QGIS: qgisprocess ----------------------
  73. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", tmp_gpkg_file_output, "', 'PREDICATE' : [0] }")
  74. algorithm <- "native:extractbylocation"
  75. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  76. cat(sprintf("\n%s\n\n", cmd))
  77. system(cmd)
  78. ## !!! Trikk jätkub: edasi leiame alles ühisosa
  79. str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_input, "', 'OVERLAY' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'INPUT_FIELDS' : '', 'OVERLAY_FIELDS' : '', 'OVERLAY_FIELDS_PREFIX' : '' }")
  80. str <- paste0("{ 'INPUT' : '", tmp_gpkg_file_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. }
  86. }
  87. ## Layers list
  88. sf::st_layers(dsn = dsn)
  89. }