| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #' Teeregistri WFS ja maaameti andmete oma teeandmete 'minu_teed' loomine
- #'
- #' 1. Põhi-, kõrval ja tugimaanteedest ning rambid moodustatakse uus andmebaas 'riigiteedevork'.
- #' 2. Ilma teenumbrita kohalike teede andmebaasi 'numbrita_kohalik_tee' loomine.
- #' 3. Eesti kaart ainult suurte saartega (üle 10km2).
- #' 4. Polügoniseerime riigiteedevõrgu.
- #' Eesti avaandmete \url{https://avaandmed.eesti.ee/datasets?emsId=35} teabevärava kaudu teederegistri WFS (Web Feature Service) andmete eksportimine postgisandmebaasi. WFS andmestiku kirjeldus: \url{https://teeregister-api.mnt.ee/teenus/wfs?request=GetCapabilities&service=WFS}
- #'
- #' @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()], [estmap::teeregister_wfs()]
- #' @keywords postgis
- #' @export
- #' @examples
- #' \dontrun{
- #'
- #' conf <- ruut::get_config()
- #' minu_teed_loomine(conf = conf)
- #' }
- minu_teed_loomine <- function(conf = NULL) {
- ## ------------- muutujad ja teisendused ---------------
- # source("R/ajutised_muutujad.R")
- vars <- ajutised_muutujad(conf = conf)
- conf <- vars$conf
- # conf$schema <- "minu_teed"
- # conf$table <- "zzz"
- # output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom")
- # conf$schema <- "eesti"
- # conf$table <- "e_501_tee_j"
- # cat(paste(paste0('"', ruut::db_table_colnames(conf = conf)$column_name, '"'), collapse = ","))
- ## -------- 1.1 Kontrollime andmebaasis vajalike tabelite olemasolu ----------
- conf$schema <- "minu_teed"
- tbl_names <- ruut::db_schema_tablenames(conf = conf)
- if (!all(c("teedevork_riigiteed_osadena", "eesti", "teedevork") %in% tbl_names)) {
- cat("\n------------------\nPuuduvad vajalikud andmebaasitabelid.\n")
- return(NULL)
- }
- ## -------------------- 1.1 Loeme eesti kontuurjoone -------------------------
- # ruut::qgis_algorithm_search_by_word("lines")
- algorithm <- "native:polygonstolines"
- # ruut::qgis_show_help(algorithm = algorithm)
- conf$schema <- "minu_teed"
- conf$table <- "eesti"
- input <- ruut::construct_to_gpkg_output_postgres_str(
- conf = conf, geometry_type = "Polygon", srid = 3301,
- checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
- )
- output <- vars$tmp_gpkg_file_output_1
- str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## ------------------------- 1.2 Riigi teed osadena ---------------------------
- ## 4.2 Eemaldame riigiteedest rambid.
- algorithm <- "native:extractbyattribute"
- # ruut::qgis_show_help(algorithm = algorithm)
- conf$schema <- "minu_teed"
- conf$table <- "teedevork_riigiteed_osadena"
- input <- ruut::construct_to_gpkg_output_postgres_str(
- conf = conf, geometry_type = "MultiLineString", srid = 3301,
- checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
- )
- output <- vars$tmp_gpkg_file_output_2
- str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '30' }", input, output)
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## ------------- 1.3 Riigiteede ja eesti kontuuri ühendamine -----------------
- ## 4.3 Enne polügoniseerimist ühendame eesti kontuuri ja teedevõrgu üheks.
- # ruut::qgis_algorithm_search_by_word("merge")
- algorithm <- "native:mergevectorlayers"
- # ruut::qgis_show_help(algorithm = algorithm)
- vars$layer_1 <- vars$tmp_gpkg_file_input_1
- vars$layer_2 <- vars$tmp_gpkg_file_input_2
- output <- vars$tmp_gpkg_file_output_3
- cmd <- sprintf(
- "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ",
- vars$layer_1, vars$layer_2, output
- )
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## -------------------- 1.4 Teeme veergude valiku ----------------------------
- # cat(paste(paste0('--FIELDS=\'', ruut::db_table_colnames(conf = conf)$column_name, '\''), collapse = " "))
- # cat(paste(paste0('--COLUMN=\'', ruut::db_table_colnames(conf = conf)$column_name, '\''), collapse = " "))
- # ruut::qgis_algorithm_search_by_word("retain")
- # algorithm <- "native:deletecolumn"
- algorithm <- "native:retainfields"
- # ruut::qgis_show_help(algorithm = algorithm)
- input <- vars$tmp_gpkg_file_input_3
- output <- vars$tmp_gpkg_file_output_4
- cmd <- sprintf("qgis_process run %s --FIELDS='fid' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## ----------------------- 1.5 Polügoniseerimine -----------------------------
- ## 4.4 Polügoniseerime saadud riigiteedevõrgu. Tekib teedega piiratud polügoonid.
- # ruut::qgis_algorithm_search_by_word("Polygonize")
- algorithm <- "native:polygonize"
- # ruut::qgis_show_help(algorithm = algorithm)
- input <- vars$tmp_gpkg_file_input_4
- output <- vars$tmp_gpkg_file_output_5
- str <- sprintf("{ 'INPUT' : '%s', 'KEEP_FIELDS' : False, 'OUTPUT' : '%s' }", input, output)
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- system(cmd)
- ## ------------------ 1.4 Copy temp gpkg file to postgis ---------------------
- conf$schema <- "minu_teed"
- conf$table <- "teedevork_riigiteed_a"
- PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
- cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, vars$tmp_gpkg_file, vars$layer_5, "POLYGON", conf$schema, conf$table)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
- ## ------------------ Teedevõrgu polügoniseerimine ------------------
- ## 4.5 Copy temp gpkg file to postgis.
- conf$schema <- "minu_teed"
- conf$table <- "teedevork_riigiteed_a"
- PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
- cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=fid -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, vars$tmp_gpkg_file, vars$layer_4, "MULTIPOLYGON", conf$schema, conf$table)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
- # ## =================== !!!!!!!!!!!!!! POOLELI !!!!!!!!!!!! =======================
- #
- # ## 4.2 Lõikame Eesti kaardist polügoniseerime riigiteedevõrgu välja.
- # ## Tekib ümber Eesti raam, mis tuleb jagada kuidagi transporditsoonideks.
- # # ruut::qgis_algorithm_search_by_word("Difference")
- # algorithm <- "native:difference"
- # # ruut::qgis_show_help(algorithm = algorithm)
- # conf$schema <- "minu_teed"
- # conf$table <- "eesti"
- # input <- ruut::construct_to_gpkg_output_postgres_str(
- # conf = conf, geometry_type = "Polygon", srid = 3301,
- # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
- # )
- # conf$schema <- "minu_teed"
- # conf$table <- "riigiteedevork_a"
- # overlay <- ruut::construct_to_gpkg_output_postgres_str(
- # conf = conf, geometry_type = "Polygon", srid = 3301,
- # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
- # )
- # conf$schema <- "minu_teed"
- # conf$table <- "riigiteedevork_xxx"
- # output <- ruut::construct_to_gpkg_output_postgres_str(
- # conf = conf, geometry_field = "geom", key = "fid"
- # )
- # str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s', 'OVERLAY' : '%s' }", input, output, overlay)
- # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- # system(cmd)
- #
- #
- # ## 4.3 Kuna polügoniseeritud pinnad ei olnud täiuslikud, siis teostame
- # ## polügoniseeriist veelkorra. Selleks lõikame esmalt teedevõrgust
- # ## tekkinud raamiga need teed mis jäävad raamis sisse.
- # # ruut::qgis_algorithm_search_by_word("Intersection")
- # algorithm <- "native:intersection"
- # # ruut::qgis_show_help(algorithm = algorithm)
- # conf$schema <- "minu_teed"
- # conf$table <- "riigiteedevork"
- # input <- ruut::construct_to_gpkg_output_postgres_str(
- # conf = conf, geometry_type = "MultiLineString", srid = 3301,
- # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
- # )
- # conf$schema <- "minu_teed"
- # conf$table <- "riigiteedevork_xxx"
- # overlay <- ruut::construct_to_gpkg_output_postgres_str(
- # conf = conf, geometry_type = "MultiPolygon", srid = 3301,
- # checkPrimaryKeyUnicity = TRUE, key = "fid", geometry_field = "geom"
- # )
- # conf$schema <- "minu_teed"
- # conf$table <- "riigiteedevork_xxx1"
- # output <- ruut::construct_to_gpkg_output_postgres_str(
- # conf = conf, geometry_field = "geom"
- # )
- # str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : [], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }", input, output, overlay)
- # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- # system(cmd)
- }
|