| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #' Piirkonna hoonete lisamine
- #'
- #' Etteantud piirkonna geomeetrilise piirjoone ('piir') järele leitakse selles piirkonnas ja piirkonna piiriga piirnevad hooned ().
- #'
- #' @param obj str Objekti nimi.
- #' @param pk Piirkonna geomeetriline joon.
- #' @param gpkg_home path Salvestatavate GPKG faili asukoht.
- #'
- #' @examples
- #' \dontrun{
- #'
- #' }
- piirkonnale_hoonete_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tmp") {
- if (is.null(pk) || !sf::st_is_valid(pk)) {
- cat("\nPalun kontrolli geomeetrilise kujundi õigsust.\n")
- return()
- }
- if (is.null(obj)) {
- cat("\nPuudu on objekti nimi.\n")
- return()
- }
- cat(sprintf("\nAlgparameetrid: objekti nimi %s ja GPKG faili kataloog %s\n", obj, gpkg_home))
- dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
- input_layer_name <- "piir"
- input_layer <- sprintf("%s|layername=%s", dsn, input_layer_name)
- tmp_gpkg_file <- tempfile(fileext = ".gpkg")
- ## ----------------- piiri sisse jäävad polügoonid -------------------
- ## --------------------------- algandmed -----------------------------
- andmed.df <- data.frame("schema" = character(0), "table" = character(0))
- andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
- # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
- j <- 1
- for (j in 1:nrow(andmed.df)) {
- output_layer_name <- as.character(andmed.df$table[j])
- input <- sprintf(
- '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)',
- conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
- andmed.df$schema[j], andmed.df$table[j]
- )
- # ruut::qgis_algorithm_search_by_word(str = "extract")
- algorithm <- "native:extractbylocation"
- # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- result <- qgisprocess::qgis_run_algorithm(
- algorithm = algorithm,
- INPUT = input,
- INTERSECT = input_layer,
- OUTPUT = tmp_gpkg_file,
- PREDICATE = c(0) # c(0, 1)
- # .quiet = TRUE
- )
- result
- # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
- system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
- }
- ## -------------- piiri -30 meetrise puhvri sisse jäävad polügoonid -------------
- ## --------------------------------- algandmed ---------------------------------
- andmed.df <- data.frame("schema" = character(0), "table" = character(0))
- andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
- andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
- # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
- andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
- andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
- ## Esmalt leiame piirile uhverjoone.
- # ruut::qgis_algorithm_search_by_word(str = "buffer")
- algorithm <- "native:buffer"
- # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- result <- qgisprocess::qgis_run_algorithm(
- algorithm = algorithm,
- DISSOLVE = 0,
- DISTANCE = -30,
- END_CAP_STYLE = 2,
- INPUT = "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/valga.gpkg|layername=piir",
- JOIN_STYLE = 2,
- MITER_LIMIT = 2,
- OUTPUT = tmp_gpkg_file,
- SEGMENTS = 5
- # .quiet = TRUE
- )
- result
- piir_buffer <- sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT"))
- # st_geometry(pk) %>% plot()
- # st_geometry(piir_buffer) %>% plot(add=T, col = 'red')
- j <- 1
- for (j in 1:nrow(andmed.df)) {
- output_layer_name <- as.character(andmed.df$table[j])
- input <- sprintf(
- '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)',
- conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
- andmed.df$schema[j], andmed.df$table[j]
- )
- # ruut::qgis_algorithm_search_by_word(str = "extract")
- algorithm <- "native:extractbylocation"
- # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- result <- qgisprocess::qgis_run_algorithm(
- algorithm = algorithm,
- INPUT = input,
- INTERSECT = piir_buffer,
- OUTPUT = tmp_gpkg_file,
- PREDICATE = c(0, 1)
- # .quiet = TRUE
- )
- result
- # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
- system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
- }
- ## ----------------- piiriga lõigatud polügoonid -------------------
- ## --------------------------- algandmed ---------------------------
- andmed.df <- data.frame("schema" = character(0), "table" = character(0))
- andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
- andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
- j <- 1
- for (j in 1:nrow(andmed.df)) {
- output_layer_name <- as.character(andmed.df$table[j])
- input <- sprintf(
- '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)',
- conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
- andmed.df$schema[j], andmed.df$table[j]
- )
- # ruut::qgis_algorithm_search_by_word(str = "intersect")
- algorithm <- "native:intersection"
- # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- result <- qgisprocess::qgis_run_algorithm(
- algorithm = algorithm,
- INPUT = input,
- INPUT_FIELDS = "",
- OVERLAY = sprintf(
- "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/%s.gpkg|layername=piir",
- obj
- ),
- OVERLAY_FIELDS = "",
- OVERLAY_FIELDS_PREFIX = "",
- OUTPUT = tmp_gpkg_file
- # .quiet = TRUE
- )
- result
- # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
- system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
- }
- # ## ------------ andmed, mis jäävad piirikasti sisse ----------------
- # ## ------------------------- algandmed -----------------------------
- # andmed.df <- data.frame("schema" = character(0), "table" = character(0))
- # # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
- # # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
- # # andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
- # # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed"))
- # # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
- # # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
- # j <- 1
- # for (j in 1:nrow(andmed.df)) {
- # output_layer_name <- andmed.df$table[j]
- # input <- sprintf(
- # '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)',
- # conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
- # andmed.df$schema[j], andmed.df$table[j]
- # )
- # # ruut::qgis_algorithm_search_by_word(str = "extract")
- # algorithm <- "native:extractbylocation"
- # # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- # result <- qgisprocess::qgis_run_algorithm(
- # algorithm = algorithm,
- # INPUT = input,
- # INTERSECT = sprintf("%s|layername=%s", dsn, "boundarybox_3301"),
- # OUTPUT = tmp_gpkg_file,
- # PREDICATE = c(0, 1)
- # # .quiet = TRUE
- # )
- # result
- # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
- # system(sprintf("ogr2ogr -f GPKG -overwrite %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
- # }
- ## ---------------------- vaata layer'id ----------------------
- # Vaata layer'eid
- sf::st_layers(dsn = dsn)
- }
- # piirkonnale_hoonete_lisamine(obj = NULL, pk = NULL, gpkg_home = "/tmp")
|