pk_lisame_jooned.R 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 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_jooned(obj = obj, conf = NULL)
  16. #'
  17. #' # Layers list.
  18. #' ruut::db_schema_tablenames(conf = conf)
  19. #' }
  20. pk_lisame_jooned <- function(obj = NULL, conf = NULL) {
  21. obj <- gsub(" ", "_", tolower(obj))
  22. ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
  23. if (is.null(conf)) {
  24. conf <- ruut::get_config()
  25. conf$schema <- obj
  26. }
  27. if (!any("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. ## ------------- muutujad ja teisendused ---------------
  32. conn <- ruut::db_connect(conf = conf)
  33. try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
  34. tmp_dir <- tempdir()
  35. tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
  36. # system(sprintf("touch %s", tmp_gpkg_file))
  37. conf$gpkg_home <- tmp_dir
  38. conf$gpkg_file <- obj
  39. conf$gpkg_table <- "test_layer"
  40. tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  41. tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  42. # write to gpkg
  43. sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
  44. # unlink(tmp_gpkg_file)
  45. ## Algorithm
  46. # ruut::qgis_algorithm_search_by_word(str = "extract")
  47. # algorithm <- "native:extractbylocation"
  48. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  49. ## -------------------- Loop -----------------------
  50. intersect_layers <- c("bb2") # c("piir", "bb", "bb2")
  51. andmed <- data.frame("schema" = character(0), "table" = character(0))
  52. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud"))
  53. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
  54. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
  55. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
  56. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
  57. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
  58. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
  59. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
  60. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
  61. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
  62. andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
  63. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_j"))
  64. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_j"))
  65. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_405_piire_j"))
  66. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_j"))
  67. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_502_roobastee_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" = "korgus_j"))
  71. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sild_j"))
  72. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sygavus_j"))
  73. for (intersect in intersect_layers) {
  74. conf$table <- intersect
  75. conf$schema <- obj
  76. intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  77. for (i in 1:nrow(andmed)) {
  78. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  79. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  80. conf$table <- andmed$table[i]
  81. conf$schema <- andmed$schema[i]
  82. input <- ruut::construct_to_gpkg_output_postgres_str(
  83. conf = conf, geometry_type = "Linestring", srid = 3301,
  84. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  85. )
  86. conf$table <- sprintf("%s", andmed$table[i])
  87. conf$schema <- obj
  88. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  89. str <- paste0("{ 'INPUT' : '", input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
  90. algorithm <- "native:intersection"
  91. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  92. cat(sprintf("\n%s\n\n", cmd))
  93. system(cmd)
  94. }
  95. }
  96. ## Layers list
  97. conf$schema <- obj
  98. ruut::db_schema_tablenames(conf = conf)
  99. sf::st_layers(dsn = tmp_gpkg_file)
  100. }