GeoJSONIsochronesIntersection.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.isochrones.geojson;

  16. import com.fasterxml.jackson.annotation.JsonIgnore;
  17. import com.fasterxml.jackson.annotation.JsonProperty;
  18. import org.heigit.ors.api.requests.isochrones.IsochronesRequest;
  19. import org.heigit.ors.api.requests.isochrones.IsochronesRequestEnums;
  20. import org.heigit.ors.common.Pair;
  21. import org.heigit.ors.exceptions.InternalServerException;
  22. import org.heigit.ors.isochrones.IsochronesErrorCodes;
  23. import org.heigit.ors.isochrones.IsochronesIntersection;
  24. import org.heigit.ors.util.FormatUtility;
  25. import org.locationtech.jts.geom.Geometry;

  26. import java.util.*;

  27. public class GeoJSONIsochronesIntersection extends GeoJSONIsochroneBase {
  28.     @JsonIgnore
  29.     private final IsochronesIntersection intersection;

  30.     @JsonProperty("properties")
  31.     private final Map<String, Object> properties;

  32.     public GeoJSONIsochronesIntersection(IsochronesIntersection intersection, IsochronesRequest request) throws InternalServerException {
  33.         this.intersection = intersection;
  34.         properties = fillProperties(intersection, request);
  35.     }

  36.     private Map<String, Object> fillProperties(IsochronesIntersection intersection, IsochronesRequest request) throws InternalServerException {
  37.         Map<String, Object> props = new HashMap<>();

  38.         List<Integer[]> contours = new ArrayList<>();
  39.         for (Pair<Integer, Integer> ref : intersection.getContourRefs()) {
  40.             Integer[] pair = new Integer[2];
  41.             pair[0] = ref.first;
  42.             pair[1] = ref.second;

  43.             contours.add(pair);
  44.         }

  45.         props.put("contours", contours);

  46.         if (request.hasAttributes()) {
  47.             List<IsochronesRequestEnums.Attributes> attr = new ArrayList<>(Arrays.asList(request.getAttributes()));

  48.             if (attr.contains(IsochronesRequestEnums.Attributes.AREA)) {
  49.                 try {
  50.                     double areaValue = 0;
  51.                     if (request.hasAreaUnits())
  52.                         areaValue = intersection.getArea(request.getAreaUnit().toString());
  53.                     else
  54.                         areaValue = intersection.getArea("");

  55.                     props.put("area", FormatUtility.roundToDecimals(areaValue, 4));
  56.                 } catch (InternalServerException e) {
  57.                     throw new InternalServerException(IsochronesErrorCodes.UNKNOWN, "There was a problem calculating the area of the isochrone");
  58.                 }
  59.             }
  60.         }

  61.         return props;
  62.     }

  63.     public Map<String, Object> getProperties() {
  64.         return properties;
  65.     }

  66.     @Override
  67.     public Geometry getIsochroneGeometry() {
  68.         return intersection.getGeometry();
  69.     }
  70. }