pk_lisame_punktid.R 6.9 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 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_punktid(obj = obj, conf = NULL)
  16. #'
  17. #' # Layers list.
  18. #' ruut::db_schema_tablenames(conf = conf)
  19. #' }
  20. pk_lisame_punktid <- 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" = "maaamet", "table" = "aadressandmed")) # POINT
  39. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw"))
  40. andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois"))
  41. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
  42. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
  43. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
  44. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
  45. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
  46. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
  47. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
  48. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
  49. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
  50. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
  51. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
  52. andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
  53. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_p"))
  54. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_p"))
  55. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_p"))
  56. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_402_korgrajatis_p"))
  57. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_p"))
  58. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_602_tehnopaigaldis_p"))
  59. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kirik_p"))
  60. andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kohanimi"))
  61. for (intersect in intersect_layers) {
  62. conf$table <- intersect
  63. conf$schema <- obj
  64. intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  65. for (i in 1:nrow(andmed)) {
  66. ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
  67. if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  68. conf$table <- andmed$table[i]
  69. conf$schema <- andmed$schema[i]
  70. input <- ruut::construct_to_gpkg_output_postgres_str(
  71. conf = conf, geometry_type = "Point", srid = 3301,
  72. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
  73. )
  74. conf$table <- sprintf("data_p_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  75. conf$schema <- obj
  76. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  77. ## ------------------- QGIS: qgisprocess ----------------------
  78. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'PREDICATE' : [0] }")
  79. algorithm <- "native:extractbylocation"
  80. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  81. cat(sprintf("\n%s\n\n", cmd))
  82. system(cmd)
  83. ## Filtreerime aadressandmete kihi eraldi alamkihtideks
  84. if (andmed$table[i] == "aadressandmed") {
  85. parent_table <- sprintf("data_p_%s_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
  86. conn <- ruut::db_connect(conf = conf)
  87. aadressandmed <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("adob_liik"), drop = FALSE])
  88. for (k in 1:nrow(aadressandmed)) {
  89. table_suffix <- aadressandmed$adob_liik[k]
  90. conf$table <- parent_table
  91. conf$schema <- obj
  92. input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Point", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
  93. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  94. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  95. output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
  96. str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'adob_liik', OPERATOR : 0, VALUE : ", aadressandmed$adob_liik[k], " }")
  97. algorithm <- "native:extractbyattribute"
  98. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  99. cat(sprintf("\n%s\n", cmd))
  100. system(cmd)
  101. }
  102. }
  103. }
  104. }
  105. ## Layers list
  106. conf$schema <- obj
  107. ruut::db_schema_tablenames(conf = conf)
  108. sf::st_layers(dsn = vars$tmp_gpkg_file)
  109. }