copy_gpkg_to_db.R 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #' Export GPKG to PostgreSQL
  2. #'
  3. #' Exports a gpkg file layer into a PostgreSQL database.
  4. #'
  5. #' gtype: Output geometry type 'enum':. Acceptable values: Number of selected option, e.g. '1' or Comma separated list of options, e.g. c(1,3). See more: \url{https://gdal.org/drivers/vector/pg.html}.
  6. #' - 0:
  7. #' - 1: NONE
  8. #' - 2: GEOMETRY
  9. #' - 3: POINT
  10. #' - 4: LINESTRING
  11. #' - 5: POLYGON
  12. #' - 6: GEOMETRYCOLLECTION
  13. #' - 7: MULTIPOINT
  14. #' - 8: MULTIPOLYGON
  15. #' - 9: MULTILINESTRING
  16. #'
  17. #' @param gpkg Gpkg file path.
  18. #' @param layer A gpkg file layer [sf::st_layers()].
  19. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  20. #' @return No output.
  21. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()], [ruut::copy_qgis_object_to_db()]
  22. #' @keywords postgis, gpkg
  23. #' @export
  24. #' @examples
  25. #' \dontrun{
  26. #'
  27. #' conf <- ruut::get_config()
  28. #' gpkg_file <- "/home/ardo/aaa/data/gpkg/grids/estonian_grids.gpkg"
  29. #' # Vaata layer'eid
  30. #' sf::st_layers(gpkg_file)
  31. #' # Vali layer
  32. #' layer <- "epk200t_grid"
  33. #' conf$table <- "aaa_1"
  34. #' conf$schema <- "data"
  35. #' copy_gpkg_to_db(gpkg = gpkg_file, layer = layer, conf = conf)
  36. #' copy_gpkg_to_db(gpkg = gpkg_file, conf = conf) # viga
  37. #' }
  38. copy_gpkg_to_db <- function(gpkg = NULL, layer = NULL, conf = NULL) {
  39. if (is.null(gpkg)) {
  40. return(cat("\ngpkg fail puudu.\n"))
  41. }
  42. if (is.null(layer)) {
  43. return(cat("\ngpkg layer puudu.\n"))
  44. }
  45. if (is.null(conf)) conf <- ruut::get_config()
  46. layer <- sprintf("|layername=%s", layer)
  47. input <- sprintf("%s%s", gpkg, layer)
  48. # obj - objekti nimetus (näiteks: valga)
  49. # ruut::qgis_algorithm_search_by_word(str = "importinto")
  50. algorithm <- "qgis:importintopostgis"
  51. # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
  52. # qgisprocess::qgis_outputs(algorithm = algorithm)
  53. ruut::db_create_new_schema(conf)
  54. result <- qgisprocess::qgis_run_algorithm(
  55. algorithm = algorithm,
  56. CREATEINDEX = 1,
  57. DATABASE = tools::toTitleCase(tolower(conf$dbname)), # millegipärast peab algama suure tähega
  58. DROP_STRING_LENGTH = 0,
  59. ENCODING = conf$encoding,
  60. FORCE_SINGLEPART = 0,
  61. GEOMETRY_COLUMN = conf$geometry_column,
  62. INPUT = input,
  63. LOWERCASE_NAMES = 1,
  64. OVERWRITE = 1,
  65. PRIMARY_KEY = conf$primary_key,
  66. SCHEMA = conf$schema,
  67. TABLENAME = conf$table,
  68. )
  69. # result
  70. }