maaamet_mullakaart.R 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #' Maa-ameti mullakaart
  2. #'
  3. #' Source: \url{https://geoportaal.maaamet.ee/est/Andmed-ja-kaardid/Mullastiku-kaart-p33.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_mullakaart(conf = conf)
  14. #' }
  15. maaamet_mullakaart <- 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_mullakaart")
  26. if (!dir.exists(tmp_dir)) {
  27. dir.create(tmp_dir)
  28. }
  29. # Download link
  30. url <- "https://geoportaal.maaamet.ee/docs/muld/Mullakaart_SHP.zip"
  31. # Estonia shapefile (ZIP archive)
  32. map_shapefile <- "Mullakaart_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. 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 <- "eesti"
  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 <- tolower(tbl_names[i])
  59. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  60. ## Export to postgis database.
  61. conf$primary_key <- "id"
  62. conf$a_srs <- "EPSG:3301"
  63. geometry_type <- "PROMOTE_TO_MULTI"
  64. cmd <- sprintf(
  65. paste0(
  66. "export PGCLIENTENCODING=UTF-8; ",
  67. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -f PostgreSQL ",
  68. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  69. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  70. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  71. "-a_srs %s -nlt %s"
  72. ),
  73. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  74. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  75. conf$a_srs, geometry_type
  76. )
  77. system(cmd)
  78. cat(sprintf(
  79. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  80. ls[i], conf$schema, conf$table
  81. ))
  82. }
  83. # Delete unnecessary files.
  84. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
  85. }
  86. }