
org.opentripplanner.ext.stopconsolidation.StopConsolidationParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
The newest version!
package org.opentripplanner.ext.stopconsolidation;
import com.csvreader.CsvReader;
import com.google.common.collect.ImmutableListMultimap;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.BooleanUtils;
import org.opentripplanner.ext.stopconsolidation.model.ConsolidatedStopGroup;
import org.opentripplanner.transit.model.framework.FeedScopedId;
public class StopConsolidationParser {
private record StopGroupEntry(String groupId, FeedScopedId stopId, boolean isPrimary) {}
public static List parseGroups(InputStream is) {
try {
var reader = new CsvReader(is, StandardCharsets.UTF_8);
reader.setDelimiter(',');
reader.readHeaders();
var entries = new ArrayList();
while (reader.readRecord()) {
var id = reader.get("stop_group_id");
var feedId = reader.get("feed_id");
var stopId = reader.get("stop_id");
var isPrimary = BooleanUtils.toBoolean(Integer.parseInt(reader.get("is_primary")));
var entry = new StopGroupEntry(id, new FeedScopedId(feedId, stopId), isPrimary);
entries.add(entry);
}
var groups = entries
.stream()
.collect(
ImmutableListMultimap.flatteningToImmutableListMultimap(
x -> x.groupId,
Stream::of
)
);
return groups
.keys()
.stream()
.map(key -> {
var group = groups.get(key);
var primaryId = group.stream().filter(e -> e.isPrimary).findAny().orElseThrow().stopId;
var secondaries = group
.stream()
.filter(e -> !e.isPrimary)
.map(e -> e.stopId)
.collect(Collectors.toSet());
return new ConsolidatedStopGroup(primaryId, secondaries);
})
.distinct()
.toList();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy