Skip to content

Commit

Permalink
Merge pull request #507 from solita-sabinaf/TIS-898/fetch_routing_data
Browse files Browse the repository at this point in the history
Fetcher to for getting a single service link
  • Loading branch information
testower authored Dec 20, 2024
2 parents 8285384 + 26b4bc9 commit 40aaebf
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 4 deletions.
51 changes: 50 additions & 1 deletion src/main/java/no/entur/uttu/graphql/LinesGraphQLSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static graphql.Scalars.GraphQLID;
import static graphql.Scalars.GraphQLInt;
import static graphql.Scalars.GraphQLString;
import static graphql.scalars.ExtendedScalars.GraphQLBigDecimal;
import static graphql.scalars.ExtendedScalars.GraphQLLong;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
Expand Down Expand Up @@ -50,6 +51,7 @@
import no.entur.uttu.graphql.fetchers.DayTypeServiceJourneyCountFetcher;
import no.entur.uttu.graphql.fetchers.ExportedPublicLinesFetcher;
import no.entur.uttu.graphql.model.Organisation;
import no.entur.uttu.graphql.model.ServiceLink;
import no.entur.uttu.graphql.model.StopPlace;
import no.entur.uttu.graphql.scalars.DateScalar;
import no.entur.uttu.graphql.scalars.DateTimeScalar;
Expand Down Expand Up @@ -87,7 +89,6 @@
import org.locationtech.jts.geom.Geometry;
import org.rutebanken.netex.model.AllVehicleModesOfTransportEnumeration;
import org.rutebanken.netex.model.OrganisationTypeEnumeration;
import org.rutebanken.netex.model.Organisation_VersionStructure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -161,6 +162,9 @@ public class LinesGraphQLSchema {
@Autowired
private DataFetcher<List<StopPlace>> stopPlacesFetcher;

@Autowired
private DataFetcher<ServiceLink> routingFetcher;

private <T extends Enum> GraphQLEnumType createEnum(
String name,
T[] values,
Expand Down Expand Up @@ -263,6 +267,8 @@ private <T extends Enum> GraphQLEnumType createEnum(
private GraphQLObjectType stopPlaceObjectType;

private GraphQLObjectType organisationObjectType;
private GraphQLObjectType routeGeometryObjectType;
private GraphQLObjectType serviceLinkObjectType;

private GraphQLArgument idArgument;
private GraphQLArgument idsArgument;
Expand Down Expand Up @@ -913,6 +919,26 @@ private void initCommonTypes() {
.field(newFieldDefinition().name(FIELD_NAME).type(multilingualStringObjectType))
.field(newFieldDefinition().name("type").type(organisationTypeEnum))
.build();

routeGeometryObjectType =
newObject()
.name("RouteGeometry")
.field(
newFieldDefinition()
.name("coordinates")
.type(new GraphQLList(new GraphQLList(GraphQLBigDecimal)))
)
.field(newFieldDefinition().name("distance").type(GraphQLBigDecimal))
.build();

serviceLinkObjectType =
newObject()
.name("ServiceLink")
.field(newFieldDefinition().name("routeGeometry").type(routeGeometryObjectType))
.field(newFieldDefinition().name("quayRefFrom").type(GraphQLString))
.field(newFieldDefinition().name("quayRefTo").type(GraphQLString))
.field(newFieldDefinition().name("serviceLinkRef").type(GraphQLString))
.build();
}

private GraphQLObjectType createQueryObject() {
Expand Down Expand Up @@ -1102,6 +1128,29 @@ private GraphQLObjectType createQueryObject() {
.description("List all organisations")
.dataFetcher(organisationsFetcher)
)
.field(
newFieldDefinition()
.type(serviceLinkObjectType)
.name("serviceLink")
.argument(
GraphQLArgument
.newArgument()
.name("quayRefFrom")
.type(GraphQLString)
.description("First stop point's id")
.build()
)
.argument(
GraphQLArgument
.newArgument()
.name("quayRefTo")
.type(GraphQLString)
.description("Second stop point's id")
.build()
)
.description("Fetch service link containing route geometry")
.dataFetcher(routingFetcher)
)
.build();
}

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package no.entur.uttu.graphql.fetchers;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.List;
import java.util.Optional;
import no.entur.uttu.graphql.model.ServiceLink;
import no.entur.uttu.routing.RouteGeometry;
import no.entur.uttu.routing.RoutingService;
import no.entur.uttu.stopplace.spi.StopPlaceRegistry;
import org.rutebanken.netex.model.Quay;
import org.rutebanken.netex.model.StopPlace;
import org.springframework.stereotype.Service;

@Service("routingFetcher")
public class RoutingFetcher implements DataFetcher<ServiceLink> {

private final RoutingService routingService;
private final StopPlaceRegistry stopPlaceRegistry;

public RoutingFetcher(
RoutingService routingService,
StopPlaceRegistry stopPlaceRegistry
) {
this.routingService = routingService;
this.stopPlaceRegistry = stopPlaceRegistry;
}

@Override
public ServiceLink get(DataFetchingEnvironment environment) {
String quayRefFrom = environment.getArgument("quayRefFrom");
String quayRefTo = environment.getArgument("quayRefTo");
Quay quayFrom = getQuay(quayRefFrom);
Quay quayTo = getQuay(quayRefTo);

if (quayFrom == null || quayTo == null) {
return new ServiceLink(quayRefFrom + "_" + quayRefTo, null, null, null);
}

RouteGeometry routeGeometry = routingService.getRouteGeometry(
quayFrom.getCentroid().getLocation().getLongitude(),
quayFrom.getCentroid().getLocation().getLatitude(),
quayTo.getCentroid().getLocation().getLongitude(),
quayTo.getCentroid().getLocation().getLatitude()
);

return new ServiceLink(
quayRefFrom + "_" + quayRefTo,
quayRefFrom,
quayRefTo,
routeGeometry
);
}

Quay getQuay(String quayRef) {
if (quayRef == null) {
return null;
}
Optional<StopPlace> stopPlaceOptional = stopPlaceRegistry.getStopPlaceByQuayRef(
quayRef
);
if (stopPlaceOptional.isEmpty()) {
return null;
}
StopPlace stopPlaceFrom = stopPlaceOptional.get();
List<Quay> stopPlaceFromQuays = stopPlaceFrom
.getQuays()
.getQuayRefOrQuay()
.stream()
.map(jaxbElement -> (org.rutebanken.netex.model.Quay) jaxbElement.getValue())
.toList();
return stopPlaceFromQuays
.stream()
.filter(quay -> quay.getId().equals(quayRef))
.toList()
.get(0);
}
}
10 changes: 10 additions & 0 deletions src/main/java/no/entur/uttu/graphql/model/ServiceLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package no.entur.uttu.graphql.model;

import no.entur.uttu.routing.RouteGeometry;

public record ServiceLink(
String serviceLinkRef,
String quayRefFrom,
String quayRefTo,
RouteGeometry routeGeometry
) {}
20 changes: 17 additions & 3 deletions src/main/java/no/entur/uttu/routing/osrm/OsrmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ public boolean isEnabled() {
return osrmApiEndpoint != null && !osrmApiEndpoint.isBlank();
}

public RouteGeometry getRouteGeometry(
private MutableRequest getRoutingRequest(
BigDecimal longitudeFrom,
BigDecimal latitudeFrom,
BigDecimal longitudeTo,
BigDecimal latitudeTo
) {
List<List<BigDecimal>> routeCoordinates = new ArrayList<>();
MutableRequest request = MutableRequest
return MutableRequest
.GET(
osrmApiEndpoint +
"/route/v1/driving/" +
Expand All @@ -78,6 +77,21 @@ public RouteGeometry getRouteGeometry(
"?alternatives=false&steps=false&overview=full&geometries=geojson"
)
.header("Content-Type", "application/json");
}

public RouteGeometry getRouteGeometry(
BigDecimal longitudeFrom,
BigDecimal latitudeFrom,
BigDecimal longitudeTo,
BigDecimal latitudeTo
) {
List<List<BigDecimal>> routeCoordinates = new ArrayList<>();
MutableRequest request = getRoutingRequest(
longitudeFrom,
latitudeFrom,
longitudeTo,
latitudeTo
);
try {
HttpResponse<String> response = httpClient.send(
request,
Expand Down

0 comments on commit 40aaebf

Please sign in to comment.