maaamet_kaardiruudud.R 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 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. #' ## Not run:
  11. #' ##
  12. #' ## maaamet_kaardiruudud()
  13. #' ##
  14. #' ## End(**Not run**)
  15. maaamet_kaardiruudud <- function(conf = NULL) {
  16. ans <- utils::askYesNo("Do you want to import maps into database?")
  17. if (!ans) {
  18. cat("\n------------------------\n")
  19. cat("Kaardikihte ei lisatud.")
  20. cat("\n------------------------\n")
  21. }
  22. if (ans) {
  23. # Download links
  24. urls <- c(
  25. "https://geoportaal.maaamet.ee/docs/pohikaart/epk2T_SHP.zip",
  26. "https://geoportaal.maaamet.ee/docs/pohikaart/epk10T_SHP.zip",
  27. "https://geoportaal.maaamet.ee/docs/pohikaart/epk20T_shp.zip",
  28. "https://geoportaal.maaamet.ee/docs/pohikaart/epk50T_shp.zip",
  29. "https://geoportaal.maaamet.ee/docs/pohikaart/epk100T_shp.zip",
  30. "https://geoportaal.maaamet.ee/docs/pohikaart/epk200T_shp.zip"
  31. )
  32. # Estonia shapefile (ZIP archive)
  33. map_shapefile <- "maaamet_shapefile.zip"
  34. # Export to postgis
  35. if (is.null(conf)) {
  36. conf <- ruut::get_config()
  37. conf$schema <- "maaamet"
  38. }
  39. # New schema
  40. ruut::db_create_new_schema(conf = conf)
  41. for (j in 1:length(urls)) {
  42. # Temp directory
  43. tmp_dir <- sprintf("/tmp/maaamet_epk/%s", j)
  44. if (!dir.exists(tmp_dir)) {
  45. dir.create(tmp_dir, recursive = T)
  46. }
  47. # Download and save
  48. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  49. utils::download.file(
  50. url = urls[j],
  51. destfile = saveTo, method = "curl", extra = "-L"
  52. )
  53. # Unzip
  54. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  55. # List of files
  56. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  57. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  58. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  59. # Multi layer
  60. for (i in 1:length(tbl_names)) {
  61. print(tbl_names[i])
  62. conf$table <- tbl_names[i]
  63. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  64. ## Export to postgis database.
  65. ruut::copy_shp_to_db(
  66. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  67. id = "fid", crs_source = "EPSG:3301", crs_target = "EPSG:4326",
  68. geometry_type = "POLYGON"
  69. )
  70. cat(sprintf(
  71. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  72. ls[i], conf$schema, conf$table
  73. ))
  74. }
  75. # Delete temp directory
  76. # system(sprintf("rm -rf %s/*", tmp_dir))
  77. }
  78. }
  79. }