ogr2postgis.R 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #' Automatic ESRI shp file import into postgis table with ogr2ogr
  2. #' @details Selle funktsiooniga imporditakse zip kujul ESRI shp fail postgis andmebaasi.
  3. #' Geomeetria tuleb määrata parameetriga \code{\link{wkt}}. Andmete projektsioon
  4. #' konverteeritakse EPSG:4326 kujule. Esialgne tabel kirjutatakse andmebaasis üle.
  5. #' @param shpZip Zipped ESRI sho file.
  6. #' @param dsn data source name (interpretation varies by driver — for some drivers,
  7. #' dsn is a file name, but may also be a folder).
  8. #' @param tbl Postgis table name.
  9. #' @param wkt Text representations (WKT) of the spatial objects.
  10. #' @return return message.
  11. #' @seealso \code{\link{ogr2postgis}}, \code{\link{wkt}},
  12. #' \code{\link{myPostgresCreateGisDb}}, \code{\link{myPostgresImprotShp}}
  13. #' @examples \dontrun{
  14. #' # Postgis conf
  15. #' source('/home/ardo/apps/git/R/apps/postgis/conf.R')
  16. #' tblName = "test_tabel"
  17. #' # Zipped shp file
  18. #' shpZip = "/home/ardo/apps/git/R/apps/postgis/data-sources/geodata/transport/teed/tanel/riigimnt.zip"
  19. #' ogr2postgis(shpZip = shpZip,
  20. #' dsn = conf$dsn, tbl = tblName, wkt = "MULTILINESTRING")
  21. #'
  22. #' }
  23. #'
  24. #' @export
  25. #'
  26. ogr2postgis <- function(shpZip, dsn, tbl = NULL,
  27. wkt = c("NONE", "GEOMETRY", "POINT", "LINESTRING",
  28. "POLYGON", "MULTIPOINT", "MULTILINESTRING",
  29. "MULTIPOLYGON", "GEOMETRYCOLLECTION")) {
  30. # Unzip
  31. zip <- utils::unzip(zipfile = shpZip,
  32. list = F, overwrite = T, exdir = paste0(tempdir(), "/unzip"))
  33. shpFileName <- list.files(paste0(tempdir(), "/unzip"), pattern = ".shp$", full.names = T, recursive = T)
  34. cmd <- paste0('ogr2ogr -lco FID="gid" -wrapdateline -t_srs "EPSG:4326" ',
  35. '-overwrite -lco GEOMETRY_NAME="geom" -a_srs "EPSG:4326"',
  36. ' -f "PostgreSQL" "', dsn, '"',
  37. ' -nlt ', wkt,' ', shpFileName, ' -nln ', tbl)
  38. # shpFileName
  39. system(cmd)
  40. unlink(zip)
  41. message(paste("Tabeli", tbl, "kopeeriti andmebaasi."))
  42. # cmd
  43. }
  44. #' Text representations (WKT) of the spatial objects
  45. #' @details Text representations (WKT) of the spatial objects. Postgis andmebaasis
  46. #' kasutatavate geomeetriate tüübid. Vajalik ESRI shp failide importimisel
  47. #' (\code{\link{ogr2postgis}})
  48. #' @seealso \code{\link{ogr2postgis}}, \code{\link{wkt}}
  49. #' @examples \dontrun{
  50. #' transpordiTsoonid::wkt
  51. #'
  52. #' }
  53. #'
  54. #' @export
  55. #'
  56. wkt = c("NONE", "GEOMETRY", "POINT", "LINESTRING", "POLYGON",
  57. "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION")