|
@@ -0,0 +1,110 @@
|
|
|
|
|
+#' Piirkonnale andmebaasides olevate polügoonide teisendamine
|
|
|
|
|
+#'
|
|
|
|
|
+#' Etteantud piirkonna geomeetrilise piirjoone ('piir') ja selle joone piirikasti ('bb') järele leitakse nende aladega kaetud polügoonid. Andmed salvestatakse postgis andmebaasi.
|
|
|
|
|
+#'
|
|
|
|
|
+#' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
|
|
|
|
|
+#' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
|
|
|
|
|
+#' @return Uute andmebaasi kihtide 'piir_...' ja 'bb_...' loomine.
|
|
|
|
|
+#' @seealso [sf::st_read()], [sf::write_sf()],[sf::st_transform()],[ruut::pk_ruudustike_lisamine()] ,[ruut::pk_polygoonide_lisamine()],[ruut::pk_sellest_alustame_db_loomist()],[ruut::gpkg_piirkonnale_joonte_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::pk_teisendame_polygoone()],[ruut::gpkg_teisendame_jooni()],[ruut::gpkg_teisendame_punkte()]
|
|
|
|
|
+#' @keywords postgis, boundary box, EPSG:3301
|
|
|
|
|
+#' @export
|
|
|
|
|
+#' @examples
|
|
|
|
|
+#' \dontrun{
|
|
|
|
|
+#'
|
|
|
|
|
+#' obj <- "marja"
|
|
|
|
|
+#' pk_teisendame_polygoone(obj = obj, conf = NULL)
|
|
|
|
|
+#'
|
|
|
|
|
+#' # Layers list.
|
|
|
|
|
+#' ruut::db_schema_tablenames(conf = conf)
|
|
|
|
|
+#' }
|
|
|
|
|
+pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
|
|
|
|
|
+ obj <- gsub(" ", "_", tolower(obj))
|
|
|
|
|
+ ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
|
|
|
|
|
+ if (is.null(conf)) {
|
|
|
|
|
+ conf <- ruut::get_config()
|
|
|
|
|
+ conf$schema <- obj
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!any("piir" %in% ruut::db_schema_tablenames(conf = conf))) {
|
|
|
|
|
+ cat("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
|
|
|
|
|
+ return(NULL)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ## ------------- muutujad ja teisendused ---------------
|
|
|
|
|
+ conn <- ruut::db_connect(conf = conf)
|
|
|
|
|
+ try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
|
|
|
|
|
+ tmp_dir <- tempdir()
|
|
|
|
|
+ tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
|
|
|
|
|
+ # system(sprintf("touch %s", tmp_gpkg_file))
|
|
|
|
|
+ conf$gpkg_home <- tmp_dir
|
|
|
|
|
+ conf$gpkg_file <- obj
|
|
|
|
|
+ conf$gpkg_table <- "test_layer"
|
|
|
|
|
+ tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
|
|
|
|
|
+ tmp_gpkg_file_input <- 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)
|
|
|
|
|
+ # write to gpkg
|
|
|
|
|
+ sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
|
|
|
|
|
+ # unlink(tmp_gpkg_file)
|
|
|
|
|
+
|
|
|
|
|
+ ## ====================== teisendused =====================
|
|
|
|
|
+ # Layers list
|
|
|
|
|
+ conf$schema <- obj
|
|
|
|
|
+ layer_names <- ruut::db_schema_tablenames(conf = conf)
|
|
|
|
|
+ layer_names <- layer_names[!layer_names %in% c("bb2", "epk10t_grid", "epk02t_grid", "epk2t_grid")] # c("bb", "bb2", "bb_epk10t_grid", "bb_epk02t_grid", "bb_epk2t_grid", "bb_epk10t", "bb_epk02t")]
|
|
|
|
|
+ for (layer_name in layer_names) {
|
|
|
|
|
+ try(ruumiline_obj <- sf::st_read(dsn = conn, layer = c(conf$schema, layer_name)))
|
|
|
|
|
+ ## Kontlrollime kas geomeetriline objekt on polügoon.
|
|
|
|
|
+ ## Arvutame sel juhul pindala.
|
|
|
|
|
+ is_polygon <- any(grepl("polygon", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE))
|
|
|
|
|
+ if (is_polygon) {
|
|
|
|
|
+ ## Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
|
|
|
|
|
+ ## Peame need pärast ühendama
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("Intersection")
|
|
|
|
|
+ algorithm <- "native:intersection"
|
|
|
|
|
+ conf$table <- sprintf("%s", layer_name)
|
|
|
|
|
+ input <- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
|
|
+ conf = conf, geometry_type = "MultiPolygon", srid = 3301,
|
|
|
|
|
+ checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
|
|
|
|
|
+ )
|
|
|
|
|
+ conf$table <- sprintf("%s", "epk02t_grid")
|
|
|
|
|
+ overlay <- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
|
|
+ conf = conf, geometry_type = "Polygon", srid = 3301,
|
|
|
|
|
+ checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
|
|
|
|
|
+ )
|
|
|
|
|
+ output <- tmp_gpkg_file_output # ajutine fail
|
|
|
|
|
+ str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : ['fid'], '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)
|
|
|
|
|
+ ## Ühendame ruudus olevad pinnad
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("Dissolve")
|
|
|
|
|
+ algorithm <- "native:dissolve"
|
|
|
|
|
+ input <- tmp_gpkg_file_input # ajutine fail
|
|
|
|
|
+ output <- tmp_gpkg_file_output_2 # ajutine fail
|
|
|
|
|
+ str <- sprintf("{ 'FIELD' : ['%s'], 'INPUT' : '%s', 'OUTPUT' : '%s' }", "id", input, output)
|
|
|
|
|
+ cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+ ## Lisame pindade pindalad ja perimeetri
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("attributes")
|
|
|
|
|
+ algorithm <- "qgis:exportaddgeometrycolumns"
|
|
|
|
|
+ input <- tmp_gpkg_file_input_2 # ajutine fail
|
|
|
|
|
+ output <- tmp_gpkg_file_output # ajutine fail
|
|
|
|
|
+ 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)
|
|
|
|
|
+ ## Add field
|
|
|
|
|
+ ## Arvutame pindala protsendi ruudust
|
|
|
|
|
+ # ruut::qgis_algorithm_search_by_word("pythonfield")
|
|
|
|
|
+ algorithm <- "qgis:advancedpythonfieldcalculator"
|
|
|
|
|
+ input <- tmp_gpkg_file_input # ajutine fail
|
|
|
|
|
+ conf$table <- sprintf("%s_epk02t", layer_name)
|
|
|
|
|
+ output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
|
|
|
|
|
+ str <- sprintf("{ 'FIELD_NAME' : 'area_pc', 'FIELD_TYPE' : 1, 'FIELD_LENGTH' : 4, 'FIELD_PRECISION' : 2, 'GLOBAL' : '', 'FORMULA' : 'value = round($geom.area()/10000,4)', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
|
|
|
|
|
+ cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
|
|
+ system(cmd)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ ## Layers list
|
|
|
|
|
+ ruut::db_schema_tablenames(conf = conf)
|
|
|
|
|
+ sf::st_layers(dsn = tmp_gpkg_file)
|
|
|
|
|
+}
|