| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #' Piirkonnale andmebaasides olevate joonte lisamine
- #'
- #' Etteantud piirkonna geomeetrilise piirjoone ('piir') ja selle joone piirikasti ('bb') järele leitakse nende aladega kaetud polügoonid. Andmed salvestatakse postgis andmebaasi.
- #'
- #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
- #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
- #' @return Uute andmebaasi kihtide 'piir_...' ja 'bb_...' loomine.
- #' @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_lisame_piirkonnale_punktid()],[ruut::pk_teisendame_polygoone()],[ruut::pk_teisendame_jooni()],[ruut::pk_teisendame_punkte()]
- #' @keywords postgis, boundary box, EPSG:3301
- #' @export
- #' @examples
- #' \dontrun{
- #'
- #' obj <- "marja"
- #' pk_lisame_jooned(obj = obj, conf = NULL)
- #'
- #' # Layers list.
- #' ruut::db_schema_tablenames(conf = conf)
- #' }
- pk_lisame_jooned <- function(obj = NULL, conf = NULL) {
- obj <- gsub(" ", "_", tolower(obj))
- ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
- if (is.null(conf)) {
- conf <- ruut::get_config()
- conf$schema <- obj
- }
- if (!any("piir" %in% ruut::db_schema_tablenames(conf = conf))) {
- cat("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
- return(NULL)
- }
- ## ------------- muutujad ja teisendused ---------------
- conn <- ruut::db_connect(conf = conf)
- try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
- tmp_dir <- tempdir()
- tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
- # system(sprintf("touch %s", tmp_gpkg_file))
- conf$gpkg_home <- tmp_dir
- conf$gpkg_file <- obj
- conf$gpkg_table <- "test_layer"
- tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
- tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
- # write to gpkg
- sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
- # unlink(tmp_gpkg_file)
- ## Algorithm
- # ruut::qgis_algorithm_search_by_word(str = "extract")
- # algorithm <- "native:extractbylocation"
- # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- ## -------------------- Loop -----------------------
- intersect_layers <- c("bb2") # c("piir", "bb", "bb2")
- andmed <- data.frame("schema" = character(0), "table" = character(0))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_alusdokumendid_ja_lepingud"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "pohimaantee"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "korvalmaantee"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "tugimaantee"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "muutee"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "ramp"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "teeosa"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kergliiklustee"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_liiklussagedus"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_omand"))
- andmed <- rbind(andmed, data.frame("schema" = "gtfs", "table" = "shapes"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_j"))
- # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_j"))
- # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_405_piire_j"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_j"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_502_roobastee_j"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_j"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_601_elektriliin_j"))
- # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "korgus_j"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sild_j"))
- # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "sygavus_j"))
- for (intersect in intersect_layers) {
- conf$table <- intersect
- conf$schema <- obj
- intersect_layer <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
- for (i in 1:nrow(andmed)) {
- ## teeregister_wfs andmebaasis on importimisel geomeetria välja tähistus 'geometry'.
- if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
- conf$table <- andmed$table[i]
- conf$schema <- andmed$schema[i]
- input <- ruut::construct_to_gpkg_output_postgres_str(
- conf = conf, geometry_type = "Linestring", srid = 3301,
- checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
- )
- conf$table <- sprintf("%s", andmed$table[i])
- conf$schema <- obj
- output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = NULL, srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
- str <- paste0("{ 'INPUT' : '", input, "', 'INPUT_FIELDS' : [], 'OUTPUT' : '", output, "', 'OVERLAY' : '", intersect_layer, "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }")
- algorithm <- "native:intersection"
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- cat(sprintf("\n%s\n\n", cmd))
- system(cmd)
- }
- }
- ## Layers list
- conf$schema <- obj
- ruut::db_schema_tablenames(conf = conf)
- sf::st_layers(dsn = tmp_gpkg_file)
- }
|