#' Maa-ameti mullakaart #' #' Source: \url{https://geoportaal.maaamet.ee/est/Andmed-ja-kaardid/Mullastiku-kaart-p33.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_mullakaart(conf = conf) #' } maaamet_mullakaart <- 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_mullakaart") if (!dir.exists(tmp_dir)) { dir.create(tmp_dir) } # Download link url <- "https://geoportaal.maaamet.ee/docs/muld/Mullakaart_SHP.zip" # Estonia shapefile (ZIP archive) map_shapefile <- "Mullakaart_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 ls <- list.files(path = tmp_dir, pattern = ".dbf") ls_long <- list.files(path = tmp_dir, 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, tbl_names[i]) ## Export to postgis database. conf$primary_key <- "id" conf$a_srs <- "EPSG:3301" geometry_type <- "PROMOTE_TO_MULTI" cmd <- sprintf( paste0( "export PGCLIENTENCODING=UTF-8; ", "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -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 ", "-a_srs %s -nlt %s" ), conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table, conf$a_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)) } }