pk_lisame_jooned.R 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ## ------------- muutujad ja teisendused ---------------
  22. vars <- ajutised_muutujad(pk = NULL, obj, conf)
  23. obj <- vars$obj
  24. piir <- vars$pk
  25. conf <- vars$conf
  26. ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
  27. if (!any("a00_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. ## Algorithm
  32. # ruut::qgis_algorithm_search_by_word(str = "extract")
  33. # algorithm <- "native:extractbylocation"
  34. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  35. ## -------------------- Loop -----------------------
  36. intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
  37. andmed <- data.frame("schema" = character(0), "table" = character(0))
  38. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud"))
  39. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
  40. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
  41. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
  42. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
  43. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
  44. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
  45. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
  46. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
  47. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
  48. andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
  49. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_j"))
  50. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_j"))
  51. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_405_piire_j"))
  52. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_j"))
  53. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_502_roobastee_j"))
  54. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_j"))
  55. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_601_elektriliin_j"))
  56. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "korgus_j"))
  57. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sild_j"))
  58. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sygavus_j"))
  59. for (intersect in intersect_layers) {
  60. conf$table <- intersect
  61. conf$schema <- obj
  62. intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  63. for (i in 1:nrow(andmed)) {
  64. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  65. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  66. conf$table <- andmed$table[i]
  67. conf$schema <- andmed$schema[i]
  68. input <- ruut::construct_to_gpkg_output_postgres_str(
  69. conf = conf, geometry_type = "Linestring", srid = 3301,
  70. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  71. )
  72. ## Andmebaasi tabeli nimi: 2 esimest tähte tuleb schema nimest millest on andmed võetud, siis '_l_' s.o 'line' mis tähistab jooni.
  73. conf$table <- sprintf("%s_l_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  74. conf$schema <- obj
  75. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  76. str <- paste0("{ 'INPUT' : '", input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
  77. algorithm <- "native:intersection"
  78. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  79. cat(sprintf("\n%s\n\n", cmd))
  80. system(cmd)
  81. }
  82. }
  83. ## Layers list
  84. conf$schema <- obj
  85. ruut::db_schema_tablenames(conf = conf)
  86. sf::st_layers(dsn = vars$tmp_gpkg_file)
  87. }