kiwi-postgis  Version 0.1.0.0
LocatePoint Class Reference

Provides various ways of computing the actual value of a point a given length along a line. More...

Collaboration diagram for LocatePoint:

Public Member Functions

 LocatePoint (LineString line, double length)
 
Coordinate getPoint ()
 
int getIndex ()
 Returns the index of the segment containing the computed point. More...
 

Static Public Member Functions

static Coordinate pointAlongSegment (LineSegment seg, double length)
 Computes the location of a point a given length along a LineSegment. More...
 
static Coordinate pointAlongSegment (Coordinate p0, Coordinate p1, double length)
 Computes the location of a point a given length along a line segment. More...
 
static Coordinate pointAlongSegmentByFraction (Coordinate p0, Coordinate p1, double frac)
 Computes the location of a point a given fraction along a line segment. More...
 
static Coordinate pointAlongLine (LineString line, double length)
 Computes the Coordinate of the point a given length along a LineString. More...
 

Private Member Functions

void compute (LineString line, double length)
 

Private Attributes

Coordinate pt
 
int index
 

Detailed Description

Provides various ways of computing the actual value of a point a given length along a line.

Constructor & Destructor Documentation

◆ LocatePoint()

LocatePoint ( LineString  line,
double  length 
)

References LocatePoint.compute().

Referenced by LocatePoint.pointAlongLine().

96  {
97  compute(line, length);
98  }
void compute(LineString line, double length)
Definition: LocatePoint.java:100

Member Function Documentation

◆ compute()

void compute ( LineString  line,
double  length 
)
private

References Coordinate.distance(), and LocatePoint.pointAlongSegment().

Referenced by LocatePoint.LocatePoint().

101  {
102  // <TODO> handle negative distances (measure from opposite end of line)
103  double totalLength = 0.0;
104  Coordinate[] coord = line.getCoordinates();
105  for (int i = 0; i < coord.length - 1; i++) {
106  Coordinate p0 = coord[i];
107  Coordinate p1 = coord[i+1];
108  double segLen = p1.distance(p0);
109  if (totalLength + segLen > length) {
110  pt = pointAlongSegment(p0, p1, length - totalLength);
111  index = i;
112  return;
113  }
114  totalLength += segLen;
115  }
116  // distance is greater than line length
117  pt = new Coordinate(coord[coord.length - 1]);
118  index = coord.length;
119  }
int index
Definition: LocatePoint.java:93
Coordinate pt
Definition: LocatePoint.java:92
static Coordinate pointAlongSegment(LineSegment seg, double length)
Computes the location of a point a given length along a LineSegment.
Definition: LocatePoint.java:29

◆ getIndex()

int getIndex ( )

Returns the index of the segment containing the computed point.

References LocatePoint.index.

126 { return index; }
int index
Definition: LocatePoint.java:93

◆ getPoint()

Coordinate getPoint ( )

References LocatePoint.pt.

Referenced by LocatePoint.pointAlongLine().

121 { return pt; }
Coordinate pt
Definition: LocatePoint.java:92

◆ pointAlongLine()

static Coordinate pointAlongLine ( LineString  line,
double  length 
)
static

Computes the Coordinate of the point a given length along a LineString.

Parameters
line
length
Returns
the Coordinate of the desired point

References LocatePoint.getPoint(), and LocatePoint.LocatePoint().

87  {
88  LocatePoint loc = new LocatePoint(line, length);
89  return loc.getPoint();
90  }
LocatePoint(LineString line, double length)
Definition: LocatePoint.java:95

◆ pointAlongSegment() [1/2]

static Coordinate pointAlongSegment ( LineSegment  seg,
double  length 
)
static

Computes the location of a point a given length along a LineSegment.

If the length exceeds the length of the line segment the last point of the segment is returned. If the length is negative the first point of the segment is returned.

Parameters
segthe line segment
lengththe length to the desired point
Returns
the Coordinate of the desired point

Referenced by LocatePoint.compute(), and LengthSubstring.computeSubstring().

30  {
31  return pointAlongSegment(seg.p0, seg.p1, length);
32  }
static Coordinate pointAlongSegment(LineSegment seg, double length)
Computes the location of a point a given length along a LineSegment.
Definition: LocatePoint.java:29

◆ pointAlongSegment() [2/2]

static Coordinate pointAlongSegment ( Coordinate  p0,
Coordinate  p1,
double  length 
)
static

Computes the location of a point a given length along a line segment.

If the length exceeds the length of the line segment the last point of the segment is returned. If the length is negative the first point of the segment is returned.

Parameters
p0the first point of the line segment
p1the last point of the line segment
lengththe length to the desired point
Returns
the Coordinate of the desired point

References Coordinate.distance(), Coordinate.x, and Coordinate.y.

47  {
48  double segLen = p1.distance(p0);
49  double frac = length / segLen;
50  if (frac <= 0.0) return p0;
51  if (frac >= 1.0) return p1;
52 
53  double x = (p1.x - p0.x) * frac + p0.x;
54  double y = (p1.y - p0.y) * frac + p0.y;
55  return new Coordinate(x, y);
56  }

◆ pointAlongSegmentByFraction()

static Coordinate pointAlongSegmentByFraction ( Coordinate  p0,
Coordinate  p1,
double  frac 
)
static

Computes the location of a point a given fraction along a line segment.

If the fraction exceeds 1 the last point of the segment is returned. If the fraction is negative the first point of the segment is returned.

Parameters
p0the first point of the line segment
p1the last point of the line segment
fracthe fraction of the segment to the desired point
Returns
the Coordinate of the desired point

References Coordinate.x, and Coordinate.y.

69  {
70  if (frac <= 0.0) return p0;
71  if (frac >= 1.0) return p1;
72 
73  double x = (p1.x - p0.x) * frac + p0.x;
74  double y = (p1.y - p0.y) * frac + p0.y;
75  return new Coordinate(x, y);
76  }

Member Data Documentation

◆ index

int index
private

Referenced by LocatePoint.getIndex().

◆ pt

Coordinate pt
private

Referenced by LocatePoint.getPoint().