0

我想使用目标管道中的包 rgrass7 并行化我的 GRASS GIS 分析和计算。由于我是 GRASS 的新手,我不确定如何在同一个 GRASSGIS 数据库中正确设置多个地图集。到目前为止,我尝试了以下代码:

library(future)
library(targets)

tar_script({
  options(tidyverse.quiet = TRUE)

  GRASS_DIRECTORY <- "grassdata"
  if (!fs::dir_exists(GRASS_DIRECTORY)) {
    fs::dir_create(GRASS_DIRECTORY)
  } else {
    fs::dir_delete(GRASS_DIRECTORY)
    fs::dir_create(GRASS_DIRECTORY)
  }

  generate_lines <-
    function() {
      coordinates <- tribble(
        ~x, ~y, ~feature_id, ~streamorder,
        7, 1, 1, 1,
        7, 3, 1, 1,
        7, 3, 17, 2,
        9, 4, 17, 2,
      )

      # coordinates <-
      # coordinates %>%
      #   mutate(feature_id = rep(1:(nrow(coordinates)/2)))

      sf_lines <-
        coordinates %>%
        mutate(point = str_c(x, " ", y)) %>%
        group_by(feature_id, streamorder) %>%
        summarise(point_collection = str_c(point, collapse = ", "), .groups = "drop") %>%
        mutate(geometry = str_c("LINESTRING (", point_collection, ")")) %>%
        select(-point_collection) %>%
        st_as_sf(wkt = "geometry") %>%
        mutate(across(all_of(c("feature_id", "streamorder")), as.integer))

      st_crs(sf_lines) <- 3035

      return(sf_lines)
    }

  initiate_grass_db <-
    function(sf_lines, streamorder) {
      grass_streamorder_directory <-
        "streamorder" %>%
        str_c(streamorder) %>%
        str_c(GRASS_DIRECTORY, ., sep = "/")
      fs::dir_create(grass_streamorder_directory)

      grass_streamorder_location <-
        grass_streamorder_directory %>%
        str_c("location", sep = "/")
      fs::dir_create(grass_streamorder_location)

      grass_streamorder_mapset <-
        grass_streamorder_directory %>%
        str_c("mapset", sep = "/")
      fs::dir_create(grass_streamorder_mapset)

      initGRASS(
        gisBase = "C:/Program Files/GRASS GIS 7.8",
        gisDbase = grass_streamorder_directory,
        location = basename(grass_streamorder_location),
        mapset = "PERMANENT",
        override = TRUE
      )

      execGRASS("g.proj",
        flags = c("c", "quiet"),
        proj4 = st_crs(sf_lines)$proj4string
      )
      b_box <- st_bbox(sf_lines)
      execGRASS("g.region",
        flags = c("quiet"),
        n = as.character(b_box["ymax"]), s = as.character(b_box["ymin"]),
        e = as.character(b_box["xmax"]), w = as.character(b_box["xmin"]),
        res = as.character(1)
      )
    }
  tar_option_set(
    packages = c(
      "rgrass7",
      "fs",
      "sf",
      "future",
      "tidyverse"
    ),
    memory = "transient",
    garbage_collection = TRUE
  )

  future::plan(future::multisession)

  list(
    tar_target(
      data,
      generate_lines()
    ),
    tar_target(
      streamorders,
      data %>%
        distinct(streamorder) %>%
        pull(streamorder)
    ),
    tar_target(
      init_grass,
      initiate_grass_db(data, streamorders),
      pattern = map(streamorders)
    )
  )
})

targets::tar_make_future(workers = 4)
#> [34m*[39m run target data
#> [34m*[39m run target streamorders
#> [34m*[39m run branch init_grass_940ac7e4
#> [34m*[39m run branch init_grass_50f0d53a
#> FEHLER: Unable to open element file <> for <DEFAULT_WIND@PERMANENT>
#> WARNUNG: Datum <Unknown_based_on_GRS80_ellipsoid> von GRASS nicht erkannt
#>          und keine Parameter gefunden.
#> FEHLER: Unable to open element file <> for <DEFAULT_WIND@PERMANENT>
#> FEHLER: Unable to open element file <> for <DEFAULT_WIND@PERMANENT>
#> WARNUNG: Datum <Unknown_based_on_GRS80_ellipsoid> von GRASS nicht erkannt
#>          und keine Parameter gefunden.
#> [34m*[39m end pipeline
#> Warnmeldungen:
#> 1: Ausführung von Kommando 'g.proj.exe -w' ergab Status 1 
#> 2: 1 targets produced warnings. Run tar_meta(fields = warnings) for the messages. 

reprex 包于 2021-03-16 创建(v0.3.0)

我正在使用 rgrass7_0.2-5 和独立的 winGRASS 7.8.5-2 都安装在默认目录中

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)

我真的很感激一些关于如何做到这一点的提示!

4

0 回答 0