-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New vectortile layer for Digitransit realtime stops #5743
Changes from 6 commits
dbc00ee
c977425
2e201f7
c38e648
f80544b
dd4db60
816fba9
c148daf
796d542
5120e12
89e82e9
d75df7f
5132bf2
a408f01
e8f9488
b4a0f8e
e6c31e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.opentripplanner.ext.vectortiles.layers.stops; | ||
|
||
import static org.opentripplanner.ext.vectortiles.layers.stops.DigitransitStopPropertyMapper.getKeyValues; | ||
import static org.opentripplanner.ext.vectortiles.layers.stops.DigitransitStopPropertyMapper.getRoutes; | ||
import static org.opentripplanner.ext.vectortiles.layers.stops.DigitransitStopPropertyMapper.getType; | ||
|
||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import org.opentripplanner.apis.support.mapping.PropertyMapper; | ||
import org.opentripplanner.framework.collection.ListUtils; | ||
import org.opentripplanner.framework.i18n.I18NStringMapper; | ||
import org.opentripplanner.inspector.vector.KeyValue; | ||
import org.opentripplanner.transit.model.site.RegularStop; | ||
import org.opentripplanner.transit.service.TransitService; | ||
|
||
public class DigitransitRealtimeStopPropertyMapper extends PropertyMapper<RegularStop> { | ||
|
||
private final TransitService transitService; | ||
private final I18NStringMapper i18NStringMapper; | ||
|
||
public DigitransitRealtimeStopPropertyMapper(TransitService transitService, Locale locale) { | ||
this.transitService = transitService; | ||
this.i18NStringMapper = new I18NStringMapper(locale); | ||
} | ||
|
||
@Override | ||
protected Collection<KeyValue> map(RegularStop stop) { | ||
Instant currentTime = Instant.now(); | ||
boolean noServiceAlert = transitService | ||
.getTransitAlertService() | ||
.getStopAlerts(stop.getId()) | ||
.stream() | ||
.anyMatch(alert -> alert.noServiceOn(currentTime)); | ||
|
||
Collection<KeyValue> sharedKeyValues = getKeyValues( | ||
stop, | ||
i18NStringMapper.mapNonnullToApi(stop.getName()), | ||
i18NStringMapper.mapToApi(stop.getDescription()), | ||
getType(transitService, stop), | ||
getRoutes(transitService, stop) | ||
); | ||
Collection<KeyValue> keyValues = ListUtils.combine( | ||
sharedKeyValues, | ||
List.of(new KeyValue("closedByServiceAlert", noServiceAlert)) | ||
); | ||
return keyValues; | ||
sharhio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ public class DigitransitStopPropertyMapper extends PropertyMapper<RegularStop> { | |
private final TransitService transitService; | ||
private final I18NStringMapper i18NStringMapper; | ||
|
||
private DigitransitStopPropertyMapper(TransitService transitService, Locale locale) { | ||
DigitransitStopPropertyMapper(TransitService transitService, Locale locale) { | ||
this.transitService = transitService; | ||
this.i18NStringMapper = new I18NStringMapper(locale); | ||
} | ||
|
@@ -34,20 +34,39 @@ protected static DigitransitStopPropertyMapper create( | |
|
||
@Override | ||
protected Collection<KeyValue> map(RegularStop stop) { | ||
Collection<TripPattern> patternsForStop = transitService.getPatternsForStop(stop); | ||
return getKeyValues( | ||
stop, | ||
i18NStringMapper.mapNonnullToApi(stop.getName()), | ||
i18NStringMapper.mapToApi(stop.getDescription()), | ||
getType(transitService, stop), | ||
getRoutes(transitService, stop) | ||
); | ||
} | ||
|
||
String type = patternsForStop | ||
.stream() | ||
.map(TripPattern::getMode) | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) | ||
.entrySet() | ||
.stream() | ||
.max(Map.Entry.comparingByValue()) | ||
.map(Map.Entry::getKey) | ||
.map(Enum::name) | ||
.orElse(null); | ||
protected static Collection<KeyValue> getKeyValues( | ||
sharhio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RegularStop stop, | ||
String name, | ||
String description, | ||
String type, | ||
String routes | ||
) { | ||
return List.of( | ||
new KeyValue("gtfsId", stop.getId().toString()), | ||
new KeyValue("name", name), | ||
new KeyValue("code", stop.getCode()), | ||
new KeyValue("platform", stop.getPlatformCode()), | ||
new KeyValue("desc", description), | ||
new KeyValue( | ||
"parentStation", | ||
stop.getParentStation() != null ? stop.getParentStation().getId() : null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. This shouldn't be shared between the mappers as the old one always returned a string. |
||
), | ||
new KeyValue("type", type), | ||
new KeyValue("routes", routes) | ||
); | ||
} | ||
|
||
String routes = JSONArray.toJSONString( | ||
protected static String getRoutes(TransitService transitService, RegularStop stop) { | ||
return JSONArray.toJSONString( | ||
transitService | ||
.getRoutesForStop(stop) | ||
.stream() | ||
|
@@ -58,18 +77,20 @@ protected Collection<KeyValue> map(RegularStop stop) { | |
}) | ||
.toList() | ||
); | ||
return List.of( | ||
new KeyValue("gtfsId", stop.getId().toString()), | ||
new KeyValue("name", i18NStringMapper.mapNonnullToApi(stop.getName())), | ||
new KeyValue("code", stop.getCode()), | ||
new KeyValue("platform", stop.getPlatformCode()), | ||
new KeyValue("desc", i18NStringMapper.mapToApi(stop.getDescription())), | ||
new KeyValue( | ||
"parentStation", | ||
stop.getParentStation() != null ? stop.getParentStation().getId() : "null" | ||
), | ||
new KeyValue("type", type), | ||
new KeyValue("routes", routes) | ||
); | ||
} | ||
|
||
protected static String getType(TransitService transitService, RegularStop stop) { | ||
Collection<TripPattern> patternsForStop = transitService.getPatternsForStop(stop); | ||
|
||
return patternsForStop | ||
.stream() | ||
.map(TripPattern::getMode) | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) | ||
.entrySet() | ||
.stream() | ||
.max(Map.Entry.comparingByValue()) | ||
.map(Map.Entry::getKey) | ||
.map(Enum::name) | ||
.orElse(null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,6 +191,19 @@ public Instant getEffectiveEndDate() { | |
.orElse(null); | ||
} | ||
|
||
/** | ||
* Checks if the alert has a NO_SERVICE alert active at the requested time. | ||
* @param instant | ||
* @return | ||
*/ | ||
public boolean noServiceOn(Instant instant) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I think in English "no service at instant" is more correct. Can you rename the method? |
||
return ( | ||
effect.equals(AlertEffect.NO_SERVICE) && | ||
(getEffectiveStartDate() != null && getEffectiveStartDate().isBefore(instant)) && | ||
(getEffectiveEndDate() == null || getEffectiveEndDate().isAfter(instant)) | ||
); | ||
} | ||
|
||
@Override | ||
public boolean sameAs(@Nonnull TransitAlert other) { | ||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two calls can move to the method by passing the i18nstring mapper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, passed the transitService as well.