03_piirkonnale_punktide_lisamine.R 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #' Piirkonna punktide lisamine
  2. #'
  3. #' Etteantud piirkonna geomeetrilise piirjoone ('piir') järele leitakse selles piirkonnas ja piirkonna piiriga piirnevad punktobjektid ().
  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_punktide_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. dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  32. dsn_p <- sprintf("%s/%s_p.gpkg", gpkg_home, obj)
  33. ogr <- sprintf(
  34. "ogr:dbname='%s'",
  35. dsn_p
  36. )
  37. tmp_gpkg_file <- tempfile(fileext = ".gpkg")
  38. ## ----------------- punktfaili loomine -------------------
  39. ## -------------- epk2t_grids tsentroidid -----------------
  40. # ruut::qgis_algorithm_search_by_word(str = "centroid")
  41. algorithm <- "native:centroids"
  42. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  43. result <- qgisprocess::qgis_run_algorithm(
  44. algorithm = algorithm,
  45. ALL_PARTS = 1,
  46. INPUT = sprintf("%s|layername=%s", dsn, "epk02t_grid"),
  47. OUTPUT = sprintf('%s table=\"%s\" (geometry)', ogr, "epk2t_centroid")
  48. # .quiet = TRUE
  49. )
  50. ## ----------------- piiri sisse jäävad punktid -------------------
  51. ## --------------------------- algandmed --------------------------
  52. andmed.df <- data.frame("schema" = character(0), "table" = character(0))
  53. # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
  54. # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "pofw"))
  55. # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "pois"))
  56. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
  57. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
  58. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
  59. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
  60. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
  61. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
  62. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
  63. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
  64. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
  65. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
  66. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
  67. andmed.df <- rbind(andmed.df, data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
  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_point, 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_p, 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_p)
  96. }
  97. # piirkonnale_punktide_lisamine(obj = NULL, pk = NULL, gpkg_home = "/tmp")