Skip to content

Commit

Permalink
Also add secondaries from stop clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed May 22, 2024
1 parent 09c5e47 commit 4205b33
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 70 deletions.
41 changes: 19 additions & 22 deletions src/ext/java/org/opentripplanner/ext/geocoder/LuceneIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ public class LuceneIndex implements Serializable {

private static final String TYPE = "type";
private static final String ID = "id";
private static final String SECONDARY_IDS = "secondary_ids";
private static final String SUGGEST = "suggest";
private static final String NAME = "name";
private static final String NAME_NGRAM = "name_ngram";
private static final String CODE = "code";
private static final String LAT = "latitude";
private static final String LON = "longitude";
private static final String MODE = "mode";
private static final String AGENCY_IDS = "agency_ids";

private final TransitService transitService;
private final Analyzer analyzer;
Expand Down Expand Up @@ -96,12 +95,11 @@ public LuceneIndex(TransitService transitService) {
directoryWriter,
StopLocation.class,
stopLocation.getId().toString(),
List.of(),
ListUtils.ofNullable(stopLocation.getName()),
ListUtils.ofNullable(stopLocation.getCode()),
stopLocation.getCoordinate().latitude(),
stopLocation.getCoordinate().longitude(),
Set.of(),
Set.of()
stopLocation.getCoordinate().longitude()
)
);

Expand All @@ -112,12 +110,11 @@ public LuceneIndex(TransitService transitService) {
directoryWriter,
StopLocationsGroup.class,
stopLocationsGroup.getId().toString(),
List.of(),
ListUtils.ofNullable(stopLocationsGroup.getName()),
List.of(),
stopLocationsGroup.getCoordinate().latitude(),
stopLocationsGroup.getCoordinate().longitude(),
Set.of(),
Set.of()
stopLocationsGroup.getCoordinate().longitude()
)
);

Expand All @@ -131,12 +128,11 @@ public LuceneIndex(TransitService transitService) {
directoryWriter,
StopCluster.class,
stopCluster.primaryId(),
stopCluster.secondaryIds(),
stopCluster.names(),
stopCluster.codes(),
stopCluster.coordinate().lat(),
stopCluster.coordinate().lon(),
stopCluster.modes(),
List.of()
stopCluster.coordinate().lon()
)
);
}
Expand Down Expand Up @@ -187,7 +183,13 @@ private StopCluster toStopCluster(Document document) {
var primaryId = FeedScopedId.parse(document.get(ID));
var primary = stopClusterMapper.toLocation(primaryId);

return new StopCluster(primary, List.of());
var secondaryIds = Arrays
.stream(document.getValues(SECONDARY_IDS))
.map(FeedScopedId::parse)
.map(stopClusterMapper::toLocation)
.toList();

return new StopCluster(primary, secondaryIds);
}

