JSONSegment.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 io.swagger.v3.oas.annotations.extensions.Extension;
  19. import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
  20. import io.swagger.v3.oas.annotations.media.Schema;
  21. import org.heigit.ors.api.requests.routing.RouteRequest;
  22. import org.heigit.ors.api.APIEnums;
  23. import org.heigit.ors.routing.RouteSegment;
  24. import org.heigit.ors.routing.RouteStep;
  25. import org.heigit.ors.util.FormatUtility;

  26. import java.util.ArrayList;
  27. import java.util.List;

  28. @Schema(description = "List containing the segments and its correspoding steps which make up the route.")
  29. @JsonInclude(JsonInclude.Include.NON_DEFAULT)
  30. public class JSONSegment {
  31.     @Schema(description = "Contains the distance of the segment in specified units.", example = "253")
  32.     @JsonProperty("distance")
  33.     @JsonInclude()
  34.     private final Double distance;
  35.     @Schema(description = "Contains the duration of the segment in seconds.", example = "37.7")
  36.     @JsonProperty("duration")
  37.     @JsonInclude()
  38.     private final Double duration;
  39.     @Schema(description = "List containing the specific steps the segment consists of.")
  40.     @JsonProperty("steps")
  41.     @JsonInclude()
  42.     private final List<JSONStep> steps;
  43.     @Schema(description = "Contains the deviation compared to a straight line that would have the factor `1`. Double the Distance would be a `2`.",
  44.             extensions = {@Extension(name = "validWhen", properties = {
  45.                     @ExtensionProperty(name = "ref", value = "attributes"),
  46.                     @ExtensionProperty(name = "valueContains", value = "detourfactor")}
  47.             )}, example = "0.5")
  48.     @JsonProperty("detourfactor")
  49.     private Double detourFactor;
  50.     @Schema(description = "Contains the proportion of the route in percent.",
  51.             extensions = {@Extension(name = "validWhen", properties = {
  52.                     @ExtensionProperty(name = "ref", value = "attributes"),
  53.                     @ExtensionProperty(name = "valueContains", value = "percentage")}
  54.             )}, example = "43.2")
  55.     @JsonProperty("percentage")
  56.     private Double percentage;
  57.     @Schema(description = "Contains the average speed of this segment in km/h.",
  58.             extensions = {@Extension(name = "validWhen", properties = {
  59.                     @ExtensionProperty(name = "ref", value = "attributes"),
  60.                     @ExtensionProperty(name = "valueContains", value = "avgspeed")}
  61.             )}, example = "56.3")
  62.     @JsonProperty("avgspeed")
  63.     private Double averageSpeed;
  64.     @Schema(description = " Contains ascent of this segment in metres.",
  65.             extensions = {@Extension(name = "validWhen", properties = {
  66.                     @ExtensionProperty(name = "ref", value = "elevation"),
  67.                     @ExtensionProperty(name = "value", value = "true", parseValue = true)}
  68.             )}, example = "56.3")
  69.     @JsonProperty("ascent")
  70.     private Double ascent;
  71.     @Schema(description = "Contains descent of this segment in metres.",
  72.             example = "45.2")
  73.     @JsonProperty("descent")
  74.     private Double descent;

  75.     public JSONSegment(RouteSegment routeSegment, RouteRequest request, double routeLength) {
  76.         this.distance = routeSegment.getDistance();
  77.         this.duration = routeSegment.getDuration();
  78.         this.detourFactor = routeSegment.getDetourFactor();
  79.         if (request.hasUseElevation() && request.getUseElevation()) {
  80.             this.ascent = routeSegment.getAscent();
  81.             this.descent = routeSegment.getDescent();
  82.         }
  83.         steps = new ArrayList<>();
  84.         for (RouteStep routeStep : routeSegment.getSteps()) {
  85.             steps.add(new JSONStep(routeStep));
  86.         }

  87.         if (request.hasAttributes()) {
  88.             APIEnums.Attributes[] attributes = request.getAttributes();
  89.             for (APIEnums.Attributes attr : attributes) {
  90.                 switch (attr) {
  91.                     case DETOUR_FACTOR -> detourFactor = routeSegment.getDetourFactor();
  92.                     case AVERAGE_SPEED -> {
  93.                         double distFactor = (!request.hasUnits() || request.getUnits() == APIEnums.Units.METRES) ? 1000 : 1;
  94.                         averageSpeed = FormatUtility.roundToDecimals(routeSegment.getDistance() / distFactor / (routeSegment.getDuration() / 3600), 2);
  95.                     }
  96.                     case ROUTE_PERCENTAGE ->
  97.                             percentage = FormatUtility.roundToDecimals(routeSegment.getDistance() * 100 / routeLength, 2);
  98.                 }
  99.             }
  100.         }
  101.     }

  102.     public Double getDistance() {
  103.         return distance;
  104.     }

  105.     public Double getDuration() {
  106.         return duration;
  107.     }

  108.     public Double getDetourFactor() {
  109.         return detourFactor;
  110.     }

  111.     public Double getAscent() {
  112.         return ascent;
  113.     }

  114.     public Double getDescent() {
  115.         return descent;
  116.     }

  117.     public List<JSONStep> getSteps() {
  118.         return steps;
  119.     }

  120.     public Double getPercentage() {
  121.         return percentage;
  122.     }

  123.     public Double getAverageSpeed() {
  124.         return averageSpeed;
  125.     }
  126. }