Commit 6d8b54d9 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

read group information from trajectoryFile if present

parent 3505a760
......@@ -13,6 +13,7 @@ import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.io.IOUtils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
......@@ -37,14 +38,17 @@ public class TrajectoryReader {
private Set<String> xKeys;
private Set<String> yKeys;
private Set<String> targetIdKeys;
private Set<String> groupIdKeys;
private int pedIdIndex;
private int stepIndex;
private int xIndex;
private int yIndex;
private int targetIdIndex;
private int groupIdIndex;
public TrajectoryReader(final Path trajectoryFilePath, final Scenario scenario) throws IOException {
public TrajectoryReader(final Path trajectoryFilePath, final Scenario scenario) {
this(trajectoryFilePath, scenario.getAttributesPedestrian());
}
......@@ -60,7 +64,9 @@ public class TrajectoryReader {
xKeys = new HashSet<>();
yKeys = new HashSet<>();
targetIdKeys = new HashSet<>();
groupIdKeys = new HashSet<>();
//should be set via Processor.getHeader
pedestrianIdKeys.add("id");
pedestrianIdKeys.add("pedestrianId");
stepKeys.add("timeStep");
......@@ -68,12 +74,14 @@ public class TrajectoryReader {
xKeys.add("x");
yKeys.add("y");
targetIdKeys.add("targetId");
groupIdKeys.add("groupId");
pedIdIndex = -1;
stepIndex = -1;
xIndex = -1;
yIndex = -1;
targetIdIndex = -1;
groupIdIndex = -1;
}
......@@ -97,34 +105,20 @@ public class TrajectoryReader {
yIndex = index;
} else if (targetIdKeys.contains(columns[index])) {
targetIdIndex = index;
} else if (groupIdKeys.contains(columns[index])){
groupIdIndex = index;
}
}
try {
if (pedIdIndex != -1 && xIndex != -1 && yIndex != -1 && stepIndex != -1) {
try (BufferedReader in = IOUtils.defaultBufferedReader(this.trajectoryFilePath)) {
return in.lines()
.skip(1) //Skip header line
.map(line -> line.split(SPLITTER))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
VPoint pos = new VPoint(Double.parseDouble(cells[xIndex]), Double.parseDouble(cells[yIndex]));
int targetId = targetIdIndex != -1 ? Integer.parseInt(cells[targetIdIndex]) : -1;
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
LinkedList<Integer> targets = new LinkedList<Integer>();
targets.addFirst(targetId);
ped.setTargets(targets);
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(pair -> pair.getKey(), Collectors.mapping(pair -> pair.getValue(), Collectors.toList())));
}
} else {
if (pedIdIndex != -1 && xIndex != -1 && yIndex != -1 && stepIndex != -1 && groupIdIndex == -1) {
// load default values with no groups
return readStandardTrajectoryFile();
} else if(pedIdIndex != -1 && xIndex != -1 && yIndex != -1 && stepIndex != -1) {//here groupIdIndex is != -1
// load values with group information
return readGroupTrajectoryFile();
}
else {
throw new IOException("could not read trajectory file, some colums are missing.");
}
} catch (Exception e) {
......@@ -133,4 +127,62 @@ public class TrajectoryReader {
}
}
private Map<Step, List<Agent>> readStandardTrajectoryFile() throws IOException {
try (BufferedReader in = IOUtils.defaultBufferedReader(this.trajectoryFilePath)) {
return in.lines()
.skip(1) //Skip header line
.map(line -> line.split(SPLITTER))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
VPoint pos = new VPoint(Double.parseDouble(cells[xIndex]), Double.parseDouble(cells[yIndex]));
int targetId = targetIdIndex != -1 ? Integer.parseInt(cells[targetIdIndex]) : -1;
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
LinkedList<Integer> targets = new LinkedList<>();
targets.addFirst(targetId);
ped.setTargets(targets);
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList())));
} catch (Exception e){
logger.warn("could not read trajectory file. The file format might not be compatible or it is missing.");
throw e;
}
}
private Map<Step, List<Agent>> readGroupTrajectoryFile() throws IOException{
try (BufferedReader in = IOUtils.defaultBufferedReader(this.trajectoryFilePath)) {
return in.lines()
.skip(1) //Skip header line
.map(line -> line.split(SPLITTER))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
VPoint pos = new VPoint(Double.parseDouble(cells[xIndex]), Double.parseDouble(cells[yIndex]));
int targetId = targetIdIndex != -1 ? Integer.parseInt(cells[targetIdIndex]) : -1;
int groupId = targetIdIndex != -1 ? Integer.parseInt(cells[groupIdIndex]) : -1;
Pedestrian ped = new Pedestrian(new AttributesAgent(this.attributesPedestrian, pedestrianId), new Random());
ped.setPosition(pos);
ped.addGroupId(groupId);
LinkedList<Integer> targets = new LinkedList<>();
targets.addFirst(targetId);
ped.setTargets(targets);
return Pair.create(new Step(Integer.parseInt(cells[0])), ped);
})
.collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList())));
} catch (Exception e){
logger.warn("could not read trajectory file. The file format might not be compatible or it is missing.");
throw e;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment