|
|
@@ -7,30 +7,27 @@
|
|
|
#' @keywords postgis, maps, ESRI Shpfile, OSM
|
|
|
#' @export
|
|
|
#' @examples
|
|
|
-#' ## Not run:
|
|
|
-#' ##
|
|
|
-#' ## maaamet_kaardiruudud()
|
|
|
-#' ##
|
|
|
-#' ## End(**Not run**)
|
|
|
+#' \dontrun{
|
|
|
+#'
|
|
|
+#' conf <- ruut::get_config()
|
|
|
+#' maaamet_kaardiruudud(conf = conf)
|
|
|
+#' }
|
|
|
maaamet_kaardiruudud <- function(conf = NULL) {
|
|
|
- ans <- utils::askYesNo("Do you want to import maps into database?")
|
|
|
- if (!ans) {
|
|
|
+ ans <- utils::askYesNo("Do you want to import maps into database?", default = F)
|
|
|
+ if (!ans | is.na(ans)) {
|
|
|
cat("\n------------------------\n")
|
|
|
cat("Kaardikihte ei lisatud.")
|
|
|
cat("\n------------------------\n")
|
|
|
+ return()
|
|
|
}
|
|
|
if (ans) {
|
|
|
- # Download links
|
|
|
- urls <- c(
|
|
|
- "https://geoportaal.maaamet.ee/docs/pohikaart/epk2T_SHP.zip",
|
|
|
- "https://geoportaal.maaamet.ee/docs/pohikaart/epk10T_SHP.zip",
|
|
|
- "https://geoportaal.maaamet.ee/docs/pohikaart/epk20T_shp.zip",
|
|
|
- "https://geoportaal.maaamet.ee/docs/pohikaart/epk50T_shp.zip",
|
|
|
- "https://geoportaal.maaamet.ee/docs/pohikaart/epk100T_shp.zip",
|
|
|
- "https://geoportaal.maaamet.ee/docs/pohikaart/epk200T_shp.zip"
|
|
|
- )
|
|
|
- # Estonia shapefile (ZIP archive)
|
|
|
- map_shapefile <- "maaamet_shapefile.zip"
|
|
|
+ # Temp directory
|
|
|
+ tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "maaamet_epk")
|
|
|
+ if (!dir.exists(tmp_dir)) {
|
|
|
+ dir.create(tmp_dir)
|
|
|
+ }
|
|
|
+ # Params
|
|
|
+ params <- c("epk2T_SHP", "epk10T_SHP", "epk20T_shp", "epk50T_shp", "epk100T_shp", "epk200T_shp")
|
|
|
# Export to postgis
|
|
|
if (is.null(conf)) {
|
|
|
conf <- ruut::get_config()
|
|
|
@@ -38,18 +35,18 @@ maaamet_kaardiruudud <- function(conf = NULL) {
|
|
|
}
|
|
|
# New schema
|
|
|
ruut::db_create_new_schema(conf = conf)
|
|
|
- for (j in 1:length(urls)) {
|
|
|
- # Temp directory
|
|
|
- tmp_dir <- sprintf("/tmp/maaamet_epk/%s", j)
|
|
|
- if (!dir.exists(tmp_dir)) {
|
|
|
- dir.create(tmp_dir, recursive = T)
|
|
|
- }
|
|
|
+ for (j in 1:length(params)) {
|
|
|
+ # Shapefile (ZIP archive)
|
|
|
+ param_split <- strsplit(params[j], "_")[[1]][1]
|
|
|
+ map_shapefile <- sprintf("%s.zip", param_split)
|
|
|
# Download and save
|
|
|
saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
|
|
|
- utils::download.file(
|
|
|
- url = urls[j],
|
|
|
- destfile = saveTo, method = "curl", extra = "-L"
|
|
|
- )
|
|
|
+ if (!file.exists(saveTo)) {
|
|
|
+ utils::download.file(
|
|
|
+ url = sprintf("https://geoportaal.maaamet.ee/docs/pohikaart/%s.zip", params[j]),
|
|
|
+ destfile = saveTo, method = "curl", extra = "-L"
|
|
|
+ )
|
|
|
+ }
|
|
|
# Unzip
|
|
|
utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
|
|
|
# List of files
|
|
|
@@ -72,8 +69,8 @@ maaamet_kaardiruudud <- function(conf = NULL) {
|
|
|
ls[i], conf$schema, conf$table
|
|
|
))
|
|
|
}
|
|
|
- # Delete temp directory
|
|
|
- # system(sprintf("rm -rf %s/*", tmp_dir))
|
|
|
+ # Delete unnecessary files.
|
|
|
+ system(sprintf("find %s -type f -not -name '*.zip' -print0 | xargs -0 rm --", tmp_dir))
|
|
|
}
|
|
|
}
|
|
|
}
|