| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #' Piirkonnale andmebaasides olevate punktide 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_teisendame_polygoone()],[ruut::pk_teisendame_jooni()],[ruut::pk_teisendame_punkte()]
- #' @keywords postgis, boundary box, EPSG:3301
- #' @export
- #' @examples
- #' \dontrun{
- #'
- #' obj <- "marja"
- #' pk_lisame_punktid(obj = obj, conf = NULL)
- #'
- #' # Layers list.
- #' ruut::db_schema_tablenames(conf = conf)
- #' }
- pk_lisame_punktid <- function(obj = NULL, conf = NULL) {
- ## ------------- muutujad ja teisendused ---------------
- vars <- ajutised_muutujad(pk = NULL, obj, conf)
- obj <- vars$obj
- piir <- vars$pk
- conf <- vars$conf
- ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
- if (!any("a00_piir" %in% ruut::db_schema_tablenames(conf = conf))) {
- cat("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
- return(NULL)
- }
- ## Algorithm
- # ruut::qgis_algorithm_search_by_word(str = "extract")
- # algorithm <- "native:extractbylocation"
- # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
- ## -------------------- Loop -----------------------
- intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
- andmed <- data.frame("schema" = character(0), "table" = character(0))
- andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
- andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw"))
- andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_bussipeatus"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "kilomeetripostid"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_jaotus"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_kandur"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_mahasoit"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_onnetus"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_rdtyl"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristmik"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ristumispunkt"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_sild"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_teeosa_points"))
- andmed <- rbind(andmed, data.frame("schema" = "teeregister_wfs", "table" = "n_ylek"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_402_korgrajatis_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_602_tehnopaigaldis_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kirik_p"))
- andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "kohanimi"))
- 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 = "Point", srid = 3301,
- checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = geom
- )
- conf$table <- sprintf("%s_p_%s", strtrim(as.character(andmed$schema[i]), 2), 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")
- ## ------------------- QGIS: qgisprocess ----------------------
- str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', 'PREDICATE' : [0] }")
- algorithm <- "native:extractbylocation"
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- cat(sprintf("\n%s\n\n", cmd))
- system(cmd)
- ## Filtreerime aadressandmete kihi eraldi alamkihtideks
- if (andmed$table[i] == "aadressandmed") {
- parent_table <- sprintf("%s_p_%s", strtrim(as.character(andmed$schema[i]), 2), andmed$table[i])
- conn <- ruut::db_connect(conf = conf)
- aadressandmed <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, parent_table), as_tibble = T))[, c("adob_liik"), drop = FALSE])
- for (k in 1:nrow(aadressandmed)) {
- table_suffix <- aadressandmed$adob_liik[k]
- conf$table <- parent_table
- conf$schema <- obj
- input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "Point", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
- # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
- conf$table <- sprintf("%s_%s", parent_table, table_suffix)
- output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
- str <- paste0("{ 'INPUT' : '", input, "', 'INTERSECT' : '", intersect_layer, "', 'OUTPUT' : '", output, "', FIELD : 'adob_liik', OPERATOR : 0, VALUE : ", aadressandmed$adob_liik[k], " }")
- algorithm <- "native:extractbyattribute"
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- cat(sprintf("\n%s\n", cmd))
- system(cmd)
- }
- }
- }
- }
- ## Layers list
- conf$schema <- obj
- ruut::db_schema_tablenames(conf = conf)
- sf::st_layers(dsn = vars$tmp_gpkg_file)
- }
|