04_piirkonnale_joonte_lisamine.R 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #' Piirkonna joonte lisamine
  2. #'
  3. #' Etteantud piirkonna geomeetrilise piirjoone ('piir') järele leitakse selles piirkonnas ja piirkonna piiriga piirnevad joonobjektid ().
  4. #'
  5. #' @param obj str Objekti nimi.
  6. #' @param pk Piirkonna geomeetriline joon.
  7. #' @param gpkg_home path Salvestatavate GPKG faili asukoht.
  8. #'
  9. #' @examples
  10. #' \dontrun{
  11. #'
  12. #' }
  13. piirkonnale_joonte_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tmp") {
  14. if (is.null(pk) || !sf::st_is_valid(pk)) {
  15. cat("\nPalun kontrolli geomeetrilise kujundi õigsust.\n")
  16. return()
  17. }
  18. if (is.null(obj)) {
  19. cat("\nPuudu on objekti nimi.\n")
  20. return()
  21. }
  22. cat(sprintf("\nAlgparameetrid: objekti nimi %s ja GPKG faili kataloog %s\n", obj, gpkg_home))
  23. postgres <- sprintf(
  24. "postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=%s checkPrimaryKeyUnicity=\'1\'",
  25. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password, "Polygon"
  26. )
  27. postgres_point <- sprintf(
  28. "postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=%s checkPrimaryKeyUnicity=\'1\'",
  29. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password, "Point"
  30. )
  31. postgres_line <- sprintf(
  32. "postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=%s checkPrimaryKeyUnicity=\'1\'",
  33. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password, "Linestring"
  34. )
  35. dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj) # Polygon
  36. dsn_p <- sprintf("%s/%s_p.gpkg", gpkg_home, obj) # Point
  37. dsn_l <- sprintf("%s/%s_l.gpkg", gpkg_home, obj) # Linestring
  38. ogr <- sprintf(
  39. "ogr:dbname='%s'",
  40. dsn_l
  41. )
  42. tmp_gpkg_file <- tempfile(fileext = ".gpkg")
  43. ## ----------------- punktfaili loomine -------------------
  44. ## -------------- epk2t_grids tsentroidid -----------------
  45. # ruut::qgis_algorithm_search_by_word(str = "polygons")
  46. algorithm <- "native:polygonstolines"
  47. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  48. result <- qgisprocess::qgis_run_algorithm(
  49. algorithm = algorithm,
  50. INPUT = sprintf("%s|layername=%s", dsn, "piir"),
  51. OUTPUT = sprintf('%s table=\"%s\" (geometry)', ogr, "piir_l")
  52. # .quiet = TRUE
  53. )
  54. ## ----------------- piiri sisse jäävad punktid -------------------
  55. ## --------------------------- algandmed --------------------------
  56. andmed.df <- data.frame("schema" = character(0), "table" = character(0))
  57. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud"))
  58. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
  59. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
  60. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
  61. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
  62. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
  63. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
  64. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
  65. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
  66. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
  67. andmed.df <- rbind(andmed.df, data.frame("schema" = "gtfs", "table" = "shapes"))
  68. j <- 1
  69. for (j in 1:nrow(andmed.df)) {
  70. output_layer_name <- as.character(andmed.df$table[j])
  71. # ruut::qgis_algorithm_search_by_word(str = "intersect")
  72. algorithm <- "native:intersection"
  73. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  74. result <- qgisprocess::qgis_run_algorithm(
  75. algorithm = algorithm,
  76. INPUT = sprintf('%s table="%s"."%s" (geometry)', postgres_line, andmed.df$schema[j], andmed.df$table[j]),
  77. INPUT_FIELDS = "",
  78. # OUTPUT = sprintf('%s table=\"%s\" (geometry)', ogr, andmed.df$table[j]),
  79. OUTPUT = tmp_gpkg_file,
  80. OVERLAY = sprintf("%s|layername=%s", dsn, "piir"),
  81. OVERLAY_FIELDS = "",
  82. OVERLAY_FIELDS_PREFIX = ""
  83. # # .quiet = TRUE
  84. )
  85. result
  86. # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
  87. system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn_l, tmp_gpkg_file, output_layer_name))
  88. ## AINULT ELAMUD aadressandmetest!!
  89. # if (output_layer_name == "aadressandmed") {
  90. # system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s_ee -t_srs \"EPSG:3301\" -where \"adob_liik = 'EE'\" ", dsn_p, tmp_gpkg_file, output_layer_name))
  91. # }
  92. }
  93. ## ---------------------- vaata layer'id ----------------------
  94. # Vaata layer'eid
  95. sf::st_layers(dsn = dsn)
  96. sf::st_layers(dsn = dsn_p)
  97. sf::st_layers(dsn = dsn_l)
  98. }
  99. # piirkonnale_joonte_lisamine(obj = NULL, pk = NULL, gpkg_home = "/tmp")