construct_to_gpkg_output_postgres_str.R 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #' 'qgisprocess' funktsiooonides GPKG INPUT/OUTPUT konstrueerimine Postgresql salvestamiseks
  2. #'
  3. #' See funktsioon konstrueerib 'qgisprocess' funktsioonides enamasti INPUT/OUTPUT/OVERLAY parameetri argumentide kasutatava fraasi, mis on vajalik postgresql andmebaasiga ühendamisegs.
  4. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  5. #' @param geometry_field str A geometri field name. Default: "geom".
  6. #' @param geometry_type str Select: 'Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection','PolygonWithHole','Collection'.
  7. #' @param srid CRS Default: 3301. Other: 4326.
  8. #' @param checkPrimaryKeyUnicity TRUE/FALSE.
  9. #' @param key str A primary key ID, usally 'id'.
  10. #' @seealso [ruut::db_connect()], [ruut::get_config()], [ruut::construct_ogr2ogr_PG_connect_str()], [ruut::construct_qgis_output_result_to_beter_format()],[ruut::construct_to_gpkg_output_file_str()]
  11. #' @return A string "postgres://dbname='%s' host=%s port=%s user='%s' password='%s' sslmode=%s key='id' srid=3301 checkPrimaryKeyUnicity='1' table=\"%s\".\"%s\" (geom)".
  12. #' @seealso [ruut::construct_ogr2ogr_PG_connect_str()], [ruut::construct_qgis_output_result_to_beter_format()]
  13. #' @keywords qgis_process
  14. #' @export
  15. #' @examples
  16. #' \dontrun{
  17. #'
  18. #'
  19. #' conf <- ruut::get_config()
  20. #' conf$schema <- "maaamet"
  21. #' conf$table <- "asustusyksus"
  22. #' input <- ruut::construct_to_gpkg_output_postgres_str(
  23. #' conf = conf, geometry_type = "Polygon", srid = 3301,
  24. #' checkPrimaryKeyUnicity = TRUE, key = "id"
  25. #' )
  26. #' conf$schema <- "data"
  27. #' conf$table <- "test"
  28. #' output <- ruut::construct_to_gpkg_output_postgres_str(
  29. #' conf = conf, geometry_type = NULL, srid = NULL,
  30. #' checkPrimaryKeyUnicity = FALSE, key = NULL
  31. #' )
  32. #'
  33. #' str <- paste0("{ 'DISSOLVE' : False, 'DISTANCE' : 100, 'END_CAP_STYLE' : 2,
  34. #' 'INPUT' : '", input, "', 'JOIN_STYLE' : 1, 'MITER_LIMIT' : 2,
  35. #' 'OUTPUT' : '", output, "', 'SEGMENTS' : 5 }")
  36. #' algorithm <- "native:buffer"
  37. #' cmd <- ruut::construct_qgis_output_result_to_beter_format(str = str, algorithm = algorithm)
  38. #' system(cmd)
  39. #' }
  40. construct_to_gpkg_output_postgres_str <- function(conf = NULL, geometry_field = "geom", geometry_type = NULL, srid = NULL, checkPrimaryKeyUnicity = FALSE, key = NULL) {
  41. if (is.null(conf)) conf <- ruut::get_config()
  42. if (is.null(geometry_type)) geometry_type <- "" else geometry_type <- sprintf("type=%s", geometry_type)
  43. if (is.null(srid)) srid <- "" else srid <- sprintf("srid=%s", srid)
  44. if (checkPrimaryKeyUnicity) checkPrimaryKeyUnicity <- "checkPrimaryKeyUnicity='1'" else checkPrimaryKeyUnicity <- ""
  45. if (is.null(key)) key <- "" else key <- sprintf("key='%s'", key)
  46. PG <- sprintf(
  47. "postgres://dbname='%s' host=%s port=%s user='%s' password='%s' sslmode=%s %s %s %s %s table=\"%s\".\"%s\" (%s)",
  48. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode, key, srid, geometry_type, checkPrimaryKeyUnicity, conf$schema, conf$table, geometry_field
  49. )
  50. PG
  51. }