osm_shp.R 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #' OSM Esri Shapefile
  2. #'
  3. #' Source: \url{https://download.geofabrik.de/europe/} . Andmed salvestatakse postgisi andmebaasi. Schema = 'osm_shp'. Koniguratsiooni muutmiseks muuda konfiguratsiooni muutujat. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
  4. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  5. #' @return No output.
  6. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()]
  7. #' @keywords postgis, maps, ESRI Shpfile, OSM
  8. #' @export
  9. #' @examples
  10. #' ## Not run:
  11. #' ##
  12. #' ## osm_shp()
  13. #' ##
  14. #' ## End(**Not run**)
  15. osm_shp <- function(conf = NULL) {
  16. ans <- utils::askYesNo("Do you want to import maps into database?")
  17. if (!ans) {
  18. cat("\n------------------------\n")
  19. cat("Kaardikihte ei lisatud.")
  20. cat("\n------------------------\n")
  21. }
  22. if (ans) {
  23. # Temp directory
  24. tmp_dir <- "/tmp/osm_shp"
  25. if (!dir.exists(tmp_dir)) {
  26. dir.create(tmp_dir)
  27. }
  28. # Download link
  29. url <- "https://download.geofabrik.de/europe"
  30. # Estonia shapefile (ZIP archive)
  31. map_shapefile <- "estonia-latest-free.shp.zip"
  32. # Download and save
  33. url_download <- sprintf("%s/%s", url, map_shapefile)
  34. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  35. if (!file.exists(saveTo)) {
  36. utils::download.file(
  37. url = url_download,
  38. destfile = saveTo, method = "curl", extra = "-L"
  39. )
  40. }
  41. # Unzip
  42. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  43. # List of files
  44. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  45. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  46. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  47. # Export to postgis
  48. if (is.null(conf)) {
  49. conf <- ruut::get_config()
  50. conf$schema <- "osm_shp"
  51. }
  52. # New schema
  53. ruut::db_create_new_schema(conf = conf)
  54. # Multi layer
  55. for (i in 1:length(tbl_names)) {
  56. print(tbl_names[i])
  57. conf$table <- gsub("gis_osm_", "", gsub("_free_1", "", tbl_names[i]))
  58. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  59. ## Export to postgis database.
  60. ruut::copy_shp_to_db(
  61. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  62. id = "fid", crs_source = "EPSG:4326", crs_target = "EPSG:4326",
  63. geometry_type = "PROMOTE_TO_MULTI"
  64. )
  65. cat(sprintf(
  66. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  67. ls[i], conf$schema, conf$table
  68. ))
  69. }
  70. # Delete temp directory
  71. # system(sprintf("rm -rf %s/*", tmp_dir))
  72. }
  73. }