maaamet_kataster.R 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #' Maa-ameti katasriüksused
  2. #'
  3. #' Source: hhttps://geoportaal.maaamet.ee/docs/katastripiirid/paev/KATASTER_EESTI_SHP.zip. 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_kataster()
  14. #' ##
  15. #' ## End(**Not run**)
  16. maaamet_kataster <- 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()
  27. # Download link
  28. if (is.null(url)) url <- "https://geoportaal.maaamet.ee/docs/katastripiirid/paev"
  29. # Estonia shapefile (ZIP archive)
  30. map_shapefile <- "KATASTER_EESTI_SHP.zip"
  31. # Download and save
  32. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  33. utils::download.file(
  34. url = sprintf("%s/%s", url, map_shapefile),
  35. destfile = saveTo, method = "curl", extra = "-L"
  36. )
  37. # Unzip
  38. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  39. # List of files
  40. ls <- list.files(path = tmp_dir, pattern = ".shp")
  41. ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
  42. tbl_names <- unlist(strsplit(x = ls, split = ".shp"))
  43. # Export to postgis
  44. if (is.null(config)) config <- ruut::get_config()
  45. config$schema <- schema
  46. # New schema
  47. ruut::db_create_new_schema(conf = config)
  48. # Multi layer
  49. for (i in 1:length(tbl_names)) {
  50. print(tbl_names[i])
  51. config$table <- tbl_names[i]
  52. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  53. ## Export to postgis database.
  54. ruut::copy_shp_to_db(
  55. dir = tmp_dir, layer = tbl_names[i], conf = config,
  56. id = "fid", crs_source = "EPSG:3301", crs_target = "EPSG:4326",
  57. geometry_type = "POLYGON"
  58. )
  59. cat(sprintf(
  60. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  61. ls[i], config$schema, config$table
  62. ))
  63. }
  64. # Delete temp directory
  65. system(sprintf("rm -rf %s/*", tmp_dir))
  66. }
  67. }