copy_qgis_object_to_db.R 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #' QGIS objekti kopeerimine andmebaasi
  2. #'
  3. #' Funktsioon kopeerib QGIS objeketi postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat config <- ruut::get_config().
  4. #' @param x object of "qgis_result" spatial object.
  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. #' @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. #' @keywords postgis, QGIS object
  11. #' @export
  12. #' @examples
  13. #' ## Not run:
  14. #' ##
  15. #' ## copy_qgis_object_to_db()
  16. #' ##
  17. #' ## End(**Not run**)
  18. copy_qgis_object_to_db <- function(x = NULL, conf = NULL, id = "fid",
  19. crs_source = "EPSG:4326",
  20. crs_target = "EPSG:4326",
  21. geometry_type = "PROMOTE_TO_MULTI") {
  22. # Command 'ogr2ogr' help
  23. # system("ogr2ogr --long-usage")
  24. if (!is.null(x) && class(x) == "qgis_result") {
  25. cmd <- sprintf(
  26. paste0(
  27. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  28. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  29. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  30. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  31. "-s_srs %s -t_srs %s -nlt %s"
  32. ),
  33. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode,
  34. conf$schema, x, id, conf$schema, conf$table, crs_source, crs_target,
  35. geometry_type
  36. )
  37. ruut::db_create_new_schema(conf)
  38. Sys.sleep(1)
  39. system(cmd)
  40. } else {
  41. NULL
  42. }
  43. }