I'm trying to process multiple operations in GRASS 7.2.0 using R 3.3.2 with the rgrass7
library.
My main goal is to calculate road network distances between multiple locations with GRASS network tools.
My problem is that the locations in my database are organized by individuals and I want to calculate network distances for locations nested in individual, but not for all the locations in the database.
Here's an example of my data structure
ID | Locations
--------------
A | 1
A | 2
A | 3
B | 4
B | 5
I was able to write in GRASS this script that calculates all distances for one ID. I omit the beginning of the script where I load the location (my_locations
) and the road (street_vector
) layers and set the region.
# 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
Next, I used the rgrass7
package to execute GRASS7.2.0. command from R 3.3.2.
The objective was to use a for
loop to generate all my network distance tables with one script.
Here's my code:
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))
}
But when I execute execGRASS
function with v.net
from GRASS, I got the following error:
Error in doGRASS(cmd, flags = flags, ..., parameters = parameters,
echoCmd = echoCmd, :
Invalid parameter name: thresh
It's like it doGRASS does not recognize thresh as a valid v.net
parameter. I'm a bit stuck here, so if someone has a clue of what I did wrong, that would really help.