GPXRouteElement.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.gpx;

  16. import io.swagger.v3.oas.annotations.media.Schema;
  17. import jakarta.xml.bind.annotation.XmlElement;
  18. import jakarta.xml.bind.annotation.XmlRootElement;
  19. import org.heigit.ors.routing.RouteResult;
  20. import org.heigit.ors.routing.RouteSegment;
  21. import org.heigit.ors.routing.RouteStep;
  22. import org.locationtech.jts.geom.Coordinate;

  23. import java.util.ArrayList;
  24. import java.util.List;

  25. @XmlRootElement(name = "rte")
  26. @Schema(name = "rte")
  27. public class GPXRouteElement {
  28.     @XmlElement(name = "rtept")
  29.     List<GPXRoutePointElement> routePoints;
  30.     @XmlElement(name = "extensions")
  31.     GPXRouteExtensions extensions;

  32.     public GPXRouteElement() {
  33.     }

  34.     public GPXRouteElement(RouteResult result) {
  35.         routePoints = new ArrayList<>();
  36.         Coordinate[] routeCoordinates = result.getGeometry();
  37.         List<RouteSegment> segments = result.getSegments();
  38.         List<RouteStep> steps = new ArrayList<>();

  39.         for (RouteSegment segment : segments) {
  40.             steps.addAll(segment.getSteps());
  41.         }

  42.         for (int i = 0; i < steps.size(); i++) {
  43.             RouteStep step = steps.get(i);
  44.             int coordStartId = step.getWayPoints()[0];
  45.             int coordEndId = step.getWayPoints()[1];
  46.             int coordinateId = coordStartId;
  47.             while (coordinateId >= coordStartId && coordinateId <= coordEndId) {
  48.                 if (coordStartId == coordEndId) {
  49.                     Coordinate c = routeCoordinates[coordinateId];
  50.                     routePoints.add(new GPXRoutePointElement(step, c.x, c.y, c.z, i));
  51.                     break;
  52.                 } else if (coordinateId < coordEndId) {
  53.                     Coordinate c = routeCoordinates[coordinateId];
  54.                     routePoints.add(new GPXRoutePointElement(step, c.x, c.y, c.z, i));
  55.                 }
  56.                 coordinateId++;
  57.             }
  58.         }

  59.         // it may be the case that we did not ask for instructions so there will be no steps
  60.         if (steps.isEmpty()) {
  61.             for (Coordinate coord : routeCoordinates) {
  62.                 routePoints.add(new GPXRoutePointElement(null, coord.x, coord.y, coord.z, -1));
  63.             }
  64.         }

  65.         extensions = new GPXRouteExtensions(result);
  66.     }
  67. }