#' Piirkonnale andmebaasides olevate punktide teisendamine #' #' Etteantud piirkonna geomeetrilise piirjoone ('piir') ja selle joone piirikasti ('bb') järele leitakse nende aladega kaetud punktid. Andmed salvestatakse GPKG faili kihtidena. #' #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada. #' @param gpkg_home path Salvestatavate GPKG faili asukoht. #' @return Uute GPKG andmebaasi kihtide 'piir_...' ja 'bb_...' loomine. #' @seealso [sf::st_read()], [sf::write_sf()],[sf::st_transform()],[ruut::gpkg_piirkonnale_ruudustike_lisamine()] ,[ruut::gpkg_piirkonnale_polygoonide_lisamine()],[ruut::gpkg_sellest_alustame_gpkg_loomist()],[ruut::gpkg_piirkonnale_joonte_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_piirkonnale_punktide_lisamine()],[ruut::gpkg_teisendame_polygoone()],[ruut::gpkg_teisendame_jooni()],[ruut::gpkg_teisendame_punkte()] #' @keywords GPKG, boundary box, EPSG:3301 #' @export #' @examples #' \dontrun{ #' #' gpkg_home <- "/tmp" #' obj <- "marja" #' gpkg_teisendame_punkte(obj = obj, gpkg_home = gpkg_home) #' #' # Layers list. #' dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj) #' sf::st_layers(dsn = dsn) #' } gpkg_teisendame_punkte <- function(obj = NULL, gpkg_home = "/tmp") { dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj) if (!file.exists(dsn)) { cat(sprintf("\nSellist faili \"%s\" ei leitud.\n", dsn)) return(NULL) } ## Konfiguratsiooni muutujale väärtuste omistamine conf <- ruut::get_config() tmp_gpkg_file <- tempfile(fileext = ".gpkg") conf$gpkg_home <- dirname(tmp_gpkg_file) conf$gpkg_file <- gsub(paste0(conf$gpkg_home, "/"), "", tmp_gpkg_file) conf$gpkg_file <- gsub(".gpkg", "", conf$gpkg_file) conf$gpkg_home <- gpkg_home conf$gpkg_file <- obj 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) ## ====================== teisendused ===================== # Layers list gpkg_info <- sf::st_layers(dsn = dsn) layer_names <- gpkg_info$name layer_names <- layer_names[!layer_names %in% c("bb2", "bb2_epk10t_grid", "bb2_epk02t_grid", "bb2_epk2t_grid")] # c("bb", "bb2", "bb_epk10t_grid", "bb_epk02t_grid", "bb_epk2t_grid", "bb_epk10t", "bb_epk02t")] for (layer_name in layer_names) { ruumiline_obj <- sf::read_sf(dsn = dsn, layer = 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$gpkg_table <- sprintf("%s_epk02t", layer_name) output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F) conf$gpkg_table <- sprintf("%s", layer_name) points <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T) conf$gpkg_table <- sprintf("%s", "bb2_epk02t_grid") polygons <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T) 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 = dsn, layer = "bb2_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$gpkg_table <- sprintf("%s_epk02t", layer_name) input <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T) output <- sprintf("%s/tif/%s.tif", conf$gpkg_home, 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) ## ----------- TIF TO POSTGIS ------------- conf$schema <- conf$gpkg_file ruut::db_create_new_schema(conf = conf) (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 sf::st_layers(dsn = dsn) }