kiwi-postgis  Version 0.1.0.0
LengthSubstring Class Reference

Computes a substring of a LineString between given distances along the line. More...

Collaboration diagram for LengthSubstring:

Public Member Functions

 LengthSubstring (LineString line)
 
LineString getSubstring (double startDistance, double endDistance)
 

Static Public Member Functions

static LineString getSubstring (LineString line, double startLength, double endLength)
 

Private Member Functions

LineString computeSubstring (double startDistance, double endDistance)
 Assumes input is strictly valid (e.g. More...
 

Private Attributes

LineString line
 

Detailed Description

Computes a substring of a LineString between given distances along the line.

  • The distances are clipped to the actual line length
  • If the start distance is equal to the end distance, a zero-length line with two identical points is returned
  • FUTURE: If the start distance is greater than the end distance, an inverted section of the line is returned

FUTURE: should handle startLength > endLength, and flip the returned linestring. Also should handle negative lengths (they are measured from end of line backwards).

Constructor & Destructor Documentation

◆ LengthSubstring()

LengthSubstring ( LineString  line)

References LengthSubstring.line.

Referenced by LengthSubstring.getSubstring().

37  {
38  this.line = line;
39  }
LineString line
Definition: LengthSubstring.java:35

Member Function Documentation

◆ computeSubstring()

LineString computeSubstring ( double  startDistance,
double  endDistance 
)
private

Assumes input is strictly valid (e.g.

startDist < endDistance)

Parameters
startDistance
endDistance
Returns

Ensure there is enough coordinates to build a valid line. Make a 2-point line with duplicate coordinates, if necessary There will always be at least one coordinate in the coordList.

References LocatePoint.pointAlongSegment().

Referenced by LengthSubstring.getSubstring().

70  {
71  Coordinate[] coordinates = line.getCoordinates();
72  CoordinateList newCoordinates = new CoordinateList();
73  double segmentStartDistance = 0.0;
74  double segmentEndDistance = 0.0;
75  boolean started = false;
76  int i = 0;
77  LineSegment segment = new LineSegment();
78  while (i < coordinates.length - 1 && endDistance > segmentEndDistance) {
79  segment.p0 = coordinates[i];
80  segment.p1 = coordinates[i + 1];
81  i++;
82  segmentStartDistance = segmentEndDistance;
83  segmentEndDistance = segmentStartDistance + segment.getLength();
84 
85  if (startDistance > segmentEndDistance)
86  continue;
87  if (startDistance >= segmentStartDistance
88  && startDistance < segmentEndDistance) {
89  newCoordinates.add(LocatePoint.pointAlongSegment(segment.p0, segment.p1,
90  startDistance - segmentStartDistance), false);
91  }
92  /*
93  if (startDistance >= segmentStartDistance
94  && startDistance == segmentEndDistance) {
95  newCoordinates.add(new Coordinate(segment.p1), false);
96  }
97  */
98  if (endDistance >= segmentEndDistance) {
99  newCoordinates.add(new Coordinate(segment.p1), false);
100  }
101  if (endDistance >= segmentStartDistance
102  && endDistance < segmentEndDistance) {
103  newCoordinates.add(LocatePoint.pointAlongSegment(segment.p0, segment.p1,
104  endDistance - segmentStartDistance), false);
105  }
106  }
107  Coordinate[] newCoordinateArray = newCoordinates.toCoordinateArray();
113  if (newCoordinateArray.length <= 1) {
114  newCoordinateArray = new Coordinate[] { newCoordinateArray[0], newCoordinateArray[0]};
115  }
116  return line.getFactory().createLineString(newCoordinateArray);
117  }
LineString line
Definition: LengthSubstring.java:35

◆ getSubstring() [1/2]

static LineString getSubstring ( LineString  line,
double  startLength,
double  endLength 
)
static

References LengthSubstring.getSubstring(), and LengthSubstring.LengthSubstring().

Referenced by LengthSubstring.getSubstring().

30  {
32  return ls.getSubstring(startLength, endLength);
33  }
LineString line
Definition: LengthSubstring.java:35
LengthSubstring(LineString line)
Definition: LengthSubstring.java:37

◆ getSubstring() [2/2]

LineString getSubstring ( double  startDistance,
double  endDistance 
)

References LengthSubstring.computeSubstring().

42  {
43  // future: if start > end, flip values and return an inverted line
44  Assert.isTrue(startDistance <= endDistance, "inverted distances not currently supported");
45 
46  Coordinate[] coordinates = line.getCoordinates();
47  // check for a zero-length segment and handle appropriately
48  if (endDistance <= 0.0) {
49  return line.getFactory().createLineString(
50  new Coordinate[] { coordinates[0], coordinates[0]});
51  }
52  if (startDistance >= line.getLength()) { return line.getFactory()
53  .createLineString(
54  new Coordinate[] { coordinates[coordinates.length - 1],
55  coordinates[coordinates.length - 1]}); }
56  if (startDistance < 0.0) {
57  startDistance = 0.0;
58  }
59  return computeSubstring(startDistance, endDistance);
60  }
LineString computeSubstring(double startDistance, double endDistance)
Assumes input is strictly valid (e.g.
Definition: LengthSubstring.java:69
LineString line
Definition: LengthSubstring.java:35

Member Data Documentation

◆ line

LineString line
private