|
|
@@ -34,7 +34,7 @@ pk_sellest_alustame_db_loomist <-
|
|
|
obj = NULL,
|
|
|
conf = NULL) {
|
|
|
## ----------- argumentide vastavuse kontroll -----------
|
|
|
- if (is.null(pk) || !sf::st_is_valid(pk)) {
|
|
|
+ if (is.null(pk)) {
|
|
|
cat("\nPalun kontrolli geomeetrilise kujundi \u00F%igsust.\n")
|
|
|
# return(NULL)
|
|
|
}
|
|
|
@@ -47,189 +47,210 @@ pk_sellest_alustame_db_loomist <-
|
|
|
pk <- vars$pk
|
|
|
conf <- vars$conf
|
|
|
# attributes(pk)$names <- attributes(pk)$sf_column <- "geom"
|
|
|
-
|
|
|
+ ## Tabelid mis on juba andmebaasis ja mida ümber ei kirjutata.
|
|
|
+ olemasolevad_tabelid <- ruut::db_schema_tablenames(conf = conf)
|
|
|
|
|
|
## --------------------- programmi osa ------------------------
|
|
|
|
|
|
## 1. Piirjoone salvestamine
|
|
|
- pk <- sf::st_transform(pk, 3301)
|
|
|
- # Write to gpkg and check validity
|
|
|
- sf::write_sf(
|
|
|
- pk,
|
|
|
- dsn = vars$tmp_gpkg_file,
|
|
|
- layer = vars$conf$gpkg_table,
|
|
|
- driver = "gpkg",
|
|
|
- fid_column_name = "fid",
|
|
|
- delete_dsn = T
|
|
|
- )
|
|
|
- # Geomeetria parandamine
|
|
|
- # ruut::qgis_algorithm_search_by_word("fix")
|
|
|
- algorithm <- "native:fixgeometries"
|
|
|
- input <- vars$tmp_gpkg_file_input_1 # ajutine fail
|
|
|
- output <- vars$tmp_gpkg_file_output_2 # 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)
|
|
|
- ## Multipart to singlepart
|
|
|
- # 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_1 # ajutine fail
|
|
|
- str <- sprintf("{
|
|
|
+ ## Kui olemasolev tabel eksisteerib andmebaasis, siis jätame arvutused
|
|
|
+ ## selle tabeliga vahele.
|
|
|
+ uus_tabel <- "a00_piir"
|
|
|
+ if (uus_tabel %in% olemasolevad_tabelid) {
|
|
|
+ cat(sprintf("\nTabel %s on juba andmebaasis olemas.\n", uus_tabel))
|
|
|
+ } else {
|
|
|
+ pk <- sf::st_transform(pk, 3301)
|
|
|
+ # Write to gpkg and check validity
|
|
|
+ sf::write_sf(
|
|
|
+ pk,
|
|
|
+ dsn = vars$tmp_gpkg_file,
|
|
|
+ layer = vars$conf$gpkg_table,
|
|
|
+ driver = "gpkg",
|
|
|
+ fid_column_name = "fid",
|
|
|
+ delete_dsn = T
|
|
|
+ )
|
|
|
+ # Geomeetria parandamine
|
|
|
+ # ruut::qgis_algorithm_search_by_word("fix")
|
|
|
+ algorithm <- "native:fixgeometries"
|
|
|
+ input <- vars$tmp_gpkg_file_input_1 # ajutine fail
|
|
|
+ output <- vars$tmp_gpkg_file_output_2 # 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)
|
|
|
+ ## Multipart to singlepart
|
|
|
+ # 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_1 # 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)
|
|
|
- # Write to postgis
|
|
|
- ruut::db_create_new_schema(conf = conf)
|
|
|
- # conn <- ruut::db_connect(conf = conf)
|
|
|
- # DBI::dbDisconnect(conn)
|
|
|
- # Eemaldame üleliigsed veerud
|
|
|
- # ruut::qgis_algorithm_search_by_word("Drop")
|
|
|
- algorithm <- "native:deletecolumn"
|
|
|
- input <- vars$tmp_gpkg_file_input_1
|
|
|
- conf$table <- "a00_piir"
|
|
|
- output <-
|
|
|
- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
- conf = conf,
|
|
|
- geometry_field = "geom",
|
|
|
- geometry_type = "Polygon",
|
|
|
- srid = 3301,
|
|
|
- checkPrimaryKeyUnicity = FALSE,
|
|
|
- key = "id"
|
|
|
- )
|
|
|
- cmd <-
|
|
|
- sprintf(
|
|
|
- "qgis_process run %s --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ",
|
|
|
- algorithm,
|
|
|
- input,
|
|
|
- output
|
|
|
- )
|
|
|
- system(cmd)
|
|
|
+ cmd <-
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
+ system(cmd)
|
|
|
+ # Write to postgis
|
|
|
+ ruut::db_create_new_schema(conf = conf)
|
|
|
+ # conn <- ruut::db_connect(conf = conf)
|
|
|
+ # DBI::dbDisconnect(conn)
|
|
|
+ # Eemaldame üleliigsed veerud
|
|
|
+ # ruut::qgis_algorithm_search_by_word("Drop")
|
|
|
+ algorithm <- "native:deletecolumn"
|
|
|
+ input <- vars$tmp_gpkg_file_input_1
|
|
|
+ conf$table <- uus_tabel
|
|
|
+ output <-
|
|
|
+ ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ conf = conf,
|
|
|
+ geometry_field = "geom",
|
|
|
+ geometry_type = "Polygon",
|
|
|
+ srid = 3301,
|
|
|
+ checkPrimaryKeyUnicity = FALSE,
|
|
|
+ key = "id"
|
|
|
+ )
|
|
|
+ cmd <-
|
|
|
+ sprintf(
|
|
|
+ "qgis_process run %s --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ",
|
|
|
+ algorithm,
|
|
|
+ input,
|
|
|
+ output
|
|
|
+ )
|
|
|
+ system(cmd)
|
|
|
+ }
|
|
|
|
|
|
## 2. Piirikasti salvestamine
|
|
|
- ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
|
|
|
- pk_attributes <- attributes(pk$geom)
|
|
|
- extent <-
|
|
|
- sprintf(
|
|
|
- "%s,%s,%s,%s [EPSG:3301]",
|
|
|
- round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
|
|
|
- ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
|
|
|
- round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
|
|
|
- ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
|
|
|
- )
|
|
|
- hspacing <-
|
|
|
- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
|
|
|
- vspacing <-
|
|
|
- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
|
|
|
- conf$table <- "a00_piir_bb"
|
|
|
- output <-
|
|
|
- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
- conf = conf,
|
|
|
- geometry_field = "geom",
|
|
|
- geometry_type = NULL,
|
|
|
- srid = 3301,
|
|
|
- checkPrimaryKeyUnicity = FALSE,
|
|
|
- key = "id"
|
|
|
- )
|
|
|
- # ruut::qgis_algorithm_search_by_word(str = "grid")
|
|
|
- algorithm <- "native:creategrid"
|
|
|
- # cat(ruut::qgis_show_help(algorithm = algorithm))
|
|
|
- cmd <-
|
|
|
- sprintf(
|
|
|
- "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
|
|
|
- algorithm,
|
|
|
- extent,
|
|
|
- hspacing,
|
|
|
- vspacing,
|
|
|
- output
|
|
|
- )
|
|
|
- cat(sprintf("\n%s\n", cmd))
|
|
|
- system(cmd)
|
|
|
+ ## Kui olemasolev tabel eksisteerib andmebaasis, siis jätame arvutused
|
|
|
+ ## selle tabeliga vahele.
|
|
|
+ uus_tabel <- "a00_piir_bb"
|
|
|
+ if (uus_tabel %in% olemasolevad_tabelid) {
|
|
|
+ cat(sprintf("\nTabel %s on juba andmebaasis olemas.\n", uus_tabel))
|
|
|
+ } else {
|
|
|
+ ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
|
|
|
+ pk_attributes <- attributes(pk$geom)
|
|
|
+ extent <-
|
|
|
+ sprintf(
|
|
|
+ "%s,%s,%s,%s [EPSG:3301]",
|
|
|
+ round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
|
|
|
+ ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
|
|
|
+ round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
|
|
|
+ ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
|
|
|
+ )
|
|
|
+ hspacing <-
|
|
|
+ (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
|
|
|
+ vspacing <-
|
|
|
+ (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
|
|
|
+ conf$table <- uus_tabel
|
|
|
+ output <-
|
|
|
+ ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ conf = conf,
|
|
|
+ geometry_field = "geom",
|
|
|
+ geometry_type = NULL,
|
|
|
+ srid = 3301,
|
|
|
+ checkPrimaryKeyUnicity = FALSE,
|
|
|
+ key = "id"
|
|
|
+ )
|
|
|
+ # ruut::qgis_algorithm_search_by_word(str = "grid")
|
|
|
+ algorithm <- "native:creategrid"
|
|
|
+ # cat(ruut::qgis_show_help(algorithm = algorithm))
|
|
|
+ cmd <-
|
|
|
+ sprintf(
|
|
|
+ "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
|
|
|
+ algorithm,
|
|
|
+ extent,
|
|
|
+ hspacing,
|
|
|
+ vspacing,
|
|
|
+ output
|
|
|
+ )
|
|
|
+ cat(sprintf("\n%s\n", cmd))
|
|
|
+ system(cmd)
|
|
|
+ }
|
|
|
|
|
|
## 3. epk2t piirikasti konstrueerimine ja salvestamine s.t kuhu mahuvad sisse
|
|
|
## meie piirkonna epk2t (1x1km) ruudud.
|
|
|
-
|
|
|
- ## 3.1 Leiame piirikastiga seotud epk2t ruudud
|
|
|
- # ruut::qgis_algorithm_search_by_word(str = "extract")
|
|
|
- algorithm <- "native:extractbylocation"
|
|
|
- # cat(ruut::qgis_show_help(algorithm = algorithm))
|
|
|
- conf$table <- "epk2t_grid"
|
|
|
- conf$schema <- "maaamet"
|
|
|
- input <- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
- conf = conf,
|
|
|
- geometry_type = "Polygon",
|
|
|
- srid = 3301,
|
|
|
- checkPrimaryKeyUnicity = TRUE,
|
|
|
- key = "id",
|
|
|
- geometry_field = "geom"
|
|
|
- )
|
|
|
- conf$table <- "a00_piir_bb"
|
|
|
- conf$schema <- obj
|
|
|
- intersect <-
|
|
|
- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ ## Kui olemasolev tabel eksisteerib andmebaasis, siis jätame arvutused
|
|
|
+ ## selle tabeliga vahele.
|
|
|
+ uus_tabel <- "a00_bb2"
|
|
|
+ if (uus_tabel %in% olemasolevad_tabelid) {
|
|
|
+ cat(sprintf("\nTabel %s on juba andmebaasis olemas.\n", uus_tabel))
|
|
|
+ } else {
|
|
|
+ ## 3.1 Leiame piirikastiga seotud epk2t ruudud
|
|
|
+ # ruut::qgis_algorithm_search_by_word(str = "extract")
|
|
|
+ algorithm <- "native:extractbylocation"
|
|
|
+ # cat(ruut::qgis_show_help(algorithm = algorithm))
|
|
|
+ conf$table <- "epk2t_grid"
|
|
|
+ conf$schema <- "maaamet"
|
|
|
+ input <- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
conf = conf,
|
|
|
geometry_type = "Polygon",
|
|
|
srid = 3301,
|
|
|
checkPrimaryKeyUnicity = TRUE,
|
|
|
- key = "id"
|
|
|
- )
|
|
|
- output <- vars$tmp_gpkg_file_output_1 # ajutine fail
|
|
|
- str <-
|
|
|
- sprintf(
|
|
|
- "{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }",
|
|
|
- input,
|
|
|
- intersect,
|
|
|
- output
|
|
|
+ key = "id",
|
|
|
+ geometry_field = "geom"
|
|
|
)
|
|
|
- cmd <-
|
|
|
- ruut::construct_qgis_output_result_to_better_format(str, algorithm)
|
|
|
- system(cmd)
|
|
|
+ conf$table <- "a00_piir_bb"
|
|
|
+ conf$schema <- obj
|
|
|
+ intersect <-
|
|
|
+ ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ conf = conf,
|
|
|
+ geometry_type = "Polygon",
|
|
|
+ srid = 3301,
|
|
|
+ checkPrimaryKeyUnicity = TRUE,
|
|
|
+ key = "id"
|
|
|
+ )
|
|
|
+ output <- vars$tmp_gpkg_file_output_1 # ajutine fail
|
|
|
+ str <-
|
|
|
+ sprintf(
|
|
|
+ "{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }",
|
|
|
+ input,
|
|
|
+ intersect,
|
|
|
+ output
|
|
|
+ )
|
|
|
+ cmd <-
|
|
|
+ ruut::construct_qgis_output_result_to_better_format(str, algorithm)
|
|
|
+ system(cmd)
|
|
|
|
|
|
- ## 3.2 Ajutise faili abil epk2t piirikasti konstrueerimine
|
|
|
- test_layer_obj <-
|
|
|
- sf::read_sf(dsn = vars$tmp_gpkg_file, layer = "test_layer_1")
|
|
|
- pk_attributes <- attributes(test_layer_obj$geom)
|
|
|
- extent <-
|
|
|
- sprintf(
|
|
|
- "%s,%s,%s,%s [EPSG:3301]",
|
|
|
- round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
|
|
|
- ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
|
|
|
- round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
|
|
|
- ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
|
|
|
- )
|
|
|
- hspacing <-
|
|
|
- (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
|
|
|
- vspacing <-
|
|
|
- (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
|
|
|
- conf$table <- "a00_bb2"
|
|
|
- output <-
|
|
|
- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
- conf = conf,
|
|
|
- geometry_field = "geom",
|
|
|
- geometry_type = NULL,
|
|
|
- srid = 3301,
|
|
|
- checkPrimaryKeyUnicity = FALSE,
|
|
|
- key = "id"
|
|
|
- )
|
|
|
- # ruut::qgis_algorithm_search_by_word(str = "grid")
|
|
|
- algorithm <- "native:creategrid"
|
|
|
- # cat(ruut::qgis_show_help(algorithm = algorithm))
|
|
|
- cmd <-
|
|
|
- sprintf(
|
|
|
- "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
|
|
|
- algorithm,
|
|
|
- extent,
|
|
|
- hspacing,
|
|
|
- vspacing,
|
|
|
- output
|
|
|
- )
|
|
|
- cat(sprintf("\n%s\n", cmd))
|
|
|
- system(cmd)
|
|
|
+ ## 3.2 Ajutise faili abil epk2t piirikasti konstrueerimine
|
|
|
+ test_layer_obj <-
|
|
|
+ sf::read_sf(dsn = vars$tmp_gpkg_file, layer = "test_layer_1")
|
|
|
+ pk_attributes <- attributes(test_layer_obj$geom)
|
|
|
+ extent <-
|
|
|
+ sprintf(
|
|
|
+ "%s,%s,%s,%s [EPSG:3301]",
|
|
|
+ round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
|
|
|
+ ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
|
|
|
+ round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
|
|
|
+ ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
|
|
|
+ )
|
|
|
+ hspacing <-
|
|
|
+ (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
|
|
|
+ vspacing <-
|
|
|
+ (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
|
|
|
+ conf$table <- uus_tabel
|
|
|
+ output <-
|
|
|
+ ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ conf = conf,
|
|
|
+ geometry_field = "geom",
|
|
|
+ geometry_type = NULL,
|
|
|
+ srid = 3301,
|
|
|
+ checkPrimaryKeyUnicity = FALSE,
|
|
|
+ key = "id"
|
|
|
+ )
|
|
|
+ # ruut::qgis_algorithm_search_by_word(str = "grid")
|
|
|
+ algorithm <- "native:creategrid"
|
|
|
+ # cat(ruut::qgis_show_help(algorithm = algorithm))
|
|
|
+ cmd <-
|
|
|
+ sprintf(
|
|
|
+ "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
|
|
|
+ algorithm,
|
|
|
+ extent,
|
|
|
+ hspacing,
|
|
|
+ vspacing,
|
|
|
+ output
|
|
|
+ )
|
|
|
+ cat(sprintf("\n%s\n", cmd))
|
|
|
+ system(cmd)
|
|
|
+ }
|
|
|
|
|
|
## Layers list
|
|
|
conf$schema <- obj
|