Kaynağa Gözat

Lisatud 'copy_gpkg_to_db'.

Ardo Kubjas 5 yıl önce
ebeveyn
işleme
d193b59d0b
3 değiştirilmiş dosya ile 126 ekleme ve 0 silme
  1. 1 0
      NAMESPACE
  2. 69 0
      R/copy_gpkg_to_db.R
  3. 56 0
      man/copy_gpkg_to_db.Rd

+ 1 - 0
NAMESPACE

@@ -3,6 +3,7 @@
 export(cat_function)
 export(construct_ogr2ogr_PG_connect_str)
 export(convert_sf2qgis)
+export(copy_gpkg_to_db)
 export(copy_qgis_object_to_db)
 export(copy_shp_to_db)
 export(db_all_schemas)

+ 69 - 0
R/copy_gpkg_to_db.R

@@ -0,0 +1,69 @@
+#' Export GPKG to PostgreSQL
+#'
+#' Exports a gpkg file layer into a PostgreSQL database.
+#'
+#' 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}.
+#'   - 0:
+#'   - 1: NONE
+#'   - 2: GEOMETRY
+#'   - 3: POINT
+#'   - 4: LINESTRING
+#'   - 5: POLYGON
+#'   - 6: GEOMETRYCOLLECTION
+#'   - 7: MULTIPOINT
+#'   - 8: MULTIPOLYGON
+#'   - 9: MULTILINESTRING
+#'
+#' @param gpkg Gpkg file path.
+#' @param layer A gpkg file layer [sf::st_layers()].
+#' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
+#' @return No output.
+#' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()], [ruut::copy_qgis_object_to_db()]
+#' @keywords postgis, gpkg
+#' @export
+#' @examples
+#' \dontrun{
+#'
+#' conf <- ruut::get_config()
+#' gpkg_file <- "/home/ardo/aaa/data/gpkg/grids/estonian_grids.gpkg"
+#' # Vaata layer'eid
+#' sf::st_layers(gpkg_file)
+#' # Vali layer
+#' layer <- "epk200t_grid"
+#' conf$table <- "aaa_1"
+#' conf$schema <- "data"
+#' copy_gpkg_to_db(gpkg = gpkg_file, layer = layer, conf = conf)
+#' copy_gpkg_to_db(gpkg = gpkg_file, conf = conf) # viga
+#' }
+copy_gpkg_to_db <- function(gpkg = NULL, layer = NULL, conf = NULL) {
+  if (is.null(gpkg)) {
+    return(cat("\ngpkg fail puudu.\n"))
+  }
+  if (is.null(layer)) {
+    return(cat("\ngpkg layer puudu.\n"))
+  }
+  if (is.null(conf)) conf <- ruut::get_config()
+  layer <- sprintf("|layername=%s", layer)
+  input <- sprintf("%s%s", gpkg, layer)
+  # obj - objekti nimetus (näiteks: valga)
+  # ruut::qgis_algorithm_search_by_word(str = "importinto")
+  algorithm <- "qgis:importintopostgis"
+  # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
+  # qgisprocess::qgis_outputs(algorithm = algorithm)
+  result <- qgisprocess::qgis_run_algorithm(
+    algorithm = algorithm,
+    CREATEINDEX = 1,
+    DATABASE = tools::toTitleCase(tolower(conf$dbname)), # millegipärast peab algama suure tähega
+    DROP_STRING_LENGTH = 0,
+    ENCODING = conf$encoding,
+    FORCE_SINGLEPART = 0,
+    GEOMETRY_COLUMN = conf$geometry_column,
+    INPUT = input,
+    LOWERCASE_NAMES = 1,
+    OVERWRITE = 1,
+    PRIMARY_KEY = conf$primary_key,
+    SCHEMA = conf$schema,
+    TABLENAME = conf$table,
+  )
+  # result
+}

+ 56 - 0
man/copy_gpkg_to_db.Rd

@@ -0,0 +1,56 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/copy_gpkg_to_db.R
+\name{copy_gpkg_to_db}
+\alias{copy_gpkg_to_db}
+\title{Export GPKG to PostgreSQL}
+\usage{
+copy_gpkg_to_db(gpkg = NULL, layer = NULL, conf = NULL)
+}
+\arguments{
+\item{gpkg}{Gpkg file path.}
+
+\item{layer}{A gpkg file layer \code{\link[sf:st_layers]{sf::st_layers()}}.}
+
+\item{conf}{A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.}
+}
+\value{
+No output.
+}
+\description{
+Exports a gpkg file layer into a PostgreSQL database.
+}
+\details{
+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}.
+\itemize{
+\item 0:
+\item 1: NONE
+\item 2: GEOMETRY
+\item 3: POINT
+\item 4: LINESTRING
+\item 5: POLYGON
+\item 6: GEOMETRYCOLLECTION
+\item 7: MULTIPOINT
+\item 8: MULTIPOLYGON
+\item 9: MULTILINESTRING
+}
+}
+\examples{
+\dontrun{
+
+conf <- ruut::get_config()
+gpkg_file <- "/home/ardo/aaa/data/gpkg/grids/estonian_grids.gpkg"
+# Vaata layer'eid
+sf::st_layers(gpkg_file)
+# Vali layer
+layer <- "epk200t_grid"
+conf$table <- "aaa_1"
+conf$schema <- "data"
+copy_gpkg_to_db(gpkg = gpkg_file, layer = layer, conf = conf)
+copy_gpkg_to_db(gpkg = gpkg_file, conf = conf) # viga
+}
+}
+\seealso{
+\code{\link[=get_config]{get_config()}}, \code{\link[=copy_shp_to_db]{copy_shp_to_db()}}, \code{\link[=copy_qgis_object_to_db]{copy_qgis_object_to_db()}}
+}
+\keyword{gpkg}
+\keyword{postgis,}