maaamet_kaardiruudud.R 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #' Maa-ameti kaardiruudukesed
  2. #'
  3. #' Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2. Andmed salvestatakse postgisi andmebaasi. Schema = 'inspire'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
  4. #' @param config list configuration file ruut::get_config()
  5. #' @param schema str database schema name. Deafult is 'inspire'
  6. #' @param url link ESRI Shapefile download url.
  7. #' @seealso [ruut::get_config()]
  8. #' @keywords postgis, maps, ESRI Shpfile
  9. #' @export
  10. #' @examples
  11. #' ## Not run:
  12. #' ##
  13. #' ## maaamet_kaardiruudud()
  14. #' ##
  15. #' ## End(**Not run**)
  16. maaamet_kaardiruudud <- function(config = NULL, schema = "maaamet",
  17. url = NULL) {
  18. ans <- utils::askYesNo("Do you want to import maps into database?")
  19. if (!ans) {
  20. cat("\n------------------------\n")
  21. cat("Kaardikihte ei lisatud.")
  22. cat("\n------------------------\n")
  23. }
  24. if (ans) {
  25. # Temp directory
  26. tmp_dir <- tempdir(check = T)
  27. # Download link
  28. if (is.null(url)) {
  29. urls <- c(
  30. "https://geoportaal.maaamet.ee/docs/pohikaart/epk2T_SHP.zip",
  31. "https://geoportaal.maaamet.ee/docs/pohikaart/epk10T_SHP.zip",
  32. "https://geoportaal.maaamet.ee/docs/pohikaart/epk20T_shp.zip",
  33. "https://geoportaal.maaamet.ee/docs/pohikaart/epk50T_shp.zip",
  34. "https://geoportaal.maaamet.ee/docs/pohikaart/epk100T_shp.zip",
  35. "https://geoportaal.maaamet.ee/docs/pohikaart/epk200T_shp.zip"
  36. )
  37. } else {
  38. urls <- url
  39. }
  40. # Estonia shapefile (ZIP archive)
  41. map_shapefile <- "maaamet_shapefile.zip"
  42. # Download and save
  43. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  44. # Export to postgis
  45. if (is.null(config)) config <- ruut::get_config()
  46. config$schema <- schema
  47. # New schema
  48. ruut::db_create_new_schema(conf = config)
  49. for (j in 1:length(urls)) {
  50. utils::download.file(
  51. url = urls[j],
  52. destfile = saveTo, method = "curl", extra = "-L"
  53. )
  54. # Unzip
  55. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  56. # List of files
  57. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  58. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  59. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  60. # Multi layer
  61. for (i in 1:length(tbl_names)) {
  62. print(tbl_names[i])
  63. config$table <- tbl_names[i]
  64. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  65. ## Export to postgis database.
  66. ruut::copy_shp_to_db(
  67. dir = tmp_dir, layer = tbl_names[i], conf = config,
  68. id = "fid", crs_source = "EPSG:3301", crs_target = "EPSG:4326",
  69. geometry_type = "POLYGON"
  70. )
  71. cat(sprintf(
  72. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  73. ls[i], config$schema, config$table
  74. ))
  75. }
  76. # Delete temp directory
  77. system(sprintf("rm -rf %s/*", tmp_dir))
  78. }
  79. }
  80. }