copy_polygon_to_db.R 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #' Polügoni kopeerimine andmebaasi
  2. #'
  3. #' Funktsioon kopeerib sf polügooni postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat config <- ruut::get_config().
  4. #' @param x object of "qgis_result" spatial polygon.
  5. #' @param conf list database configuration. Default get_config()
  6. #' @param id field andmebaasi unikaalne id veerg. Default '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. #' @keywords postgis, polygon
  10. #' @export
  11. #' @examples
  12. #' ## Not run:
  13. #' ##
  14. #' ## copy_polygon_to_db()
  15. #' ##
  16. #' ## End(**Not run**)
  17. copy_polygon_to_db <- function(x = NULL, conf = NULL, id = "fid",
  18. crs_source = "EPSG:4326",
  19. crs_target = "EPSG:4326") {
  20. if (!is.null(x) && class(x) == "qgis_result") {
  21. cmd <- sprintf(
  22. paste0(
  23. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  24. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  25. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt POLYGON ",
  26. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  27. "-s_srs %s -t_srs %s -nlt POLYGON"
  28. ),
  29. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode,
  30. conf$schema, x, id, conf$schema, conf$table, crs_source, crs_target
  31. )
  32. source(paste0(getwd(), "/R/db_create_new_schema.R"))
  33. # Create new schema if not exist.
  34. db_create_new_schema(conf)
  35. system(cmd)
  36. } else {
  37. NULL
  38. }
  39. }