Class BasicSegmentString

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.noding;
14  
15 import org.locationtech.jts.geom.Coordinate;
16 import org.locationtech.jts.geom.impl.CoordinateArraySequence;
17 import org.locationtech.jts.io.WKTWriter;
18  
19 /**
20  * Represents a list of contiguous line segments,
21  * and supports noding the segments.
22  * The line segments are represented by an array of {@link Coordinate}s.
23  * Intended to optimize the noding of contiguous segments by
24  * reducing the number of allocated objects.
25  * SegmentStrings can carry a context object, which is useful
26  * for preserving topological or parentage information.
27  * All noded substrings are initialized with the same context object.
28  *
29  * @version 1.7
30  */
31 public class BasicSegmentString
32     implements SegmentString 
33 {
34   private Coordinate[] pts;
35   private Object data;
36  
37   /**
38    * Creates a new segment string from a list of vertices.
39    *
40    * @param pts the vertices of the segment string
41    * @param data the user-defined data of this segment string (may be null)
42    */
43   public BasicSegmentString(Coordinate[] pts, Object data)
44   {
45     this.pts = pts;
46     this.data = data;
47   }
48  
49   /**
50    * Gets the user-defined data for this segment string.
51    *
52    * @return the user-defined data
53    */
54   public Object getData() { return data; }
55  
56   /**
57    * Sets the user-defined data for this segment string.
58    *
59    * @param data an Object containing user-defined data
60    */
61   public void setData(Object data) { this.data = data; }
62  
63   public int size() { return pts.length; }
64   public Coordinate getCoordinate(int i) { return pts[i]; }
65   public Coordinate[] getCoordinates() { return pts; }
66  
67   public boolean isClosed()
68   {
69     return pts[0].equals(pts[pts.length - 1]);
70   }
71  
72   /**
73    * Gets the octant of the segment starting at vertex <code>index</code>.
74    *
75    * @param index the index of the vertex starting the segment.  Must not be
76    * the last index in the vertex list
77    * @return the octant of the segment at the vertex
78    */
79   public int getSegmentOctant(int index)
80   {
81     if (index == pts.length - 1return -1;
82     return Octant.octant(getCoordinate(index), getCoordinate(index + 1));
83   }
84  
85   public String toString()
86   {
87     return WKTWriter.toLineString(new CoordinateArraySequence(pts));
88   }
89 }
90