inspire_grids.R 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #' INSPIRE kaardiruudud
  2. #'
  3. <<<<<<< HEAD
  4. #' 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.
  5. #' @param config list configuration file ruut::get_config()
  6. #' @param schema str database schema name. Deafult is 'inspire'
  7. #' @param url link ESRI Shapefile download url.
  8. #' @seealso [ruut::get_config()]
  9. #' @keywords postgis, maps, ESRI Shpfile
  10. #' @export
  11. #' @examples
  12. #' ## Not run:
  13. #' ##
  14. #' ## inspire_grids()
  15. #' ##
  16. #' ## End(**Not run**)
  17. inspire_grids <- function(config = NULL, schema = "inspire",
  18. url = NULL) {
  19. =======
  20. #' Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2. Andmed salvestatakse postgisi andmebaasi. Schema = 'inspire'. Koniguratsiooni muutmiseks muuda konfiguratsiooni.
  21. #' @keywords postgis, maps, ESRI Shpfile
  22. #' @export
  23. #' @examples
  24. #' inspire_grids()
  25. inspire_grids <- function() {
  26. >>>>>>> 741ecf506c7c6a793e1f9b1ca6fab87fdf5738ce
  27. ans <- utils::askYesNo("Do you want to import maps into database?")
  28. if (!ans) {
  29. cat("\n------------------------\n")
  30. cat("Kaardikihte ei lisatud.")
  31. cat("\n------------------------\n")
  32. }
  33. if (ans) {
  34. # Temp directory
  35. tmp_dir <- tempdir()
  36. # Download link
  37. if (is.null(url)) url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
  38. # Estonia shapefile (ZIP archive)
  39. map_shapefile <- "Estonia_shapefile.zip"
  40. # Download and save
  41. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  42. utils::download.file(
  43. url = url,
  44. destfile = saveTo, method = "curl", extra = "-L"
  45. )
  46. # Unzip
  47. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  48. # List of files
  49. ls <- list.files(path = tmp_dir, pattern = ".shp")
  50. ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
  51. tbl_names <- unlist(strsplit(x = ls, split = ".shp"))
  52. # Export to postgis
  53. if (is.null(config)) config <- ruut::get_config()
  54. config$schema <- schema
  55. # New schema
  56. ruut::db_create_new_schema(conf = config)
  57. # Multi layer
  58. for (i in 1:length(tbl_names)) {
  59. print(tbl_names[i])
  60. config$table <- tbl_names[i]
  61. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  62. ## Export to postgis database.
  63. ruut::copy_shp_to_db(
  64. dir = tmp_dir, layer = tbl_names[i], conf = config,
  65. id = "fid", crs_source = "EPSG:3035", crs_target = "EPSG:4326",
  66. geometry_type = "POLYGON"
  67. )
  68. cat(sprintf(
  69. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  70. ls[i], config$schema, config$table
  71. ))
  72. }
  73. }
  74. }