View Javadoc
1   package org.opentrafficsim.core.network.geotools;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.media.j3d.Bounds;
6   import javax.vecmath.Point3d;
7   
8   import nl.tudelft.simulation.language.d3.BoundingBox;
9   import nl.tudelft.simulation.language.d3.DirectedPoint;
10  
11  import org.opentrafficsim.core.network.AbstractLink;
12  import org.opentrafficsim.core.unit.FrequencyUnit;
13  import org.opentrafficsim.core.unit.LengthUnit;
14  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
15  
16  import com.vividsolutions.jts.geom.Coordinate;
17  import com.vividsolutions.jts.geom.Envelope;
18  import com.vividsolutions.jts.geom.Point;
19  
20  /**
21   * Link using Geotools Coordinates.
22   * <p>
23   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version Jan 4, 2015 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @param <IDL> the ID type of the Link, e.g., String or Integer.
30   * @param <IDN> the ID type of the Node, e.g., String or Integer.
31   */
32  public class LinkGeotools<IDL, IDN> extends AbstractLink<IDL, IDN, Coordinate, NodeGeotools<IDN>>
33  {
34      /** */
35      private static final long serialVersionUID = 20150104L;
36  
37      /** The geometry of this link. May be null. */
38      private LinearGeometry geometry = null;
39  
40      /**
41       * Construct a new link.
42       * @param id the link id
43       * @param startNode start node (directional)
44       * @param endNode end node (directional)
45       * @param length link length in a length unit
46       * @param capacity link capacity in GTUs per hour
47       */
48      public LinkGeotools(final IDL id, final NodeGeotools<IDN> startNode, final NodeGeotools<IDN> endNode,
49          final DoubleScalar.Rel<LengthUnit> length, final DoubleScalar.Abs<FrequencyUnit> capacity)
50      {
51          super(id, startNode, endNode, length, capacity);
52      }
53  
54      /**
55       * Construct a new link with infinite capacity.
56       * @param id the link id
57       * @param startNode start node (directional)
58       * @param endNode end node (directional)
59       * @param length link length in a length unit
60       */
61      public LinkGeotools(final IDL id, final NodeGeotools<IDN> startNode, final NodeGeotools<IDN> endNode,
62          final DoubleScalar.Rel<LengthUnit> length)
63      {
64          super(id, startNode, endNode, length);
65      }
66  
67      /**
68       * @return the geometry of this link. <br>
69       * <b>Do not modify the returned object or chaos will ensue.</b>
70       */
71      public final LinearGeometry getGeometry()
72      {
73          return this.geometry;
74      }
75  
76      /**
77       * @param geometry set the geometry of this link.
78       */
79      public final void setGeometry(final LinearGeometry geometry)
80      {
81          this.geometry = geometry;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      @SuppressWarnings("checkstyle:designforextension")
87      public DirectedPoint getLocation() throws RemoteException
88      {
89          Point c = getGeometry().getLineString().getCentroid();
90          return new DirectedPoint(new double[] {c.getX(), c.getY(), 0.0d});
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      @SuppressWarnings("checkstyle:designforextension")
96      public Bounds getBounds() throws RemoteException
97      {
98          DirectedPoint c = getLocation();
99          if (getGeometry() != null)
100         {
101             Envelope envelope = getGeometry().getLineString().getEnvelopeInternal();
102             return new BoundingBox(new Point3d(envelope.getMinX() - c.x, envelope.getMinY() - c.y, 0.0d), new Point3d(
103                 envelope.getMaxX() - c.x, envelope.getMaxY() - c.y, 0.0d));
104         }
105         double minX = Math.min(getEndNode().getX(), getStartNode().getX());
106         double minY = Math.min(getEndNode().getY(), getStartNode().getY());
107         double maxX = Math.max(getEndNode().getX(), getStartNode().getX());
108         double maxY = Math.max(getEndNode().getY(), getStartNode().getY());
109         return new BoundingBox(new Point3d(minX - c.x, minY - c.y, 0.0d), new Point3d(maxX - c.x, maxY - c.y, 0.0d));
110     }
111 
112     /**
113      * String ID implementation of the Point link.
114      * <p>
115      * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
116      * All rights reserved. <br>
117      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
118      * <p>
119      * @version Jan 4, 2015 <br>
120      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
121      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
122      */
123     public static class STR extends LinkGeotools<String, String>
124     {
125         /** */
126         private static final long serialVersionUID = 20150104L;
127 
128         /**
129          * Construct a new link.
130          * @param id the link id
131          * @param startNode start node (directional)
132          * @param endNode end node (directional)
133          * @param length link length in a length unit
134          * @param capacity link capacity in GTUs per hour
135          */
136         public STR(final String id, final NodeGeotools.STR startNode, final NodeGeotools.STR endNode,
137             final DoubleScalar.Rel<LengthUnit> length, final DoubleScalar.Abs<FrequencyUnit> capacity)
138         {
139             super(id, startNode, endNode, length, capacity);
140         }
141 
142         /**
143          * Construct a new link with infinite capacity.
144          * @param id the link id
145          * @param startNode start node (directional)
146          * @param endNode end node (directional)
147          * @param length link length in a length unit
148          */
149         public STR(final String id, final NodeGeotools.STR startNode, final NodeGeotools.STR endNode,
150             final DoubleScalar.Rel<LengthUnit> length)
151         {
152             super(id, startNode, endNode, length);
153         }
154     }
155 
156     /**
157      * Integer ID implementation of the Point link.
158      * <p>
159      * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
160      * All rights reserved. <br>
161      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
162      * <p>
163      * @version Jan 4, 2015 <br>
164      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
165      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
166      */
167     public static class INT extends LinkGeotools<Integer, Integer>
168     {
169         /** */
170         private static final long serialVersionUID = 20150104L;
171 
172         /**
173          * Construct a new link.
174          * @param id the link id
175          * @param startNode start node (directional)
176          * @param endNode end node (directional)
177          * @param length link length in a length unit
178          * @param capacity link capacity in GTUs per hour
179          */
180         public INT(final int id, final NodeGeotools.INT startNode, final NodeGeotools.INT endNode,
181             final DoubleScalar.Rel<LengthUnit> length, final DoubleScalar.Abs<FrequencyUnit> capacity)
182         {
183             super(id, startNode, endNode, length, capacity);
184         }
185 
186         /**
187          * Construct a new link with infinite capacity.
188          * @param id the link id
189          * @param startNode start node (directional)
190          * @param endNode end node (directional)
191          * @param length link length in a length unit
192          */
193         public INT(final int id, final NodeGeotools.INT startNode, final NodeGeotools.INT endNode,
194             final DoubleScalar.Rel<LengthUnit> length)
195         {
196             super(id, startNode, endNode, length);
197         }
198     }
199 
200 }