0

我正在尝试使用带有rgrass7库的 R 3.3.2 在 GRASS 7.2.0 中处理多个操作。

我的主要目标是使用 GRASS 网络工具计算多个位置之间的道路网络距离。

我的问题是我的数据库中的位置是由个人组织的,我想计算嵌套在个人中的位置的网络距离,而不是数据库中的所有位置。

这是我的数据结构的示例

ID | Locations
--------------
A  |    1
A  |    2
A  |    3
B  |    4
B  |    5

我能够在 GRASS 中编写这个脚本来计算一个 ID 的所有距离。我省略了加载位置 ( my_locations) 和道路 ( street_vector) 图层并设置区域的脚本开头。

# Select locations for ID == 'A'
v.extract --overwrite --verbose input=my_locations output=my_locations_sub where="ID='A'"

# Prepare network: connect my_locations to street vector map 
v.net input=street_vectors points=my_locations_sub output=network operation=connect thresh=500

## The option operation=connect allows to create the links between lines and      
## points and to create a single vector map containing lines, nodes and 
## links. 

## The parameter thresh=500 means that only points within the distance of 
## 500 metres are connected to the lines.

# verify result
v.category input=network option=report

# shortest path between all points
v.net.allpairs input=network out=network_dist --overwrite

# output results
v.db.select network_dist

接下来,我使用该rgrass7包执行GRASS7.2.0。来自 R 3.3.2 的命令。目标是使用一个for循环来生成我所有的网络距离表和一个脚本。

这是我的代码:

library(rgrass7)

IDs <- read.table("./path/to/my/ID_list.txt", header = TRUE)

# initialisation of GRASS7
initGRASS(gisBase = "C:/Program Files/GRASS GIS 7.2.0",
          gisDbase = "C:/Users/Me/GRASSdata",
          location = "My_project", mapset = "R", override = TRUE)   

# For loop to calculate road network distance by IDs
for (i in 1: length(IDs)){
  ID <- IDs[i]
  condition <- paste0("ID=\'", as.character(ID), "\'")

  execGRASS('v.extract', parameters = list(input='my_locations', 
             output='my_locations_sub', where=condition))

  execGRASS('v.net', parameters = list(input = street_vectors', 
             points = 'my_locations_sub', output = 'network', 
             operation = 'connect', thresh = 500))

  execGRASS('v.net.allpairs', parameters = list(input='network', 
                                                out='netword_dist'), 
             flags = 'overwrite')

  # Set the path to write files
  path <- paste0("./data/", ID, ".csv")

  # Write file
  execGRASS('db.out.ogr', parameters = list(input = 'network_dist', 
                                            output = path))

}

但是当我execGRASS使用 GRASS 执行函数时v.net,出现以下错误:

 Error in doGRASS(cmd, flags = flags, ..., parameters = parameters, 
 echoCmd = echoCmd,  : 
       Invalid parameter name: thresh

就像 doGRASS 不将 thresh 识别为有效v.net参数一样。我有点卡在这里,所以如果有人知道我做错了什么,那真的很有帮助。

4

1 回答 1

0

我只是意识到我犯了一个错误会复制我的代码。我错过了input = 'street_vectors'以下部分中的 ' :

execGRASS('v.net', parameters = list(input = 'street_vectors', 
         points = 'my_locations_sub', output = 'network', 
         operation = 'connect', thresh = 500))
于 2017-02-27T20:36:22.660 回答