static IndexWriterConfig iwcWithSuggestField(Analyzer analyzer, final Set<String> suggestFields) {
Expand All @@ -211,17 +213,19 @@ private static void addToIndex(
IndexWriter writer,
Class<?> type,
String id,
Collection<String> secondaryIds,
Collection<I18NString> names,
Collection<String> codes,
double latitude,
double longitude,
Collection<String> modes,
Collection<String> agencyIds
double longitude
) {
String typeName = type.getSimpleName();

Document document = new Document();
document.add(new StoredField(ID, id));
for (var secondaryId : secondaryIds) {
document.add(new StoredField(SECONDARY_IDS, secondaryId));
}
document.add(new TextField(TYPE, typeName, Store.YES));
for (var name : names) {
document.add(new TextField(NAME, Objects.toString(name), Store.YES));
Expand All @@ -236,13 +240,6 @@ private static void addToIndex(
document.add(new ContextSuggestField(SUGGEST, code, 1, typeName));
}

for (var mode : modes) {
document.add(new TextField(MODE, mode, Store.YES));
}
for (var aId : agencyIds) {
document.add(new TextField(AGENCY_IDS, aId, Store.YES));
}

try {
writer.addDocument(document);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ record LuceneStopCluster(
String primaryId,
Collection<String> secondaryIds,
Collection<I18NString> names,
Collection<String> modes,
Collection<String> codes,
StopCluster.Coordinate coordinate
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.opentripplanner.framework.collection.ListUtils;
import org.opentripplanner.framework.geometry.WgsCoordinate;
import org.opentripplanner.framework.i18n.I18NString;
Expand Down Expand Up @@ -39,7 +40,7 @@ Iterable<LuceneStopCluster> generateStopClusters(
Collection<StopLocation> stopLocations,
Collection<StopLocationsGroup> stopLocationsGroups
) {
var stops = stopLocations
List<StopLocation> stops = stopLocations
.stream()
// remove stop locations without a parent station
.filter(sl -> sl.getParentStation() == null)
Expand All @@ -48,22 +49,24 @@ Iterable<LuceneStopCluster> generateStopClusters(
.toList();

// if they are very close to each other and have the same name, only one is chosen (at random)
var deduplicatedStops = ListUtils
.distinctByKey(
stops,
sl -> new DeduplicationKey(sl.getName(), sl.getCoordinate().roundToApproximate100m())
var deduplicatedStops = stops
.stream()
.collect(
Collectors.groupingBy(sl ->
new DeduplicationKey(sl.getName(), sl.getCoordinate().roundToApproximate100m())
)
)
.values()
.stream()
.flatMap(s -> this.map(s).stream())
.map(group -> this.map(group).orElse(null))
.filter(Objects::nonNull)
.toList();
var stations = stopLocationsGroups.stream().map(this::map).toList();

return Iterables.concat(deduplicatedStops, stations);
}

LuceneStopCluster map(StopLocationsGroup g) {
var modes = transitService.getModesOfStopLocationsGroup(g).stream().map(Enum::name).toList();

var childStops = g.getChildStops();
var ids = childStops.stream().map(s -> s.getId().toString()).toList();
var childNames = childStops
Expand All @@ -78,32 +81,34 @@ LuceneStopCluster map(StopLocationsGroup g) {
ids,
ListUtils.combine(List.of(g.getName()), childNames),
codes,
modes,
toCoordinate(g.getCoordinate())
);
}

Optional<LuceneStopCluster> map(StopLocation sl) {
Optional<LuceneStopCluster> map(List<StopLocation> stopLocations) {
var primary = stopLocations.getFirst();
var secondaryIds = stopLocations.stream().skip(1).map(sl -> sl.getId().toString()).toList();
var names = stopLocations.stream().map(StopLocation::getName).toList();
var codes = stopLocations.stream().map(StopLocation::getCode).filter(Objects::nonNull).toList();

return Optional
.ofNullable(sl.getName())
.map(name -> {
var modes = transitService.getModesOfStopLocation(sl).stream().map(Enum::name).toList();
return new LuceneStopCluster(
sl.getId().toString(),
List.of(),
List.of(name),
modes,
ListUtils.ofNullable(sl.getCode()),
toCoordinate(sl.getCoordinate())
);
});
.ofNullable(primary.getName())
.map(name ->
new LuceneStopCluster(
primary.getId().toString(),
secondaryIds,
names,
codes,
toCoordinate(primary.getCoordinate())
)
);
}

List<Agency> agenciesForStopLocation(StopLocation stop) {
private List<Agency> agenciesForStopLocation(StopLocation stop) {
return transitService.getRoutesForStop(stop).stream().map(Route::getAgency).distinct().toList();
}

List<Agency> agenciesForStopLocationsGroup(StopLocationsGroup group) {
private List<Agency> agenciesForStopLocationsGroup(StopLocationsGroup group) {
return group
.getChildStops()
.stream()
Expand All @@ -112,28 +117,10 @@ List<Agency> agenciesForStopLocationsGroup(StopLocationsGroup group) {
.toList();
}

private static StopCluster.Coordinate toCoordinate(WgsCoordinate c) {
return new StopCluster.Coordinate(c.latitude(), c.longitude());
}

static StopCluster.Agency toAgency(Agency a) {
return new StopCluster.Agency(a.getId(), a.getName());
}

static StopCluster.FeedPublisher toFeedPublisher(FeedInfo fi) {
if (fi == null) {
return null;
} else {
return new StopCluster.FeedPublisher(fi.getPublisherName());
}
}

StopCluster.Location toLocation(FeedScopedId id) {
var loc = transitService.getStopLocation(id);
if (loc != null) {
var feedPublisher = toFeedPublisher(
transitService.getFeedInfo(id.getFeedId())
);
var feedPublisher = toFeedPublisher(transitService.getFeedInfo(id.getFeedId()));
var modes = transitService.getModesOfStopLocation(loc).stream().map(Enum::name).toList();
var agencies = agenciesForStopLocation(loc)
.stream()
Expand All @@ -150,10 +137,12 @@ StopCluster.Location toLocation(FeedScopedId id) {
);
} else {
var group = transitService.getStopLocationsGroup(id);
var feedPublisher = toFeedPublisher(
transitService.getFeedInfo(id.getFeedId())
);
var modes = transitService.getModesOfStopLocationsGroup(group).stream().map(Enum::name).toList();
var feedPublisher = toFeedPublisher(transitService.getFeedInfo(id.getFeedId()));
var modes = transitService
.getModesOfStopLocationsGroup(group)
.stream()
.map(Enum::name)
.toList();
var agencies = agenciesForStopLocationsGroup(group)
.stream()
.map(StopClusterMapper::toAgency)
Expand All @@ -170,5 +159,21 @@ StopCluster.Location toLocation(FeedScopedId id) {
}
}

private static StopCluster.Coordinate toCoordinate(WgsCoordinate c) {
return new StopCluster.Coordinate(c.latitude(), c.longitude());
}

static StopCluster.Agency toAgency(Agency a) {
return new StopCluster.Agency(a.getId(), a.getName());
}

private static StopCluster.FeedPublisher toFeedPublisher(FeedInfo fi) {
if (fi == null) {
return null;
} else {
return new StopCluster.FeedPublisher(fi.getPublisherName());
}
}

private record DeduplicationKey(I18NString name, WgsCoordinate coordinate) {}
}

0 comments on commit 4205b33

Please sign in to comment.