| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #' PostgreSQL - Automatic ESRI shp file import into postgis table
- #' @details ESRI shape failide automaatne importimine postgis tabeliks. Eelnevalt peab olema loodud postgis andmebaas
- #' \code{\link{myPostgresCreateGisDb}} ja defineeritud tabeli nimi. Andmed salvestatakse WGS4326 projektsioonis.
- #' Kui parameeter 'shp' on määramata, siis tuleb *.shp fail valida failide kataloogist.
- #' @param host Name or the numeric IPaddress of the host to connect to.
- #' @param dbname The database name.
- #' @param user PostgreSQL user name to connect as. Defaults to be the same as the operating system name of the user running the application.
- #' @param password Password to be used if the server demands password authentication.
- #' @param port Port number to connect to at the server host.
- #' @param shpFile ESRI shp file.
- #' @param tbl Postgis table name.
- #' @return return message.
- #' @seealso \code{\link{myPostgresConnect}}, \code{\link{myPostgresDropTable}},
- #' \code{\link{myPostgresCreateGisDb}}, \code{\link{myPostgresImprotShp}}
- #' @examples \dontrun{
- #' myPostgresImprotShp(host = '192.168.255.26', port = 5432,
- #' user = 'osm', password = 'osm', shpFile = NULL, dbname = 'xxxxx', tbl = 'table_xxx')
- #'
- #' }
- #'
- #' @export
- #'
- myPostgresImprotShp <- function(host = '192.168.255.26', port = 5432, user = 'osm', password = 'osm', dbname = 'shp',
- shpFile = NULL, tbl = NULL) {
- '%>%' <- magrittr::`%>%`
- if(is.null(tbl)) {message("Postgis tabeli nimi on puudu!"); return(NULL)}
- if(is.null(shpFile) || !file.exists(shpFile)) shpFile <- file.choose()
- system(paste0("shp2pgsql -s 4326 -I '", shpFile, "' ", tbl, " > /tmp/tmp.sql"))
- queries <- readLines("/tmp/tmp.sql")
- # class(queries)
- # Ühe tekstmuutuja moodustamine
- d0 = paste(queries,collapse=" ")
- # Tekstmuutuja jagamine osadeks semikooloni järele.
- d1 = strsplit(d0, split = ";", fixed = TRUE) %>% unlist()
- # semikooloni lisamine veeru lõppu
- d2 = paste(paste0(d1, ";"))
- write(d2, file = "/tmp/tmp1.sql")
- system(paste0("export PGPASSWORD='", password, "' && psql -U ", user, " -d ", dbname, " -h ", host, " -p ", port, " -f /tmp/tmp1.sql"))
- message("Shp faili importimine postgis andmebaasi lopetati.")
- }
|