Skip to content

Commit 59786bb

Browse files
Merge pull request #7054 from ibi-group/vector-fuel
Add rental vehicle's fuel percentage and propulsion type to vector tiles API
2 parents 266653b + 54152a3 commit 59786bb

File tree

6 files changed

+93
-46
lines changed

6 files changed

+93
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.opentripplanner.ext.vectortiles.layers.vehiclerental.mapper;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.opentripplanner.street.model.RentalFormFactor.BICYCLE;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import org.junit.jupiter.api.Test;
10+
import org.opentripplanner.framework.i18n.I18NString;
11+
import org.opentripplanner.service.vehiclerental.model.RentalVehicleFuel;
12+
import org.opentripplanner.service.vehiclerental.model.RentalVehicleTypeFactory;
13+
import org.opentripplanner.service.vehiclerental.model.VehicleRentalVehicle;
14+
import org.opentripplanner.transit.model.basic.Ratio;
15+
import org.opentripplanner.transit.model.framework.FeedScopedId;
16+
17+
class DigitransitRentalVehiclePropertyMapperTest {
18+
19+
private static final String NAME = "a rental";
20+
21+
@Test
22+
void noFuel() {
23+
var mapper = new DigitransitRentalVehiclePropertyMapper();
24+
var vehicle = builder().build();
25+
26+
Map<String, Object> map = new HashMap<>();
27+
mapper.map(vehicle).forEach(o -> map.put(o.key(), o.value()));
28+
29+
assertEquals("A:B", map.get("id"));
30+
assertEquals("BICYCLE", map.get("formFactor"));
31+
assertEquals("HUMAN", map.get("propulsionType"));
32+
assertNull(map.get("fuelPercentage"));
33+
assertEquals("A", map.get("network"));
34+
assertEquals(true, map.get("pickupAllowed"));
35+
assertNull(map.get("name"));
36+
}
37+
38+
@Test
39+
void fuelPercentage() {
40+
var mapper = new DigitransitRentalVehiclePropertyMapper();
41+
var vehicle = builder()
42+
.withFuel(RentalVehicleFuel.of().withPercent(Ratio.of(0.3)).build())
43+
.build();
44+
45+
Map<String, Object> map = new HashMap<>();
46+
mapper.map(vehicle).forEach(o -> map.put(o.key(), o.value()));
47+
48+
assertEquals(0.3, map.get("fuelPercentage"));
49+
}
50+
51+
private static VehicleRentalVehicle.Builder builder() {
52+
return VehicleRentalVehicle.of()
53+
.withId(new FeedScopedId("A", "B"))
54+
.withLatitude(1)
55+
.withLongitude(2)
56+
.withName(I18NString.of(NAME))
57+
.withVehicleType(RentalVehicleTypeFactory.vehicleType(BICYCLE));
58+
}
59+
}
Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.opentripplanner.service.vehiclerental.model.RentalVehicleTypeFactory.vehicleType;
56
import static org.opentripplanner.street.model.RentalFormFactor.BICYCLE;
67
import static org.opentripplanner.street.model.RentalFormFactor.SCOOTER;
78

@@ -14,37 +15,15 @@
1415
import org.opentripplanner.framework.i18n.TranslatedString;
1516
import org.opentripplanner.service.vehiclerental.model.RentalVehicleType;
1617
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStation;
17-
import org.opentripplanner.service.vehiclerental.model.VehicleRentalVehicle;
1818
import org.opentripplanner.street.model.RentalFormFactor;
1919
import org.opentripplanner.transit.model.framework.FeedScopedId;
2020

