NodeParser.java

  1. package org.opentrafficsim.road.network.factory.xml.network;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import org.djunits.unit.DirectionUnit;
  5. import org.djunits.value.vdouble.scalar.Direction;
  6. import org.opentrafficsim.core.geometry.OTSPoint3D;
  7. import org.opentrafficsim.core.network.NetworkException;
  8. import org.opentrafficsim.core.network.Node;
  9. import org.opentrafficsim.core.network.OTSNetwork;
  10. import org.opentrafficsim.core.network.OTSNode;
  11. import org.opentrafficsim.xml.generated.LINK;
  12. import org.opentrafficsim.xml.generated.NETWORK;
  13. import org.opentrafficsim.xml.generated.NODE;

  14. /**
  15.  * NodeParser takes care of parsing the NODE tags in the XML network. <br>
  16.  * <br>
  17.  * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  18.  * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
  19.  * source code and binary code of this software is proprietary information of Delft University of Technology.
  20.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  21.  */
  22. public final class NodeParser
  23. {
  24.     /** */
  25.     public NodeParser()
  26.     {
  27.         // utility class
  28.     }

  29.     /**
  30.      * Parse the Nodes.
  31.      * @param otsNetwork the network to insert the parsed objects in
  32.      * @param network the NETWORK tag
  33.      * @throws NetworkException when the objects cannot be inserted into the network due to inconsistencies
  34.      */
  35.     public static void parseNodes(final OTSNetwork otsNetwork, final NETWORK network) throws NetworkException
  36.     {
  37.         for (NODE xmlNode : network.getNODE())
  38.             new OTSNode(otsNetwork, xmlNode.getNAME(), new OTSPoint3D(xmlNode.getCOORDINATE()));
  39.     }

  40.     /**
  41.      * Calculate the default angles of the Nodes, in case they have not been set. This is based on the STRAIGHT LINK elements in
  42.      * the XML file.
  43.      * @param otsNetwork the network to insert the parsed objects in
  44.      * @param network the NETWORK tag
  45.      * @return a map of nodes and their default direction
  46.      */
  47.     public static Map<String, Direction> calculateNodeAngles(final OTSNetwork otsNetwork, final NETWORK network)
  48.     {
  49.         Map<String, Direction> nodeDirections = new HashMap<>();
  50.         for (NODE xmlNode : network.getNODE())
  51.         {
  52.             if (xmlNode.getDIRECTION() != null)
  53.             {
  54.                 nodeDirections.put(xmlNode.getNAME(), xmlNode.getDIRECTION());
  55.             }
  56.         }

  57.         for (LINK xmlLink : network.getLINK())
  58.         {
  59.             if (xmlLink.getSTRAIGHT() != null)
  60.             {
  61.                 Node startNode = otsNetwork.getNode(xmlLink.getNODESTART().getNAME());
  62.                 Node endNode = otsNetwork.getNode(xmlLink.getNODEEND().getNAME());
  63.                 double direction = Math.atan2(endNode.getPoint().y - startNode.getPoint().y,
  64.                         endNode.getPoint().x - startNode.getPoint().x);
  65.                 if (!nodeDirections.containsKey(startNode.getId()))
  66.                 {
  67.                     nodeDirections.put(startNode.getId(), new Direction(direction, DirectionUnit.EAST_RADIAN));
  68.                 }
  69.                 if (!nodeDirections.containsKey(endNode.getId()))
  70.                 {
  71.                     nodeDirections.put(endNode.getId(), new Direction(direction, DirectionUnit.EAST_RADIAN));
  72.                 }
  73.             }
  74.         }

  75.         for (NODE xmlNode : network.getNODE())
  76.         {
  77.             if (!nodeDirections.containsKey(xmlNode.getNAME()))
  78.             {
  79.                 System.err.println("Warning: Node " + xmlNode.getNAME() + " does not have a (calculated) direction");
  80.             }
  81.         }

  82.         return nodeDirections;
  83.     }

  84. }