gpkg_sellest_alustame_gpkg_loomist.R 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #' Uue GPKG loomine
  2. #'
  3. #' 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.
  4. #'
  5. #' @importFrom magrittr %>%
  6. #' @importFrom rlang .data
  7. #' @param pk Piirkonna geomeetriline joon.
  8. #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
  9. #' @param gpkg_home path Salvestatavate GPKG faili asukoht.
  10. #' @return GPKG andmebaasi loomine koos kihtidega 'piir' ja 'bb' (boundary box, piirikast).
  11. #' @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()]
  12. #' @keywords GPKG, boundary box, EPSG:3301
  13. #' @export
  14. #' @examples
  15. #' \dontrun{
  16. #'
  17. #' # read geojson from string:
  18. #' geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658300,6474800],
  19. #' [658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],
  20. #' [658300,6474800]]}')
  21. #' x <- sf::read_sf(geojson_txt) %>% sf::st_cast("POLYGON")
  22. #' sf::st_crs(x) <- 3301
  23. #' x
  24. #' sf::st_geometry(x) %>% graphics::plot()
  25. #'
  26. #' gpkg_home <- "/tmp"
  27. #' obj <- "marja"
  28. #'
  29. #' gpkg_sellest_alustame_gpkg_loomist(pk = x, obj = obj, gpkg_home = gpkg_home)
  30. #' # Layers list.
  31. #' dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  32. #' sf::st_layers(dsn = dsn)
  33. #' bb <- sf::read_sf(dsn = dsn, layer = "bb")
  34. #' pk <- sf::read_sf(dsn = dsn, layer = "piir")
  35. #' sf::st_geometry(bb) %>% graphics::plot(border = 3, lwd = 0.3, col = "#d3fffb")
  36. #' sf::st_geometry(pk) %>% graphics::plot(add = T, border = 3, lwd = 0.3, col = "#a3fffb")
  37. #' }
  38. gpkg_sellest_alustame_gpkg_loomist <- function(pk = NULL, obj = NULL, gpkg_home = "/tmp") {
  39. ## Konfiguratsiooni muutujale väärtuste omistamine
  40. conf <- ruut::get_config()
  41. conf$gpkg_home <- gpkg_home
  42. conf$gpkg_file <- obj
  43. tmp_gpkg_file <- tempfile(fileext = ".gpkg")
  44. tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  45. tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  46. ## ----------- argumentide vastavuse kontroll -----------
  47. if (is.null(pk) || !sf::st_is_valid(pk)) {
  48. cat("\nPalun kontrolli geomeetrilise kujundi \u00F%igsust.\n")
  49. return(NULL)
  50. }
  51. if (is.null(obj)) {
  52. cat("\nPalun sisesta objekti nimi.\n")
  53. return(NULL)
  54. }
  55. if (!dir.exists(gpkg_home)) {
  56. dir.create(gpkg_home)
  57. cat(sprintf("\nLoodi kataloog %s.\n", gpkg_home))
  58. }
  59. ## ------------- muutujad ja teisendused ---------------
  60. obj <- gsub(" ", "_", tolower(obj))
  61. dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  62. ## ------------------- programmi osa -----------------
  63. ## 1. Piirjoone salvestamine
  64. pk <- sf::st_transform(pk, 3301)
  65. # write to gpkg
  66. sf::write_sf(pk, dsn = dsn, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T) # append = FALSE, delete_layer = T
  67. ## 2. Piirikasti salvestamine
  68. ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
  69. pk_attributes <- attributes(pk$geometry)
  70. 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)
  71. hspacing <- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
  72. vspacing <- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
  73. conf$gpkg_table <- "bb"
  74. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  75. # ruut::qgis_algorithm_search_by_word(str = "grid")
  76. algorithm <- "native:creategrid"
  77. # cat(ruut::qgis_show_help(algorithm = algorithm))
  78. 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)
  79. cat(sprintf("\n%s\n", cmd))
  80. system(cmd)
  81. ## 3. epk2t piirikasti konstrueerimine ja salvestamine s.t kuhu mahuvad sisse
  82. ## meie piirkonna epk2t (1x1km) ruudud.
  83. ## 3.1 Leiame piirikastiga seotud epk2t ruudud
  84. 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] }"
  85. # ruut::qgis_algorithm_search_by_word(str = "extract")
  86. algorithm <- "native:extractbylocation"
  87. # cat(ruut::qgis_show_help(algorithm = algorithm))
  88. conf$table <- "epk2t_grid"
  89. conf$schema <- "maaamet"
  90. input <- ruut::construct_to_gpkg_output_postgres_str(
  91. conf = conf, geometry_type = "Polygon", srid = 3301,
  92. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  93. )
  94. conf$gpkg_table <- "bb"
  95. intersect <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = TRUE)
  96. str <- sprintf("{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }", input, intersect, tmp_gpkg_file_output)
  97. cmd <- ruut::construct_qgis_output_result_to_better_format(str, algorithm)
  98. system(cmd)
  99. test_layer_obj <- sf::read_sf(dsn = dsn, layer = "test_layer")
  100. pk_attributes <- attributes(test_layer_obj$geom)
  101. 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)
  102. hspacing <- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
  103. vspacing <- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
  104. conf$gpkg_table <- "bb2"
  105. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  106. # ruut::qgis_algorithm_search_by_word(str = "grid")
  107. algorithm <- "native:creategrid"
  108. # cat(ruut::qgis_show_help(algorithm = algorithm))
  109. 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)
  110. cat(sprintf("\n%s\n", cmd))
  111. system(cmd)
  112. # Layers list
  113. sf::st_layers(dsn = dsn)
  114. }