ajutised_muutujad.R 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. cat(sprintf("\n-----------------\n%s\n", obj))
  2. ## Objekti muutujale väärtuste omistamine, kui seda pole antud.
  3. if (is.null(obj)) {
  4. obj <- "demo"
  5. }
  6. obj <- gsub(" ", "_", tolower(obj))
  7. ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
  8. if (is.null(conf)) {
  9. conf <- ruut::get_config()
  10. conf$schema <- obj
  11. }
  12. ## Kontrollime kas objektil on andmebaas ja kas andmebaasis on piirjoon.
  13. if (ruut::db_is_shema_exist(schema = conf$schema)) {
  14. piir_table_name <- "a00_piir"
  15. pk <- NULL
  16. if (piir_table_name %in% ruut::db_schema_tablenames(conf = conf)) {
  17. conn <- ruut::db_connect(conf = conf)
  18. try(pk <- sf::st_read(dsn = conn, layer = c(conf$schema, piir_table_name)))
  19. }
  20. } else {
  21. pk <- NULL
  22. }
  23. ## Piirkonna muutujale väärtuste omistamine, kui seda pole antud.
  24. if (is.null(pk)) {
  25. # geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658300,6474800],[658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],[658300,6474800]]}')
  26. geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658400,6474800],[658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],[659200,6474950],[658400,6474800]]}')
  27. pk <- sf::read_sf(geojson_txt)
  28. pk <- sf::st_cast(pk, "POLYGON")
  29. sf::st_crs(pk) <- 3301
  30. attributes(pk)$names <- attributes(pk)$sf_column <- "geom"
  31. }
  32. ## ------------- muutujad ja teisendused ---------------
  33. tmp_dir <- tempdir() # ajutine kataloog
  34. tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj) # ajutine gpkg fail
  35. conf$gpkg_home <- tmp_dir
  36. conf$gpkg_file <- obj
  37. ## Loome lingid gpkg ajutistest tabelitest lugemiseks ja kirjutamiseks
  38. conf$gpkg_table <- layer_6 <- "test_layer_6"
  39. tmp_gpkg_file_output_6 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  40. tmp_gpkg_file_input_6 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  41. conf$gpkg_table <- layer_5 <- "test_layer_5"
  42. tmp_gpkg_file_output_5 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  43. tmp_gpkg_file_input_5 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  44. conf$gpkg_table <- layer_4 <- "test_layer_4"
  45. tmp_gpkg_file_output_4 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  46. tmp_gpkg_file_input_4 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  47. conf$gpkg_table <- layer_3 <- "test_layer_3"
  48. tmp_gpkg_file_output_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  49. tmp_gpkg_file_input_3 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  50. conf$gpkg_table <- layer_2 <- "test_layer_2"
  51. tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  52. tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  53. conf$gpkg_table <- layer_1 <- "test_layer_1"
  54. tmp_gpkg_file_output_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
  55. tmp_gpkg_file_input_1 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
  56. # Loome ajutise gpkg faili koos ajutise tabeliga.
  57. sf::write_sf(pk, dsn = tmp_gpkg_file, layer = "test_layer_0", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
  58. cat(sprintf("\n------------------\nAjutise gpkg faili asukoht: %s\n", tmp_gpkg_file))
  59. sf::st_layers(dsn = tmp_gpkg_file)
  60. # unlink(tmp_gpkg_file)