DirectionAdapter.java

  1. package org.opentrafficsim.xml.bindings;

  2. import org.djunits.value.vdouble.scalar.Direction;
  3. import org.opentrafficsim.xml.bindings.types.DirectionType;

  4. /**
  5.  * DirectionAdapter converts between the XML String for an Direction and the DJUnits Direction. EAST is taken as zero degrees,
  6.  * and the Direction adapts an ENU (East-North-Up) model, where positive x is East, positive y is North, positive z is up, and
  7.  * degrees go anti-clockwise from the positive x-axis (East).
  8.  * <p>
  9.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  10.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  11.  * </p>
  12.  * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
  13.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  14.  */
  15. public class DirectionAdapter extends ScalarAdapter<Direction, DirectionType>
  16. {

  17.     /** {@inheritDoc} */
  18.     @Override
  19.     public DirectionType unmarshal(final String field)
  20.     {
  21.         if (isExpression(field))
  22.         {
  23.             return new DirectionType(trimBrackets(field));
  24.         }
  25.         try
  26.         {
  27.             String direction = field;
  28.             if (direction.trim().endsWith("deg"))
  29.             {
  30.                 direction = direction.replace("deg", "deg(E)");
  31.             }
  32.             if (direction.trim().endsWith("rad"))
  33.             {
  34.                 direction = direction.replace("rad", "rad(E)");
  35.             }
  36.             direction = direction.replace("East", "E");
  37.             direction = direction.replace("North", "N");
  38.             return new DirectionType(Direction.valueOf(direction));
  39.         }
  40.         catch (Exception exception)
  41.         {
  42.             throw exception;
  43.         }
  44.     }

  45. }