#' Teeregistri WFS ja maaameti 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). #' 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, maps, WFS, OSM #' @export #' @examples #' \dontrun{ #' #' conf <- ruut::get_config() #' minu_teed_loomine(conf = conf) #' } minu_teed_loomine <- function(conf = NULL) { ## ------------- muutujad ja teisendused --------------- if (is.null(conf)) { conf <- ruut::get_config() conf$schema <- "teeregister_wfs" } ## Defineerime ajutised failid ja muutujad. tmp_dir <- tempdir() # ajutine kataloog obj <- "demo" tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj) # ajutine gpkg fail conf$gpkg_home <- tmp_dir conf$gpkg_file <- obj conf$primary_key <- "id" ## Loome lingid gpkg ajutistest tabelitest lugemiseks ja kirjutamiseks conf$gpkg_table <- "test_layer_3" tmp_gpkg_file_output_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F) tmp_gpkg_file_input_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T) conf$gpkg_table <- "test_layer_2" tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F) tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T) conf$gpkg_table <- "test_layer_1" tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F) tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T) ## =================== 1. 'riigiteedevõrk' ======================= ## Teeregistri avaandmete kihtide lugemine # Loe instaleeritud tabelid andmebaasis installed_tables <- ruut::db_schema_tablenames(conf = conf) ## Kontrollime ühendatavate andmebaaside olemasolu. if (all(c("pohimaantee", "ramp", "tugimaantee", "korvalmaantee") %in% installed_tables)) { ## 1. Ühendame riigiteed üheks võrgustikuks # ruut::qgis_algorithm_search_by_word("merge") algorithm <- "native:mergevectorlayers" # ruut::qgis_show_help(algorithm = algorithm) conf$table <- "pohimaantee" layer_1 <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry" ) conf$table <- "ramp" layer_2 <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry" ) conf$table <- "tugimaantee" layer_3 <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry" ) conf$table <- "korvalmaantee" layer_4 <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geometry" ) conf$schema <- "minu_teed" # New schema ruut::db_create_new_schema(conf = conf) conf$table <- "riigiteedevork" output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "fid") cmd <- sprintf( "qgis_process run native:mergevectorlayers --CRS='epsg:3301' --LAYERS='%s' --LAYERS='%s' --LAYERS='%s' --LAYERS='%s' --OUTPUT='%s' ", layer_1, layer_2, layer_3, layer_4, output ) system(cmd) cat(sprintf("\n%s\n-------------\n", "Loodi teedev\U00F5rgu andmebaas 'riigiteedevork'.\n")) } ## =================== 2. 'numbrita_kohalik_tee' ======================= ## 1. Sorteerime välja ETAK andmebaasist "e_501_tee_j" kojalikud teed (s.o tyyp IN (50,60,)) ## 1.1 Esmalt sorteerime >= 50 # ruut::qgis_algorithm_search_by_word("extract") algorithm <- "native:extractbyattribute" # ruut::qgis_show_help(algorithm = algorithm) conf$schema <- "eesti" conf$table <- "e_501_tee_j" input <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom" ) output <- tmp_gpkg_file_output_1 str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 3, 'OUTPUT' : '%s', 'VALUE' : '50' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) ## 1.2 Seejärel sorteerime <= 60 input <- tmp_gpkg_file_input_1 output <- tmp_gpkg_file_output_2 str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) ## 1.3 Seejärel sorteerime '<= 60'tee'=NULL (ilma teenumbrita ehk kvartalissisesed). ## Järelejäävad teed on tegelikult 'muud teed'. input <- tmp_gpkg_file_input_2 conf$schema <- "minu_teed" # New schema ruut::db_create_new_schema(conf = conf) conf$table <- "numbrita_kohalik_tee" output <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom" ) str <- sprintf("{ 'FIELD' : 'tee', 'INPUT' : '%s', 'OPERATOR' : 8, 'OUTPUT' : '%s' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) cat(sprintf("\n%s\n-------------\n", "Loodi andmebaas 'numbrita_kohalik_tee'.\n")) ## =================== 3. 'eesti' ======================= ## 3.1 Maakondadest Eesti maismaapiir # ruut::qgis_algorithm_search_by_word("dissolve") algorithm <- "native:dissolve" # ruut::qgis_show_help(algorithm = algorithm) conf$schema <- "maaamet" conf$table <- "maakond" input <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom" ) output <- tmp_gpkg_file_output_1 str <- sprintf("{ 'FIELD' : [], 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) ## 3.2 Kustutame ebaolulised väljad # ruut::qgis_algorithm_search_by_word("delete") algorithm <- "native:deletecolumn" # ruut::qgis_show_help(algorithm = algorithm) input <- tmp_gpkg_file_input_1 output <- tmp_gpkg_file_output_2 cmd <- sprintf("qgis_process run %s --COLUMN='mnimi' --COLUMN='mkood' --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output) system(cmd) ## 3.3 Jaotame multipart polügooni singlepart polügoonideks. # ruut::qgis_algorithm_search_by_word("multipart") algorithm <- "native:multiparttosingleparts" # ruut::qgis_show_help(algorithm = algorithm) input <- tmp_gpkg_file_input_2 output <- tmp_gpkg_file_output_3 str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) ## 3.4 Lisame pindade pindalad ja perimeetri # ruut::qgis_algorithm_search_by_word("columns") algorithm <- "qgis:exportaddgeometrycolumns" # ruut::qgis_show_help(algorithm = algorithm) input <- tmp_gpkg_file_input_3 output <- tmp_gpkg_file_output_1 str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) ## 3.5 Eemaldame objektid mille pindala on väiksem kui 10km2 (väikesaared) # ruut::qgis_algorithm_search_by_word("extract") algorithm <- "native:extractbyattribute" # ruut::qgis_show_help(algorithm = algorithm) input <- tmp_gpkg_file_input_1 conf$schema <- "minu_teed" conf$table <- "eesti" output <- ruut::construct_to_gpkg_output_postgres_str( conf = conf, key = "fid", geometry_field = "geom" ) str <- sprintf("{ 'FIELD' : 'area', 'OPERATOR' : 2, 'VALUE' : '10000000', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output) cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm) system(cmd) }