maaamet_kaardiruudud.R 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #' Maa-ameti kaardiruudukesed
  2. #'
  3. #' Source: \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Kaardilehtede-susteemid-p224.html}. Andmed salvestatakse postgisi andmebaasi. Schema = 'maaamet'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. 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. #' maaamet_kaardiruudud(conf = conf)
  14. #' }
  15. maaamet_kaardiruudud <- 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"), "maaamet_epk")
  26. if (!dir.exists(tmp_dir)) {
  27. dir.create(tmp_dir)
  28. }
  29. # Params
  30. params <- c("epk2T_SHP", "epk10T_SHP", "epk20T_shp", "epk50T_shp", "epk100T_shp", "epk200T_shp")
  31. # Export to postgis
  32. if (is.null(conf)) {
  33. conf <- ruut::get_config()
  34. conf$schema <- "maaamet"
  35. }
  36. # New schema
  37. ruut::db_create_new_schema(conf = conf)
  38. for (j in 1:length(params)) {
  39. # Shapefile (ZIP archive)
  40. param_split <- strsplit(params[j], "_")[[1]][1]
  41. map_shapefile <- sprintf("%s.zip", param_split)
  42. # Download and save
  43. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  44. if (!file.exists(saveTo)) {
  45. utils::download.file(
  46. url = sprintf("https://geoportaal.maaamet.ee/docs/pohikaart/%s.zip", params[j]),
  47. destfile = saveTo, method = "curl", extra = "-L"
  48. )
  49. }
  50. # Unzip
  51. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  52. # List of files
  53. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  54. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  55. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  56. # Multi layer
  57. for (i in 1:length(tbl_names)) {
  58. print(tbl_names[i])
  59. conf$table <- tbl_names[i]
  60. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  61. ## Export to postgis database.
  62. ruut::copy_shp_to_db(
  63. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  64. id = "fid", crs_source = "EPSG:3301", crs_target = "EPSG:4326",
  65. geometry_type = "POLYGON"
  66. )
  67. cat(sprintf(
  68. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  69. ls[i], conf$schema, conf$table
  70. ))
  71. }
  72. # Delete unnecessary files.
  73. system(sprintf("find %s -type f -not -name '*.zip' -print0 | xargs -0 rm --", tmp_dir))
  74. }
  75. }
  76. }