|
|
@@ -0,0 +1,157 @@
|
|
|
+#' Piirkonnale andmebaasides olevate polügoonide 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_ruudustike_lisamine()] ,[ruut::pk_polygoonide_lisamine()],[ruut::pk_sellest_alustame_db_loomist()],[ruut::gpkg_piirkonnale_joonte_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_teisendame_polygoone()],[ruut::gpkg_teisendame_jooni()],[ruut::gpkg_teisendame_punkte()]
|
|
|
+#' @keywords postgis, boundary box, EPSG:3301
|
|
|
+#' @export
|
|
|
+#' @examples
|
|
|
+#' \dontrun{
|
|
|
+#'
|
|
|
+#' obj <- "marja"
|
|
|
+#' pk_polygoonide_lisamine(obj = obj, conf = NULL)
|
|
|
+#'
|
|
|
+#' # Layers list.
|
|
|
+#' ruut::db_schema_tablenames(conf = conf)
|
|
|
+#' }
|
|
|
+pk_polygoonide_lisamine <- 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(ruut::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" = "osm_shp", "table" = "landuse_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "osm_shp", "table" = "natural_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a_dissolved"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a_dissolved"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a_dissolved"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_ka"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_302_ou_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_303_haritav_maa_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_304_lage_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a_dissolved"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_a_dissolved"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_ka"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_307_turbavali_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_401_hoone_ka"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_ka"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_404_maaalune_hoone_ka"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_501_tee_a"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_ka"))
|
|
|
+ andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "mullakaart"))
|
|
|
+ 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)) {
|
|
|
+ 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 = "Polygon", 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)
|
|
|
+ ## Filtreerime maakasutuse kihi 'landuse-a' eraldi alamkihtideks
|
|
|
+ if (andmed$table[i] == "landuse_a") {
|
|
|
+ landuse_a <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("code", "fclass")])
|
|
|
+ parent_table <- as.character(andmed$table[i])
|
|
|
+ for (k in 1:nrow(landuse_a)) {
|
|
|
+ table_suffix <- landuse_a$fclass[k]
|
|
|
+ conf$table <- parent_table
|
|
|
+ conf$schema <- obj
|
|
|
+ input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", 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 : 'code', OPERATOR : 0, VALUE : '", landuse_a$code[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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ## Filtreerime hoonete kihi 'landuse-a' eraldi tüübi järele alamkihtideks
|
|
|
+ if (andmed$table[i] == "e_401_hoone_ka") {
|
|
|
+ alamobjektid <- unique(as.data.frame(sf::st_read(dsn = conn, layer = c(conf$schema, as.character(andmed$table[i])), as_tibble = T))[, c("kood", "tyyp")])
|
|
|
+ parent_table <- as.character(andmed$table[i])
|
|
|
+ for (k in 1:nrow(alamobjektid)) {
|
|
|
+ table_suffix <- alamobjektid$tyyp[k]
|
|
|
+ conf$table <- parent_table
|
|
|
+ conf$schema <- obj
|
|
|
+ input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", 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 : 'tyyp', OPERATOR : 0, VALUE : '", alamobjektid$tyyp[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)
|
|
|
+ ## ----------- lisame tsentroidid -----------
|
|
|
+ conf$table <- sprintf("%s_%s", parent_table, table_suffix)
|
|
|
+ conf$schema <- obj
|
|
|
+ input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
|
|
|
+ conf$table <- sprintf("%s_%s_p", 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 <- sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
|
|
|
+ # ruut::qgis_algorithm_search_by_word("centroid")
|
|
|
+ algorithm <- "native:centroids"
|
|
|
+ # ruut::qgis_show_help(algorithm = algorithm)
|
|
|
+ cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
+ cat(sprintf("\n%s\n", cmd))
|
|
|
+ system(cmd)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ## Layers list
|
|
|
+ ruut::db_schema_tablenames(conf = conf)
|
|
|
+ sf::st_layers(dsn = tmp_gpkg_file)
|
|
|
+}
|