inspire_grids.R 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #' INSPIRE kaardiruudud
  2. #'
  3. #' Source: \url{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. #' ## inspire_grids()
  13. #' ##
  14. #' ## End(**Not run**)
  15. inspire_grids <- 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. # Temp directory
  24. tmp_dir <- "/tmp/inspire"
  25. if (!dir.exists(tmp_dir)) {
  26. dir.create(tmp_dir)
  27. }
  28. # Download link
  29. url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
  30. # Estonia shapefile (ZIP archive)
  31. map_shapefile <- "Estonia_shapefile.zip"
  32. # Download and save
  33. url_download <- sprintf("%s/%s", url, "")
  34. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  35. if (!file.exists(saveTo)) {
  36. utils::download.file(
  37. url = url_download,
  38. destfile = saveTo, method = "curl", extra = "-L"
  39. )
  40. }
  41. # Unzip
  42. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  43. # List of files
  44. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  45. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  46. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  47. # Export to postgis
  48. if (is.null(conf)) conf <- ruut::get_config()
  49. conf$schema <- "inspire"
  50. # New schema
  51. ruut::db_create_new_schema(conf = conf)
  52. # Multi layer
  53. for (i in 1:length(tbl_names)) {
  54. print(tbl_names[i])
  55. conf$table <- tbl_names[i]
  56. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  57. ## Export to postgis database.
  58. ruut::copy_shp_to_db(
  59. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  60. id = "fid", crs_source = "EPSG:3035", crs_target = "EPSG:4326",
  61. geometry_type = "POLYGON"
  62. )
  63. cat(sprintf(
  64. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  65. ls[i], conf$schema, conf$table
  66. ))
  67. }
  68. # Delete temp directory
  69. # system(sprintf("rm -rf %s/*", tmp_dir))
  70. }
  71. }