21-
public class VehicleRentalLayerTest {
21+
class DigitransitVehicleRentalStationPropertyMapperTest {
2222

23-
public static final String NAME = "a rental";
23+
private static final String NAME = "a rental";
2424

2525
@Test
26-
public void floatingVehicle() {
27-
var mapper = new DigitransitRentalVehiclePropertyMapper();
28-
var vehicle = VehicleRentalVehicle.of()
29-
.withId(new FeedScopedId("A", "B"))
30-
.withLatitude(1)
31-
.withLongitude(2)
32-
.withName(new NonLocalizedString(NAME))
33-
.withVehicleType(vehicleType(BICYCLE))
34-
.build();
35-
36-
Map<String, Object> map = new HashMap<>();
37-
mapper.map(vehicle).forEach(o -> map.put(o.key(), o.value()));
38-
39-
assertEquals("A:B", map.get("id"));
40-
assertEquals("BICYCLE", map.get("formFactor"));
41-
assertEquals("A", map.get("network"));
42-
assertEquals(true, map.get("pickupAllowed"));
43-
assertNull(map.get("name"));
44-
}
45-
46-
@Test
47-
public void station() {
26+
void station() {
4827
var mapper = new DigitransitVehicleRentalStationPropertyMapper(new Locale("en-US"));
4928
var station = VehicleRentalStation.of()
5029
.withId(new FeedScopedId("A", "B"))
@@ -64,7 +43,7 @@ public void station() {
6443
}
6544

6645
@Test
67-
public void stationWithTranslations() {
46+
void stationWithTranslations() {
6847
var mapper = new DigitransitVehicleRentalStationPropertyMapper(new Locale("de"));
6948
var germanName = "nameDE";
7049
var station = VehicleRentalStation.of()
@@ -92,7 +71,7 @@ public void stationWithTranslations() {
9271
}
9372

9473
@Test
95-
public void realtimeStation() {
74+
void realtimeStation() {
9675
var mapper = new DigitransitRealtimeVehicleRentalStationPropertyMapper(new Locale("en-US"));
9776
var station = VehicleRentalStation.of()
9877
.withId(new FeedScopedId("A", "B"))
@@ -117,14 +96,4 @@ public void realtimeStation() {
11796
assertEquals(8, map.get("vehiclesAvailable"));
11897
assertEquals(3, map.get("spacesAvailable"));
11998
}
120-
121-
private static RentalVehicleType vehicleType(RentalFormFactor formFactor) {
122-
return RentalVehicleType.of()
123-
.withId(new FeedScopedId("1", formFactor.name()))
124-
.withName(I18NString.of("bicycle"))
125-
.withFormFactor(formFactor)
126-
.withPropulsionType(RentalVehicleType.PropulsionType.HUMAN)
127-
.withMaxRangeMeters(1000d)
128-
.build();
129-
}
13099
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.opentripplanner.ext.vectortiles.layers.vehiclerental.mapper;
22

33
import static org.opentripplanner.ext.vectortiles.layers.vehiclerental.mapper.DigitransitVehicleRentalStationPropertyMapper.getFeedScopedIdAndNetwork;
4+
import static org.opentripplanner.inspector.vector.KeyValue.kv;
45

56
import java.util.ArrayList;
67
import java.util.Collection;
@@ -11,11 +12,15 @@
1112
public class DigitransitRentalVehiclePropertyMapper extends PropertyMapper<VehicleRentalVehicle> {
1213

1314
@Override
14-
protected Collection<KeyValue> map(VehicleRentalVehicle place) {
15+
protected Collection<KeyValue> map(VehicleRentalVehicle vehicle) {
1516
var items = new ArrayList<KeyValue>();
16-
items.addAll(getFeedScopedIdAndNetwork(place));
17-
items.add(new KeyValue("formFactor", place.vehicleType().formFactor().toString()));
18-
items.add(new KeyValue("pickupAllowed", place.isAllowPickup()));
17+
items.addAll(getFeedScopedIdAndNetwork(vehicle));
18+
items.add(kv("formFactor", vehicle.vehicleType().formFactor()));
19+
items.add(kv("propulsionType", vehicle.vehicleType().propulsionType()));
20+
if (vehicle.fuel() != null && vehicle.fuel().percent() != null) {
21+
items.add(kv("fuelPercentage", vehicle.fuel().percent().asDouble()));
22+
}
23+
items.add(kv("pickupAllowed", vehicle.isAllowPickup()));
1924
return items;
2025
}
2126
}

application/src/main/java/org/opentripplanner/apis/transmodel/model/stop/RentalVehicleType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static GraphQLObjectType create(
6161
.name("currentRangeMeters")
6262
.type(Scalars.GraphQLFloat)
6363
.dataFetcher(environment ->
64-
((VehicleRentalVehicle) environment.getSource()).getFuel().range()
64+
((VehicleRentalVehicle) environment.getSource()).fuel().range()
6565
)
6666
.build()
6767
)

application/src/main/java/org/opentripplanner/service/vehiclerental/model/VehicleRentalVehicle.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ public VehicleRentalSystem vehicleRentalSystem() {
196196
return system;
197197
}
198198

199-
public RentalVehicleFuel getFuel() {
200-
return fuel;
201-
}
202-
203199
public OffsetDateTime availableUntil() {
204200
return availableUntil;
205201
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.opentripplanner.service.vehiclerental.model;
2+
3+
import org.opentripplanner.framework.i18n.I18NString;
4+
import org.opentripplanner.street.model.RentalFormFactor;
5+
import org.opentripplanner.transit.model.framework.FeedScopedId;
6+
7+
public class RentalVehicleTypeFactory {
8+
9+
public static RentalVehicleType vehicleType(RentalFormFactor formFactor) {
10+
return RentalVehicleType.of()
11+
.withId(new FeedScopedId("1", formFactor.name()))
12+
.withName(I18NString.of("bicycle"))
13+
.withFormFactor(formFactor)
14+
.withPropulsionType(RentalVehicleType.PropulsionType.HUMAN)
15+
.withMaxRangeMeters(1000d)
16+
.build();
17+
}
18+
}

0 commit comments

Comments
 (0)