osm_shp.R 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #' \dontrun{
  11. #'
  12. #' conf <- ruut::get_config()
  13. #' osm_shp(conf = conf)
  14. #' }
  15. osm_shp <- function(conf = NULL) {
  16. ans <- utils::askYesNo("Do you want to import maps into database?", default = F)
  17. if (!ans | is.na(ans)) {
  18. cat("\n------------------------\n")
  19. cat("Kaardikihte ei lisatud.")
  20. cat("\n------------------------\n")
  21. return()
  22. }
  23. if (ans) {
  24. # Temp directory
  25. tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "osm_shp")
  26. if (!dir.exists(tmp_dir)) {
  27. dir.create(tmp_dir)
  28. }
  29. # Download link
  30. url <- "https://download.geofabrik.de/europe"
  31. # Estonia shapefile (ZIP archive)
  32. map_shapefile <- "estonia-latest-free.shp.zip"
  33. # Download and save
  34. url_download <- sprintf("%s/%s", url, map_shapefile)
  35. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  36. if (!file.exists(saveTo)) {
  37. utils::download.file(
  38. url = url_download,
  39. destfile = saveTo, method = "curl", extra = "-L"
  40. )
  41. }
  42. # Unzip
  43. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  44. # List of files
  45. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  46. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  47. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  48. # Export to postgis
  49. if (is.null(conf)) {
  50. conf <- ruut::get_config()
  51. conf$schema <- "osm_shp"
  52. }
  53. # New schema
  54. ruut::db_create_new_schema(conf = conf)
  55. # Multi layer
  56. for (i in 1:length(tbl_names)) {
  57. print(tbl_names[i])
  58. conf$table <- gsub("gis_osm_", "", gsub("_free_1", "", tbl_names[i]))
  59. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  60. ## Export to postgis database.
  61. ruut::copy_shp_to_db(
  62. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  63. id = "fid", crs_source = "EPSG:4326", crs_target = "EPSG:4326",
  64. geometry_type = "PROMOTE_TO_MULTI"
  65. )
  66. cat(sprintf(
  67. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  68. ls[i], conf$schema, conf$table
  69. ))
  70. }
  71. # Delete unnecessary files.
  72. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
  73. }
  74. }