02_piirkonnale_hoonete_lisamine.R 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #' Piirkonna hoonete lisamine
  2. #'
  3. #' Etteantud piirkonna geomeetrilise piirjoone ('piir') järele leitakse selles piirkonnas ja piirkonna piiriga piirnevad hooned ().
  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_hoonete_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. dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  24. input_layer_name <- "piir"
  25. input_layer <- sprintf("%s|layername=%s", dsn, input_layer_name)
  26. tmp_gpkg_file <- tempfile(fileext = ".gpkg")
  27. ## ----------------- piiri sisse jäävad polügoonid -------------------
  28. ## --------------------------- algandmed -----------------------------
  29. andmed.df <- data.frame("schema" = character(0), "table" = character(0))
  30. andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  31. # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
  32. j <- 1
  33. for (j in 1:nrow(andmed.df)) {
  34. output_layer_name <- as.character(andmed.df$table[j])
  35. input <- sprintf(
  36. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
  37. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  38. andmed.df$schema[j], andmed.df$table[j]
  39. )
  40. # ruut::qgis_algorithm_search_by_word(str = "extract")
  41. algorithm <- "native:extractbylocation"
  42. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  43. result <- qgisprocess::qgis_run_algorithm(
  44. algorithm = algorithm,
  45. INPUT = input,
  46. INTERSECT = input_layer,
  47. OUTPUT = tmp_gpkg_file,
  48. PREDICATE = c(0) # c(0, 1)
  49. # .quiet = TRUE
  50. )
  51. result
  52. # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
  53. system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
  54. }
  55. ## -------------- piiri -30 meetrise puhvri sisse jäävad polügoonid -------------
  56. ## --------------------------------- algandmed ---------------------------------
  57. andmed.df <- data.frame("schema" = character(0), "table" = character(0))
  58. andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  59. andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
  60. # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
  61. andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  62. andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  63. ## Esmalt leiame piirile uhverjoone.
  64. # ruut::qgis_algorithm_search_by_word(str = "buffer")
  65. algorithm <- "native:buffer"
  66. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  67. result <- qgisprocess::qgis_run_algorithm(
  68. algorithm = algorithm,
  69. DISSOLVE = 0,
  70. DISTANCE = -30,
  71. END_CAP_STYLE = 2,
  72. INPUT = "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/valga.gpkg|layername=piir",
  73. JOIN_STYLE = 2,
  74. MITER_LIMIT = 2,
  75. OUTPUT = tmp_gpkg_file,
  76. SEGMENTS = 5
  77. # .quiet = TRUE
  78. )
  79. result
  80. piir_buffer <- sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT"))
  81. # st_geometry(pk) %>% plot()
  82. # st_geometry(piir_buffer) %>% plot(add=T, col = 'red')
  83. j <- 1
  84. for (j in 1:nrow(andmed.df)) {
  85. output_layer_name <- as.character(andmed.df$table[j])
  86. input <- sprintf(
  87. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
  88. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  89. andmed.df$schema[j], andmed.df$table[j]
  90. )
  91. # ruut::qgis_algorithm_search_by_word(str = "extract")
  92. algorithm <- "native:extractbylocation"
  93. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  94. result <- qgisprocess::qgis_run_algorithm(
  95. algorithm = algorithm,
  96. INPUT = input,
  97. INTERSECT = piir_buffer,
  98. OUTPUT = tmp_gpkg_file,
  99. PREDICATE = c(0, 1)
  100. # .quiet = TRUE
  101. )
  102. result
  103. # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
  104. system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
  105. }
  106. ## ----------------- piiriga lõigatud polügoonid -------------------
  107. ## --------------------------- algandmed ---------------------------
  108. andmed.df <- data.frame("schema" = character(0), "table" = character(0))
  109. andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  110. andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
  111. j <- 1
  112. for (j in 1:nrow(andmed.df)) {
  113. output_layer_name <- as.character(andmed.df$table[j])
  114. input <- sprintf(
  115. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
  116. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  117. andmed.df$schema[j], andmed.df$table[j]
  118. )
  119. # ruut::qgis_algorithm_search_by_word(str = "intersect")
  120. algorithm <- "native:intersection"
  121. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  122. result <- qgisprocess::qgis_run_algorithm(
  123. algorithm = algorithm,
  124. INPUT = input,
  125. INPUT_FIELDS = "",
  126. OVERLAY = sprintf(
  127. "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/%s.gpkg|layername=piir",
  128. obj
  129. ),
  130. OVERLAY_FIELDS = "",
  131. OVERLAY_FIELDS_PREFIX = "",
  132. OUTPUT = tmp_gpkg_file
  133. # .quiet = TRUE
  134. )
  135. result
  136. # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
  137. system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
  138. }
  139. # ## ------------ andmed, mis jäävad piirikasti sisse ----------------
  140. # ## ------------------------- algandmed -----------------------------
  141. # andmed.df <- data.frame("schema" = character(0), "table" = character(0))
  142. # # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  143. # # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  144. # # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
  145. # # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed"))
  146. # # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  147. # # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  148. # j <- 1
  149. # for (j in 1:nrow(andmed.df)) {
  150. # output_layer_name <- andmed.df$table[j]
  151. # input <- sprintf(
  152. # 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
  153. # conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  154. # andmed.df$schema[j], andmed.df$table[j]
  155. # )
  156. # # ruut::qgis_algorithm_search_by_word(str = "extract")
  157. # algorithm <- "native:extractbylocation"
  158. # # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  159. # result <- qgisprocess::qgis_run_algorithm(
  160. # algorithm = algorithm,
  161. # INPUT = input,
  162. # INTERSECT = sprintf("%s|layername=%s", dsn, "boundarybox_3301"),
  163. # OUTPUT = tmp_gpkg_file,
  164. # PREDICATE = c(0, 1)
  165. # # .quiet = TRUE
  166. # )
  167. # result
  168. # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
  169. # system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
  170. # }
  171. ## ---------------------- vaata layer'id ----------------------
  172. # Vaata layer'eid
  173. sf::st_layers(dsn = dsn)
  174. }
  175. # piirkonnale_hoonete_lisamine(obj = NULL, pk = NULL, gpkg_home = "/tmp")