| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #' Uue GPKG loomine
- #'
- #' Funktsioon loob geopaketi GPKG faili. Tegemist on sqlite andmebaasiga. Samas lisatakse faili piir ja piiri piirkast. Ülejäänud funktsioonid kasutavad selle funktsiooni poolt loodud objekti nime ja piiri ega piirikasti pole vaja enam teiste funktsioonide rakendamisel lisada.
- #'
- #' @importFrom magrittr %>%
- #' @importFrom rlang .data
- #' @param pk Piirkonna geomeetriline joon.
- #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
- #' @param gpkg_home path Salvestatavate GPKG faili asukoht.
- #' @return GPKG andmebaasi loomine koos kihtidega 'piir' ja 'bb' (boundary box, piirikast).
- #' @seealso [sf::st_read()], [sf::write_sf()],[sf::st_transform()],[ruut::gpkg_piirkonnale_ruudustike_lisamine()] ,[ruut::gpkg_piirkonnale_polygoonide_lisamine()],[ruut::gpkg_sellest_alustame_gpkg_loomist()],[ruut::gpkg_piirkonnale_joonte_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()]
- #' @keywords GPKG, boundary box, EPSG:3301
- #' @export
- #' @examples
- #' \dontrun{
- #'
- #' # read geojson from string:
- #' geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658300,6474800],
- #' [658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],
- #' [658300,6474800]]}')
- #' x <- sf::read_sf(geojson_txt) %>% sf::st_cast("POLYGON")
- #' sf::st_crs(x) <- 3301
- #' x
- #' sf::st_geometry(x) %>% graphics::plot()
- #'
- #' gpkg_home <- "/tmp"
- #' obj <- "marja"
- #'
- #' gpkg_sellest_alustame_gpkg_loomist(pk = x, obj = obj, gpkg_home = gpkg_home)
- #' # Layers list.
- #' dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
- #' sf::st_layers(dsn = dsn)
- #' bb <- sf::read_sf(dsn = dsn, layer = "bb")
- #' pk <- sf::read_sf(dsn = dsn, layer = "piir")
- #' sf::st_geometry(bb) %>% graphics::plot(border = 3, lwd = 0.3, col = "#d3fffb")
- #' sf::st_geometry(pk) %>% graphics::plot(add = T, border = 3, lwd = 0.3, col = "#a3fffb")
- #' }
- gpkg_sellest_alustame_gpkg_loomist <- function(pk = NULL, obj = NULL, gpkg_home = "/tmp") {
- ## Konfiguratsiooni muutujale väärtuste omistamine
- conf <- ruut::get_config()
- conf$gpkg_home <- gpkg_home
- conf$gpkg_file <- obj
- tmp_gpkg_file <- tempfile(fileext = ".gpkg")
- 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)
- ## ----------- argumentide vastavuse kontroll -----------
- if (is.null(pk) || !sf::st_is_valid(pk)) {
- cat("\nPalun kontrolli geomeetrilise kujundi \u00F%igsust.\n")
- return(NULL)
- }
- if (is.null(obj)) {
- cat("\nPalun sisesta objekti nimi.\n")
- return(NULL)
- }
- if (!dir.exists(gpkg_home)) {
- dir.create(gpkg_home)
- cat(sprintf("\nLoodi kataloog %s.\n", gpkg_home))
- }
- ## ------------- muutujad ja teisendused ---------------
- obj <- gsub(" ", "_", tolower(obj))
- dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
- ## ------------------- programmi osa -----------------
- ## 1. Piirjoone salvestamine
- pk <- sf::st_transform(pk, 3301)
- # write to gpkg
- sf::write_sf(pk, dsn = dsn, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T) # append = FALSE, delete_layer = T
- ## 2. Piirikasti salvestamine
- ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
- pk_attributes <- attributes(pk$geometry)
- extent <- sprintf("%s,%s,%s,%s [EPSG:3301]", round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["xmax"] / 100)) * 100, round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["ymax"] / 100)) * 100)
- hspacing <- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
- vspacing <- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
- conf$gpkg_table <- "bb"
- output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
- # ruut::qgis_algorithm_search_by_word(str = "grid")
- algorithm <- "native:creategrid"
- # cat(ruut::qgis_show_help(algorithm = algorithm))
- cmd <- sprintf("qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"", algorithm, extent, hspacing, vspacing, output)
- cat(sprintf("\n%s\n", cmd))
- system(cmd)
- ## 3. epk2t piirikasti konstrueerimine ja salvestamine s.t kuhu mahuvad sisse
- ## meie piirkonna epk2t (1x1km) ruudud.
- ## 3.1 Leiame piirikastiga seotud epk2t ruudud
- str <- "{ 'INPUT' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable authcfg=0i30k14 key=\'id\' srid=3301 type=MultiPolygon checkPrimaryKeyUnicity=\'1\' table=\"maaamet\".\"epk2t_grid\" (geom)', 'INTERSECT' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/marja.gpkg|layername=bb', 'OUTPUT' : 'TEMPORARY_OUTPUT', 'PREDICATE' : [5,6] }"
- # ruut::qgis_algorithm_search_by_word(str = "extract")
- algorithm <- "native:extractbylocation"
- # cat(ruut::qgis_show_help(algorithm = algorithm))
- conf$table <- "epk2t_grid"
- conf$schema <- "maaamet"
- input <- ruut::construct_to_gpkg_output_postgres_str(
- conf = conf, geometry_type = "Polygon", srid = 3301,
- checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
- )
- conf$gpkg_table <- "bb"
- intersect <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
- str <- sprintf("{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }", input, intersect, tmp_gpkg_file_output)
- cmd <- ruut::construct_qgis_output_result_to_better_format(str, algorithm)
- system(cmd)
- test_layer_obj <- sf::read_sf(dsn = dsn, layer = "test_layer")
- pk_attributes <- attributes(test_layer_obj$geom)
- extent <- sprintf("%s,%s,%s,%s [EPSG:3301]", round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["xmax"] / 100)) * 100, round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["ymax"] / 100)) * 100)
- hspacing <- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
- vspacing <- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
- conf$gpkg_table <- "bb2"
- output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
- # ruut::qgis_algorithm_search_by_word(str = "grid")
- algorithm <- "native:creategrid"
- # cat(ruut::qgis_show_help(algorithm = algorithm))
- cmd <- sprintf("qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"", algorithm, extent, hspacing, vspacing, output)
- cat(sprintf("\n%s\n", cmd))
- system(cmd)
- # Layers list
- sf::st_layers(dsn = dsn)
- }
|