|
|
@@ -0,0 +1,108 @@
|
|
|
+#' Piirkonnale andmebaasides olevate punktide teisendamine
|
|
|
+#'
|
|
|
+#' Etteantud piirkonna geomeetrilise piirjoone ('piir') ja selle joone piirikasti ('bb') järele leitakse nende aladega kaetud punktid. 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_sellest_alustame_db_loomist()],[ruut::pk_lisame_ruudustikud()] ,[ruut::pk_lisame_polygoonid()],[ruut::pk_lisame_jooned()],[ruut::pk_lisame_punktid()],[ruut::pk_lisame_piirkonnale_punktid()],[ruut::pk_teisendame_polygoone()],[ruut::pk_teisendame_jooni()],[ruut::pk_teisendame_punkte()]
|
|
|
+#' @keywords postgis, boundary box, EPSG:3301
|
|
|
+#' @export
|
|
|
+#' @examples
|
|
|
+#' \dontrun{
|
|
|
+#'
|
|
|
+#' obj <- "marja"
|
|
|
+#' pk_teisendame_punkte(obj = obj, conf = NULL)
|
|
|
+#'
|
|
|
+#' # Layers list.
|
|
|
+#' ruut::db_schema_tablenames(conf = conf)
|
|
|
+#' }
|
|
|
+pk_teisendame_punkte <- 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)))
|
|
|
+ ## ------------- Punktide arv ruudus ---------------
|
|
|
+ ## Kontlrollime kas geomeetriline objekt on punkt.
|
|
|
+ (is_point <- any(grepl("point", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE)))
|
|
|
+ if (is_point) {
|
|
|
+ # ruut::qgis_algorithm_search_by_word("Count")
|
|
|
+ algorithm <- "native:countpointsinpolygon"
|
|
|
+ conf$table <- sprintf("%s", layer_name)
|
|
|
+ points <- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ conf = conf, geometry_type = "Point", srid = 3301,
|
|
|
+ checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
|
|
|
+ )
|
|
|
+ conf$table <- sprintf("%s", "epk02t_grid")
|
|
|
+ polygons <- 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", 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("{ 'CLASSFIELD' : '', 'FIELD' : 'numpoints', 'OUTPUT' : '%s', 'POINTS' : '%s', 'POLYGONS' : '%s', 'WEIGHT' : '' }", output, points, polygons)
|
|
|
+ cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
+ system(cmd)
|
|
|
+ ## ------------- TIFF ----------------
|
|
|
+ grid_layer <- sf::read_sf(dsn = conn, layer = c(conf$schema, "epk02t_grid"))
|
|
|
+ # sf::st_crs(grid_layer) <- 3301
|
|
|
+ pk_attributes <- attributes(grid_layer$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)
|
|
|
+ # ruut::qgis_algorithm_search_by_word("Rasterize")
|
|
|
+ algorithm <- "gdal:rasterize"
|
|
|
+ # ruut::qgis_show_help(algorithm = algorithm)
|
|
|
+ conf$table <- sprintf("%s_epk02t", layer_name)
|
|
|
+ input <- ruut::construct_to_gpkg_output_postgres_str(
|
|
|
+ conf = conf, geometry_type = "MultiPolygon", srid = 3301,
|
|
|
+ checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
|
|
|
+ )
|
|
|
+ output <- sprintf("%s/%s.tif", tmp_dir, layer_name)
|
|
|
+ str <- sprintf("{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'numpoints', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }", extent, input, output)
|
|
|
+ cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
|
|
|
+ system(cmd) # Salvestame kataloogi
|
|
|
+ ## ----------- TIF TO POSTGIS -------------
|
|
|
+ conf$schema <- obj
|
|
|
+ (cmd <- sprintf(
|
|
|
+ "export PGPASSWORD=%s && raster2pgsql -I %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
|
|
|
+ conf$password, output, conf$schema, layer_name, conf$user, conf$dbname, conf$host, conf$port
|
|
|
+ ))
|
|
|
+ system(cmd)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ## Layers list
|
|
|
+ conf$schema <- obj
|
|
|
+ ruut::db_schema_tablenames(conf = conf)
|
|
|
+ sf::st_layers(dsn = tmp_gpkg_file)
|
|
|
+}
|