|
|
@@ -0,0 +1,89 @@
|
|
|
+#' Maa-ameti kogu Eesti andmestik põhikaardina
|
|
|
+#'
|
|
|
+#' Source: \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Topokaardid-ja-aluskaardid/Eesti-pohikaart-1-10000/Laadi-pohikaart-alla-p612.html} . Andmed salvestatakse postgisi andmebaasi. Schema = 'eesti'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
|
|
|
+#' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
|
|
|
+#' @return No output.
|
|
|
+#' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()]
|
|
|
+#' @keywords postgis, maps, ESRI Shpfile, OSM
|
|
|
+#' @export
|
|
|
+#' @examples
|
|
|
+#' \dontrun{
|
|
|
+#'
|
|
|
+#' conf <- ruut::get_config()
|
|
|
+#' maaamet_eesti(conf = conf)
|
|
|
+#' }
|
|
|
+maaamet_eesti <- function(conf = NULL) {
|
|
|
+ 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) {
|
|
|
+ # Temp directory
|
|
|
+ tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "maaamet_eesti")
|
|
|
+ if (!dir.exists(tmp_dir)) {
|
|
|
+ dir.create(tmp_dir)
|
|
|
+ }
|
|
|
+ # Download link
|
|
|
+ url <- "https://geoportaal.maaamet.ee/index.php?lang_id=1&plugin_act=otsing&andmetyyp=pohikaart&dl=1&f=ETAK_Eesti_pohikaart_SHP.zip&page_id=612"
|
|
|
+ # Estonia shapefile (ZIP archive)
|
|
|
+ map_shapefile <- "ETAK_Eesti_pohikaart_SHP.zip"
|
|
|
+ # Download and save
|
|
|
+ url_download <- sprintf("%s", url)
|
|
|
+ saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
|
|
|
+ if (!file.exists(saveTo)) {
|
|
|
+ utils::download.file(
|
|
|
+ url = url_download,
|
|
|
+ destfile = saveTo, method = "curl", extra = "-L"
|
|
|
+ )
|
|
|
+ }
|
|
|
+ # Unzip
|
|
|
+ utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
|
|
|
+ # List of files
|
|
|
+ tmp_dir_kihid <- sprintf("%s/ETAK_Eesti_pohikaart_2021_SHP/Kihid", tmp_dir)
|
|
|
+ ls <- list.files(path = tmp_dir_kihid, pattern = ".dbf")
|
|
|
+ ls_long <- list.files(path = tmp_dir_kihid, pattern = ".dbf", full.names = T)
|
|
|
+ tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
|
|
|
+ # Export to postgis
|
|
|
+ if (is.null(conf)) {
|
|
|
+ conf <- ruut::get_config()
|
|
|
+ conf$schema <- "eesti"
|
|
|
+ }
|
|
|
+ # New schema
|
|
|
+ ruut::db_create_new_schema(conf = conf)
|
|
|
+ # Multi layer
|
|
|
+ for (i in 1:length(tbl_names)) {
|
|
|
+ print(tbl_names[i])
|
|
|
+ conf$table <- tolower(tbl_names[i])
|
|
|
+ source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
|
|
|
+ ## Export to postgis database.
|
|
|
+ conf$primary_key <- "id"
|
|
|
+ conf$s_srs <- "EPSG:3301"
|
|
|
+ conf$t_srs <- "EPSG:3301"
|
|
|
+ geometry_type <- "PROMOTE_TO_MULTI"
|
|
|
+ if (tbl_names[i] %in% c("Kirik_p")) encoding <- "ISO-8859-4" else encoding <- "UTF-8"
|
|
|
+ cmd <- sprintf(
|
|
|
+ paste0(
|
|
|
+ "export PGCLIENTENCODING=%s; ",
|
|
|
+ "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
|
|
|
+ "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
|
|
|
+ "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
|
|
|
+ "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
|
|
|
+ "-s_srs %s -t_srs %s -nlt %s -skipfailures"
|
|
|
+ ), encoding, encoding,
|
|
|
+ conf$dbname, conf$host, conf$port, conf$user, conf$password,
|
|
|
+ conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
|
|
|
+ conf$s_srs, conf$t_srs, geometry_type
|
|
|
+ )
|
|
|
+ system(cmd)
|
|
|
+ cat(sprintf(
|
|
|
+ "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
|
|
|
+ ls[i], conf$schema, conf$table
|
|
|
+ ))
|
|
|
+ }
|
|
|
+ # Delete unnecessary files.
|
|
|
+ system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
|
|
|
+ }
|
|
|
+}
|