pk_lisame_punktid.R 7.5 KB

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