maaamet_eesti.R 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #' Maa-ameti kogu Eesti andmestik põhikaardina
  2. #'
  3. #' Source: \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Topokaardid-ja-aluskaardid/Eesti-pohikaart-1-10000/Laadi-pohikaart-alla-p612.html} . Andmed salvestatakse postgisi andmebaasi. Schema = 'eesti'. 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_eesti(conf = conf)
  14. #' }
  15. maaamet_eesti <- 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_eesti")
  26. if (!dir.exists(tmp_dir)) {
  27. dir.create(tmp_dir)
  28. }
  29. # Download link
  30. url <- "https://geoportaal.maaamet.ee/index.php?lang_id=1&plugin_act=otsing&andmetyyp=pohikaart&dl=1&f=ETAK_Eesti_pohikaart_SHP.zip&page_id=612"
  31. # Estonia shapefile (ZIP archive)
  32. map_shapefile <- "ETAK_Eesti_pohikaart_SHP.zip"
  33. # Download and save
  34. url_download <- sprintf("%s", url)
  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. tmp_dir_kihid <- sprintf("%s/ETAK_Eesti_pohikaart_2021_SHP/Kihid", tmp_dir)
  46. ls <- list.files(path = tmp_dir_kihid, pattern = ".dbf")
  47. ls_long <- list.files(path = tmp_dir_kihid, pattern = ".dbf", full.names = T)
  48. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  49. # Export to postgis
  50. if (is.null(conf)) {
  51. conf <- ruut::get_config()
  52. conf$schema <- "eesti"
  53. }
  54. # New schema
  55. ruut::db_create_new_schema(conf = conf)
  56. # Multi layer
  57. for (i in 1:length(tbl_names)) {
  58. print(tbl_names[i])
  59. conf$table <- tolower(tbl_names[i])
  60. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  61. ## Export to postgis database.
  62. conf$primary_key <- "id"
  63. conf$s_srs <- "EPSG:3301"
  64. conf$t_srs <- "EPSG:3301"
  65. geometry_type <- "PROMOTE_TO_MULTI"
  66. if (tbl_names[i] %in% c("Kirik_p")) encoding <- "ISO-8859-4" else encoding <- "UTF-8"
  67. cmd <- sprintf(
  68. paste0(
  69. "export PGCLIENTENCODING=%s; ",
  70. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  71. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  72. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  73. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  74. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  75. ), encoding, encoding,
  76. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  77. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  78. conf$s_srs, conf$t_srs, geometry_type
  79. )
  80. system(cmd)
  81. cat(sprintf(
  82. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  83. ls[i], conf$schema, conf$table
  84. ))
  85. }
  86. # Delete unnecessary files.
  87. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
  88. }
  89. }