|
@@ -0,0 +1,196 @@
|
|
|
|
|
+#' Maa-ameti ortofoto splittimine 1x1km suurusteks TIFF fotofailideks
|
|
|
|
|
+#'
|
|
|
|
|
+#' Maa-ameti allalaetud ortofoto jagamine väiksemateks osadeks. Andmestruktuur: raster. Mõõtkava 1:2 000 (vaata ruudustiku infot \url{https://geoportaal.maaamet.ee/est/ruumiandmed/kaardilehtede-susteemid-p224.html}). Loe: \url{https://geoportaal.maaamet.ee/est/andmed-ja-kaardid/ortofotod-p99.html}. Uued kaardid salvestatakse kataloogi (default = '~/ortofotod'). Faili nimi on EPK2T maa-ameti kaardiruudu väärtusega. Eesti topokaardistuse tarbeks toodetud ortofotodes on piksli suurusega 20-40 cm ja katavad kogu riigi territooriumi. Andmete kasutamisel palume viidata Maa-ametile. Näiteks: 'Ortofoto, Maa-amet 2021'. Käsitsi allalaadimine /url{https://geoportaal.maaamet.ee/index.php?lang_id=1&page_id=610}.
|
|
|
|
|
+#' @importFrom magrittr %>%
|
|
|
|
|
+#' @param epk10t 5-kohaline number, mis vastab kaardi 1:10 000 ruudu numbrile \url{https://geoportaal.maaamet.ee/est/ruumiandmed/kaardilehtede-susteemid-p224.html}.
|
|
|
|
|
+#' @param dir kataloog, kus GEOTIFF failasub.
|
|
|
|
|
+#' @param saveToDir Splititud GEOTIFF failide salvestamise kataloog.
|
|
|
|
|
+#' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
|
|
|
|
|
+#' @return GEOTIFF files.
|
|
|
|
|
+#' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()]
|
|
|
|
|
+#' @keywords maa-amet, ortofoto
|
|
|
|
|
+#' @export
|
|
|
|
|
+#' @examples
|
|
|
|
|
+#' \dontrun{
|
|
|
|
|
+#'
|
|
|
|
|
+#' conf <- ruut::get_config()
|
|
|
|
|
+#' ortofoto_split_to_epk2t(epk10t = 54711, dir = "/home/ardo/ortofotod",
|
|
|
|
|
+#' saveToDir = "/home/ardo/ortofotod", conf = conf)
|
|
|
|
|
+#' }
|
|
|
|
|
+ortofoto_split_to_epk2t <-
|
|
|
|
|
+ function(epk10t = NULL,
|
|
|
|
|
+ dir = "/home/ardo/ortofotod",
|
|
|
|
|
+ saveToDir = "/home/ardo/ortofotod",
|
|
|
|
|
+ conf = NULL) {
|
|
|
|
|
+ ## ------------- muutujad ja teisendused ---------------
|
|
|
|
|
+ # source("R/ajutised_muutujad.R")
|
|
|
|
|
+ vars <- ajutised_muutujad(conf = conf)
|
|
|
|
|
+ conf <- vars$conf
|
|
|
|
|
+ # epk10t <- 62053; epk10t <- 65813
|
|
|
|
|
+ # epk10t numbri õigsuse kontroll
|
|
|
|
|
+ epk10t_all <-
|
|
|
|
|
+ utils::read.csv(file = system.file("csv", "epk10t.csv", package = "estmap"),
|
|
|
|
|
+ header = T)[, "nr"]
|
|
|
|
|
+ if (!epk10t %in% epk10t_all) {
|
|
|
|
|
+ cat(
|
|
|
|
|
+ sprintf(
|
|
|
|
|
+ "\n---------------\n\nValitud epk10t ruudu number %s ei ole \u00F5ige.\n",
|
|
|
|
|
+ epk10t
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ return()
|
|
|
|
|
+ }
|
|
|
|
|
+ # GEOTIFF failide salvestamise kataloog.
|
|
|
|
|
+ if (!dir.exists(saveToDir)) {
|
|
|
|
|
+ dir.create(saveToDir, recursive = T)
|
|
|
|
|
+ }
|
|
|
|
|
+ # Kas GEOTIFF fail eksisteerib.
|
|
|
|
|
+ input_tif_file <- sprintf("%s/%s.tif", dir, epk10t)
|
|
|
|
|
+ if (!file.exists(input_tif_file)) {
|
|
|
|
|
+ cat(sprintf(
|
|
|
|
|
+ "\n---------------\n\nEi leia TIFF faili: %s.\n",
|
|
|
|
|
+ input_tif_file
|
|
|
|
|
+ ))
|
|
|
|
|
+ return()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ # 1. ============= Ortofoto TIFF faili piirkast =============
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("mask")
|
|
|
|
|
+ algorithm <- "native:polygonfromlayerextent"
|
|
|
|
|
+ input <- input_tif_file
|
|
|
|
|
+ output <- vars$tmp_gpkg_file_output_1 # ajutine fail
|
|
|
|
|
+ str <-
|
|
|
|
|
+ sprintf("{
|
|
|
|
|
+ 'INPUT' : '%s',
|
|
|
|
|
+ 'OUTPUT' : '%s',
|
|
|
|
|
+ 'ROUND_TO' : 0.0
|
|
|
|
|
+}",
|
|
|
|
|
+ input,
|
|
|
|
|
+ output)
|
|
|
|
|
+ cmd <-
|
|
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+
|
|
|
|
|
+ # 2. ============= epk2t_grid ja TIFF piirkasti ühisosa =============
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("intersection")
|
|
|
|
|
+ algorithm <- "native:extractbylocation"
|
|
|
|
|
+ conf$schema <- "maaamet"
|
|
|
|
|
+ conf$table <- "epk2t_grid"
|
|
|
|
|
+ input <-
|
|
|
|
|
+ ruut::construct_to_gpkg_output_postgres_str(
|
|
|
|
|
+ conf = conf,
|
|
|
|
|
+ geometry_field = "geom",
|
|
|
|
|
+ geometry_type = "Polygon",
|
|
|
|
|
+ srid = 3301,
|
|
|
|
|
+ checkPrimaryKeyUnicity = TRUE,
|
|
|
|
|
+ key = "id"
|
|
|
|
|
+ )
|
|
|
|
|
+ intersect <- vars$tmp_gpkg_file_input_1 # ajutine fail
|
|
|
|
|
+ output <- vars$tmp_gpkg_file_output_2 # ajutine fail
|
|
|
|
|
+ str <-
|
|
|
|
|
+ sprintf(
|
|
|
|
|
+ "{
|
|
|
|
|
+ 'INPUT' : '%s',
|
|
|
|
|
+ 'INTERSECT' : '%s',
|
|
|
|
|
+ 'OUTPUT' : '%s',
|
|
|
|
|
+ 'PREDICATE' : [6]
|
|
|
|
|
+}",
|
|
|
|
|
+ input,
|
|
|
|
|
+ intersect,
|
|
|
|
|
+ output
|
|
|
|
|
+ )
|
|
|
|
|
+ cmd <-
|
|
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ # 3. ============= Multipolygon to polygon =============
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("singlepart")
|
|
|
|
|
+ algorithm <- "native:multiparttosingleparts"
|
|
|
|
|
+ input <- vars$tmp_gpkg_file_input_2 # ajutine fail
|
|
|
|
|
+ output <- vars$tmp_gpkg_file_output_3 # ajutine fail
|
|
|
|
|
+ str <-
|
|
|
|
|
+ sprintf(" {
|
|
|
|
|
+ 'INPUT' : '%s',
|
|
|
|
|
+ 'OUTPUT' : '%s'
|
|
|
|
|
+ }",
|
|
|
|
|
+ input,
|
|
|
|
|
+ output)
|
|
|
|
|
+ cmd <-
|
|
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+
|
|
|
|
|
+ # 4. ============= epk2t kaardiosadega tiff faili lõikamine tükkideks =============
|
|
|
|
|
+ # EPK2T kaartide atribuutide tabeli kirjutamine csv fail.
|
|
|
|
|
+ input <- paste(vars$tmp_gpkg_file,
|
|
|
|
|
+ "test_layer_3") # ajutine fail
|
|
|
|
|
+ output <-
|
|
|
|
|
+ sprintf("%s/%s.csv", vars$tmp_dir, "xxxx") # ajutine fail
|
|
|
|
|
+
|
|
|
|
|
+ cmd <-
|
|
|
|
|
+ sprintf(
|
|
|
|
|
+ "ogr2ogr -f \"CSV\" -lco SEPARATOR=COMMA -lco STRING_QUOTING=IF_NEEDED -lco LINEFORMAT=LF %s %s",
|
|
|
|
|
+ output,
|
|
|
|
|
+ input
|
|
|
|
|
+ )
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+ # EPK2T indeksite lugemine CSV failist
|
|
|
|
|
+ epk2t_list <- utils::read.csv(file = output)[, 'id']
|
|
|
|
|
+ # Loop
|
|
|
|
|
+ for (i in 1:length(epk2t_list)) {
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("extract")
|
|
|
|
|
+ algorithm <- "native:extractbyattribute"
|
|
|
|
|
+ input <- vars$tmp_gpkg_file_input_3
|
|
|
|
|
+ output <- vars$tmp_gpkg_file_output_4
|
|
|
|
|
+ str <-
|
|
|
|
|
+ sprintf(
|
|
|
|
|
+ "{'FIELD' : 'id',
|
|
|
|
|
+ 'INPUT' : '%s',
|
|
|
|
|
+ 'OPERATOR' : 0,
|
|
|
|
|
+ 'OUTPUT' : '%s',
|
|
|
|
|
+ 'VALUE' : '%d'
|
|
|
|
|
+}",
|
|
|
|
|
+ input,
|
|
|
|
|
+ output,
|
|
|
|
|
+ epk2t_list[i]
|
|
|
|
|
+ )
|
|
|
|
|
+ cmd <-
|
|
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+ # =================== Salvestame TIFF faili =============================
|
|
|
|
|
+ algorithm <- "gdal:cliprasterbymasklayer"
|
|
|
|
|
+ input <- input_tif_file
|
|
|
|
|
+ mask <- vars$tmp_gpkg_file_input_4
|
|
|
|
|
+ str <-
|
|
|
|
|
+ sprintf(
|
|
|
|
|
+ "{ 'ALPHA_BAND' : false,
|
|
|
|
|
+ 'CROP_TO_CUTLINE' : true,
|
|
|
|
|
+ 'DATA_TYPE' : 0,
|
|
|
|
|
+ 'EXTRA' : '',
|
|
|
|
|
+ 'INPUT' : '%s',
|
|
|
|
|
+ 'KEEP_RESOLUTION' : false,
|
|
|
|
|
+ 'MASK' : '%s',
|
|
|
|
|
+ 'MULTITHREADING' : false,
|
|
|
|
|
+ 'NODATA' : null,
|
|
|
|
|
+ 'OPTIONS' : '',
|
|
|
|
|
+ 'OUTPUT' : '%s/%s.tif',
|
|
|
|
|
+ 'SET_RESOLUTION' : false,
|
|
|
|
|
+ 'SOURCE_CRS' : 'EPSG:3301',
|
|
|
|
|
+ 'TARGET_CRS' : 'EPSG:3301'
|
|
|
|
|
+}",
|
|
|
|
|
+ input,
|
|
|
|
|
+ mask,
|
|
|
|
|
+ saveToDir,
|
|
|
|
|
+ epk2t_list[i]
|
|
|
|
|
+ )
|
|
|
|
|
+ cmd <-
|
|
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+ }
|
|
|
|
|
+ cat(
|
|
|
|
|
+ sprintf(
|
|
|
|
|
+ "\n---------------\n\nEPK2T tiff failid salvestati kataloogi %s.\n",
|
|
|
|
|
+ epk10t
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|