JSONRouteResponse.java

  1. /*
  2.  * This file is part of Openrouteservice.
  3.  *
  4.  * Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  5.  * GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  6.  * of the License, or (at your option) any later version.
  7.  *
  8.  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  9.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10.  * See the GNU Lesser General Public License for more details.
  11.  *
  12.  * You should have received a copy of the GNU Lesser General Public License along with this library;
  13.  * if not, see <https://www.gnu.org/licenses/>.
  14.  */

  15. package org.heigit.ors.api.responses.routing.json;

  16. import com.fasterxml.jackson.annotation.JsonInclude;
  17. import com.fasterxml.jackson.annotation.JsonProperty;
  18. import com.graphhopper.util.shapes.BBox;
  19. import io.swagger.v3.oas.annotations.media.Schema;
  20. import org.heigit.ors.api.EndpointsProperties;
  21. import org.heigit.ors.api.SystemMessageProperties;
  22. import org.heigit.ors.api.requests.routing.RouteRequest;
  23. import org.heigit.ors.api.responses.common.boundingbox.BoundingBoxFactory;
  24. import org.heigit.ors.api.responses.routing.RouteResponse;
  25. import org.heigit.ors.api.responses.routing.RouteResponseInfo;
  26. import org.heigit.ors.exceptions.StatusCodeException;
  27. import org.heigit.ors.routing.RouteResult;
  28. import org.heigit.ors.util.GeomUtility;

  29. import java.util.ArrayList;
  30. import java.util.List;

  31. @Schema(name = "JSONRouteResponse")
  32. public class JSONRouteResponse extends RouteResponse {
  33.     public JSONRouteResponse(RouteResult[] routeResults, RouteRequest request, SystemMessageProperties systemMessageProperties, EndpointsProperties endpointsProperties) throws StatusCodeException {
  34.         super(request, systemMessageProperties, endpointsProperties);

  35.         this.routeResults = new ArrayList<>();

  36.         List<BBox> bboxes = new ArrayList<>();
  37.         for (RouteResult result : routeResults) {
  38.             this.routeResults.add(new JSONIndividualRouteResponse(result, request));
  39.             bboxes.add(result.getSummary().getBBox());
  40.             responseInformation.setGraphDate(result.getGraphDate());
  41.         }

  42.         BBox bounding = GeomUtility.generateBoundingFromMultiple(bboxes.toArray(new BBox[0]));
  43.         bbox = BoundingBoxFactory.constructBoundingBox(bounding, request);
  44.     }

  45.     @JsonProperty("routes")
  46.     @Schema(description = "A list of routes returned from the request")
  47.     public JSONIndividualRouteResponse[] getRoutes() {
  48.         return routeResults.toArray(new JSONIndividualRouteResponse[0]);
  49.     }

  50.     @JsonProperty("bbox")
  51.     @JsonInclude(JsonInclude.Include.NON_EMPTY)
  52.     @Schema(description = "Bounding box that covers all returned routes", example = "[49.414057, 8.680894, 49.420514, 8.690123]")
  53.     public double[] getBBoxsArray() {
  54.         return bbox.getAsArray();
  55.     }

  56.     @JsonProperty("metadata")
  57.     @Schema(description = "Information about the service and request")
  58.     public RouteResponseInfo getInfo() {
  59.         return responseInformation;
  60.     }
  61. }