| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #' ESRI Shapefile kopeerimine andmebaasi
- #'
- #' Funktsioon kopeerib ESRI Shapefile objeketi postgis andmebaasi. Andmebaasi parameetri muutmiseks muuda konfiguratsiooni muutujat conf <- ruut::get_conf().
- #' @param dir path directory of the shape files.
- #' @param layer str layer name.
- #' @param conf list database confuration. Default get_conf()
- #' @param id field andmebaasi unikaalne id veerg. Default 'fid'
- #' @param crs_source CRS source CRS for example 'EPSG:3301' (Estonia). Default is 'EPSG:4326'.
- #' @param crs_target CRS target CRS for example 'EPSG:4326'. Default is 'EPSG:4326'.
- #' @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.
- #' @keywords postgis, ESRI Shapefile
- #' @export
- #' @examples
- #' ## Not run:
- #' ##
- #' ## copy_qgis_object_to_db()
- #' ##
- #' ## End(**Not run**)
- copy_shp_to_db <- function(dir = NULL, layer = NULL, conf = NULL, id = "fid",
- crs_source = "EPSG:4326",
- crs_target = "EPSG:4326",
- geometry_type = "PROMOTE_TO_MULTI") {
- # Command 'ogr2ogr' help
- # system("ogr2ogr --long-usage")
- # Kontrollime kas etteantud kataloogis paiknevad failid
- if (length(list.files(path = dir, pattern = sprintf("%s*", layer))) > 0) {
- source <- sprintf('"%s" "%s"', dir, layer)
- cmd <- sprintf(
- paste0(
- "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
- "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
- "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
- "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
- "-s_srs %s -t_srs %s -nlt %s"
- ),
- conf$dbname, conf$host, conf$port, conf$user, conf$password,
- conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
- crs_source, crs_target, geometry_type
- )
- ruut::db_create_new_schema(conf)
- Sys.sleep(1)
- system(cmd)
- } else {
- cat("\nEi leitud antud kataloogist faile.")
- return(NULL)
- }
- }
|