copy_shp_to_db.R 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #' ESRI Shapefile kopeerimine andmebaasi
  2. #'
  3. #' Funktsioon kopeerib ESRI Shapefile objekti postgis andmebaasi. Andmebaasi ja konfiguratsiooni muutmiseks muuda konfiguratsiooni muutujat \code{conf}. Vaata \code{\link[ruut]{get_config}}.
  4. #' @param dir The directory where the shapefiles are located.
  5. #' @param layer str Layer to use.
  6. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  7. #' @param id A specified feature id will be processed. Unique field database. Default value 'fid'.
  8. #' @param crs_source CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.
  9. #' @param crs_target CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.
  10. #' @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.
  11. #' @return No output.
  12. #' @seealso [ruut::db_create_new_schema()], [ruut::get_config()]
  13. #' @keywords postgis, ESRI Shapefile
  14. #' @export
  15. #' @examples
  16. #' \dontrun{
  17. #'
  18. #' copy_qgis_object_to_db()
  19. #'
  20. #' }
  21. copy_shp_to_db <- function(dir = NULL, layer = NULL, conf = NULL, id = "fid",
  22. crs_source = "EPSG:4326",
  23. crs_target = "EPSG:4326",
  24. geometry_type = "PROMOTE_TO_MULTI") {
  25. # Command 'ogr2ogr' help
  26. # system("ogr2ogr --long-usage")
  27. # Kontrollime kas etteantud kataloogis paiknevad failid
  28. if (length(list.files(path = dir, pattern = sprintf("%s*", layer))) > 0) {
  29. source <- sprintf('"%s" "%s"', dir, layer)
  30. cmd <- sprintf(
  31. paste0(
  32. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  33. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  34. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  35. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  36. "-s_srs %s -t_srs %s -nlt %s"
  37. ),
  38. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  39. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  40. crs_source, crs_target, geometry_type
  41. )
  42. ruut::db_create_new_schema(conf)
  43. Sys.sleep(1)
  44. system(cmd)
  45. } else {
  46. cat("\nEi leitud antud kataloogist faile.")
  47. return(NULL)
  48. }
  49. }