copy_qgis_object_to_db.R 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #' QGIS objekti kopeerimine andmebaasi
  2. #'
  3. #' Funktsioon kopeerib QGIS objekti ('qgis_object') postgis andmebaasi. Andmebaasi ja konfiguratsiooni muutmiseks muuda konfiguratsiooni muutujat \code{conf}. Vaata \code{\link[ruut]{get_config}}.
  4. #' @param x object of 'qgis_result' spatial object.
  5. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  6. #' @param id A specified feature id will be processed. Unique field database. Default value 'fid'.
  7. #' @param crs_source CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.
  8. #' @param crs_target CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.
  9. #' @param geometry_type str Force a geometry type for new layer. One of: NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, or MULTILINESTRING, or PROMOTE_TO_MULTI or CONVERT_TO_LINEAR. Add "25D" for 3D layers.
  10. #' @return No output.
  11. #' @seealso [ruut::db_create_new_schema()], [ruut::get_config()], \url{https://gdal.org/programs/ogr2ogr.html}
  12. #' @keywords postgis, QGIS object
  13. #' @export
  14. #' @examples
  15. #' \dontrun{
  16. #'
  17. #' copy_qgis_object_to_db()
  18. #'
  19. #' }
  20. copy_qgis_object_to_db <- function(x = NULL, conf = NULL, id = "fid",
  21. crs_source = "EPSG:4326",
  22. crs_target = "EPSG:4326",
  23. geometry_type = "PROMOTE_TO_MULTI") {
  24. # Command 'ogr2ogr' help
  25. # system("ogr2ogr --long-usage")
  26. if (!is.null(x) && class(x) == "qgis_result") {
  27. cmd <- sprintf(
  28. paste0(
  29. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  30. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  31. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  32. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  33. "-s_srs %s -t_srs %s -nlt %s"
  34. ),
  35. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode,
  36. conf$schema, x, id, conf$schema, conf$table, crs_source, crs_target,
  37. geometry_type
  38. )
  39. ruut::db_create_new_schema(conf)
  40. Sys.sleep(1)
  41. system(cmd)
  42. } else {
  43. NULL
  44. }
  45. }