Skip to content

Commit 079a45f

Browse files
Merge pull request #13 from Contargo/routing-coordinates
Add API for routing with coordinates
2 parents 41eaf60 + ed45fab commit 079a45f

File tree

4 files changed

+352
-4
lines changed

4 files changed

+352
-4
lines changed

src/main/java/net/contargo/iris/route/RouteInformation.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import net.contargo.iris.GeoLocation;
44
import net.contargo.iris.container.ContainerType;
55

6+
import java.util.Objects;
7+
68

79
/**
810
* Encapsulates detail information that are needed to compute a {@link net.contargo.iris.route.Route}.
@@ -96,4 +98,28 @@ public void setRouteCombo(RouteCombo routeCombo) {
9698

9799
this.routeCombo = routeCombo;
98100
}
101+
102+
103+
@Override
104+
public boolean equals(Object o) {
105+
106+
if (this == o)
107+
return true;
108+
109+
if (o == null || getClass() != o.getClass())
110+
return false;
111+
112+
RouteInformation that = (RouteInformation) o;
113+
114+
return Objects.equals(destination, that.destination) && product == that.product
115+
&& containerType == that.containerType && routeDirection == that.routeDirection
116+
&& routeCombo == that.routeCombo;
117+
}
118+
119+
120+
@Override
121+
public int hashCode() {
122+
123+
return Objects.hash(destination, product, containerType, routeDirection, routeCombo);
124+
}
99125
}

src/main/java/net/contargo/iris/route/api/RoutesApiController.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import com.wordnik.swagger.annotations.ApiOperation;
44

5+
import net.contargo.iris.GeoLocation;
6+
import net.contargo.iris.address.dto.GeoLocationDto;
57
import net.contargo.iris.api.ControllerConstants;
8+
import net.contargo.iris.connection.dto.RouteDataDto;
69
import net.contargo.iris.connection.dto.RouteDto;
10+
import net.contargo.iris.connection.dto.RoutePartDto;
711
import net.contargo.iris.connection.dto.SeaportConnectionRoutesDtoService;
812
import net.contargo.iris.container.ContainerType;
913
import net.contargo.iris.route.RouteCombo;
1014
import net.contargo.iris.route.RouteInformation;
15+
import net.contargo.iris.route.dto.EnricherDtoService;
16+
import net.contargo.iris.route.dto.RoutingResultDto;
1117
import net.contargo.iris.route.service.RouteUrlSerializationService;
1218
import net.contargo.iris.seaport.dto.SeaportDto;
1319
import net.contargo.iris.seaport.dto.SeaportDtoService;
@@ -22,23 +28,39 @@
2228
import org.springframework.web.bind.annotation.PathVariable;
2329
import org.springframework.web.bind.annotation.RequestMapping;
2430
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.ResponseBody;
2532

2633
import java.lang.invoke.MethodHandles;
2734

35+
import java.math.BigDecimal;
2836
import java.math.BigInteger;
2937

38+
import java.util.ArrayList;
39+
import java.util.Collection;
3040
import java.util.List;
3141

42+
import static net.contargo.iris.container.ContainerType.TWENTY_LIGHT;
43+
import static net.contargo.iris.route.RouteCombo.WATERWAY;
44+
import static net.contargo.iris.route.RouteDirection.EXPORT;
45+
import static net.contargo.iris.route.RouteProduct.ONEWAY;
46+
import static net.contargo.iris.route.RouteType.BARGE;
47+
3248
import static org.slf4j.LoggerFactory.getLogger;
3349

3450
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
3551
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
3652

3753
import static org.springframework.web.bind.annotation.RequestMethod.GET;
3854

55+
import static java.math.BigDecimal.ZERO;
56+
57+
import static java.util.Comparator.comparing;
58+
import static java.util.stream.Collectors.toList;
59+
3960

4061
/**
4162
* @author Sandra Thieme - thieme@synyx.de
63+
* @author Ben Antony - antony@synyx.de
4264
*/
4365
@Controller
4466
public class RoutesApiController {
@@ -50,15 +72,17 @@ public class RoutesApiController {
5072
private final SeaportDtoService seaportDtoService;
5173
private final SeaportConnectionRoutesDtoService seaportConnectionRoutesDtoService;
5274
private final RouteUrlSerializationService routeUrlSerializationService;
75+
private final EnricherDtoService enricherDtoService;
5376

5477
@Autowired
5578
public RoutesApiController(SeaportDtoService seaportDtoService,
5679
SeaportConnectionRoutesDtoService seaportConnectionRoutesDtoService,
57-
RouteUrlSerializationService routeUrlSerializationService) {
80+
RouteUrlSerializationService routeUrlSerializationService, EnricherDtoService enricherDtoService) {
5881

5982
this.seaportDtoService = seaportDtoService;
6083
this.seaportConnectionRoutesDtoService = seaportConnectionRoutesDtoService;
6184
this.routeUrlSerializationService = routeUrlSerializationService;
85+
this.enricherDtoService = enricherDtoService;
6286
}
6387

6488
/**
@@ -124,4 +148,48 @@ public RoutesResponse getRoutes(@PathVariable("seaportuid") BigInteger seaportUi
124148

125149
return response;
126150
}
151+
152+
153+
@ApiOperation(value = "Returns a list of all possible routes to a location.")
154+
@RequestMapping(value = "/routes", method = GET)
155+
@ResponseBody
156+
public List<RoutingResultDto> getRoutesWithCoordinates(@RequestParam(value = "lat") double latitude,
157+
@RequestParam(value = "lon") double longitude) {
158+
159+
RouteInformation routeInformation = new RouteInformation(new GeoLocation(BigDecimal.valueOf(latitude),
160+
BigDecimal.valueOf(longitude)), ONEWAY, TWENTY_LIGHT, EXPORT, WATERWAY);
161+
162+
return seaportDtoService.getAllActive()
163+
.stream()
164+
.map(s -> seaportConnectionRoutesDtoService.getAvailableSeaportConnectionRoutes(s, routeInformation))
165+
.flatMap(Collection::stream)
166+
.map(enricherDtoService::enrich)
167+
.map(this::toRoutingResultDto)
168+
.sorted(comparing(RoutingResultDto::getDistance))
169+
.collect(toList());
170+
}
171+
172+
173+
private RoutingResultDto toRoutingResultDto(RouteDto routeDto) {
174+
175+
RouteDataDto data = routeDto.getData();
176+
BigDecimal bargeDistance = data.getParts().stream().filter(p -> p.getRouteType() == BARGE)
177+
.map(p -> p.getData().getBargeDieselDistance())
178+
.reduce(ZERO, BigDecimal::add);
179+
180+
List<GeoLocationDto> stops = new ArrayList<>();
181+
stops.add(data.getParts().get(0).getOrigin());
182+
stops.addAll(data.getParts().stream().map(RoutePartDto::getDestination).collect(toList()));
183+
184+
return new RoutingResultDto.Builder().withCo2(data.getCo2())
185+
.withCo2DirectTruck(data.getCo2DirectTruck())
186+
.withDistance(data.getTotalDistance())
187+
.withDuration(data.getTotalDuration())
188+
.withOnewayTruckDistance(data.getTotalOnewayTruckDistance())
189+
.withRealTollDistance(data.getTotalRealTollDistance())
190+
.withTollDistance(data.getTotalTollDistance())
191+
.withBargeDistance(bargeDistance)
192+
.withStops(stops)
193+
.build();
194+
}
127195
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package net.contargo.iris.route.dto;
2+
3+
import net.contargo.iris.address.dto.GeoLocationDto;
4+
5+
import java.math.BigDecimal;
6+
7+
import java.util.List;
8+
9+
10+
/**
11+
* @author Ben Antony - antony@synyx.de
12+
*/
13+
public class RoutingResultDto {
14+
15+
private final BigDecimal co2;
16+
private final BigDecimal co2DirectTruck;
17+
private final BigDecimal distance;
18+
private final BigDecimal bargeDistance;
19+
private final BigDecimal onewayTruckDistance;
20+
private final BigDecimal realTollDistance;
21+
private final BigDecimal tollDistance;
22+
private final BigDecimal duration;
23+
private final List<GeoLocationDto> stops;
24+
25+
private RoutingResultDto(Builder builder) {
26+
27+
this.co2 = builder.co2;
28+
this.co2DirectTruck = builder.co2DirectTruck;
29+
this.distance = builder.distance;
30+
this.bargeDistance = builder.bargeDistance;
31+
this.onewayTruckDistance = builder.onewayTruckDistance;
32+
this.realTollDistance = builder.realTollDistance;
33+
this.tollDistance = builder.tollDistance;
34+
this.duration = builder.duration;
35+
this.stops = builder.stops;
36+
}
37+
38+
public BigDecimal getCo2() {
39+
40+
return co2;
41+
}
42+
43+
44+
public BigDecimal getCo2DirectTruck() {
45+
46+
return co2DirectTruck;
47+
}
48+
49+
50+
public BigDecimal getDistance() {
51+
52+
return distance;
53+
}
54+
55+
56+
public BigDecimal getBargeDistance() {
57+
58+
return bargeDistance;
59+
}
60+
61+
62+
public BigDecimal getOnewayTruckDistance() {
63+
64+
return onewayTruckDistance;
65+
}
66+
67+
68+
public BigDecimal getRealTollDistance() {
69+
70+
return realTollDistance;
71+
}
72+
73+
74+
public BigDecimal getTollDistance() {
75+
76+
return tollDistance;
77+
}
78+
79+
80+
public BigDecimal getDuration() {
81+
82+
return duration;
83+
}
84+
85+
86+
public List<GeoLocationDto> getStops() {
87+
88+
return stops;
89+
}
90+
91+
public static class Builder {
92+
93+
private BigDecimal co2;
94+
private BigDecimal co2DirectTruck;
95+
private BigDecimal distance;
96+
private BigDecimal bargeDistance;
97+
private BigDecimal onewayTruckDistance;
98+
private BigDecimal realTollDistance;
99+
private BigDecimal tollDistance;
100+
private BigDecimal duration;
101+
private List<GeoLocationDto> stops;
102+
103+
public RoutingResultDto build() {
104+
105+
return new RoutingResultDto(this);
106+
}
107+
108+
109+
public Builder withCo2(BigDecimal co2) {
110+
111+
this.co2 = co2;
112+
113+
return this;
114+
}
115+
116+
117+
public Builder withCo2DirectTruck(BigDecimal co2DirectTruck) {
118+
119+
this.co2DirectTruck = co2DirectTruck;
120+
121+
return this;
122+
}
123+
124+
125+
public Builder withDistance(BigDecimal distance) {
126+
127+
this.distance = distance;
128+
129+
return this;
130+
}
131+
132+
133+
public Builder withBargeDistance(BigDecimal bargeDistance) {
134+
135+
this.bargeDistance = bargeDistance;
136+
137+
return this;
138+
}
139+
140+
141+
public Builder withOnewayTruckDistance(BigDecimal onewayTruckDistance) {
142+
143+
this.onewayTruckDistance = onewayTruckDistance;
144+
145+
return this;
146+
}
147+
148+
149+
public Builder withRealTollDistance(BigDecimal realTollDistance) {
150+
151+
this.realTollDistance = realTollDistance;
152+
153+
return this;
154+
}
155+
156+
157+
public Builder withTollDistance(BigDecimal tollDistance) {
158+
159+
this.tollDistance = tollDistance;
160+
161+
return this;
162+
}
163+
164+
165+
public Builder withDuration(BigDecimal duration) {
166+
167+
this.duration = duration;
168+
169+
return this;
170+
}
171+
172+
173+
public Builder withStops(List<GeoLocationDto> stops) {
174+
175+
this.stops = stops;
176+
177+
return this;
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)