Skip to content

Commit ac7ff2a

Browse files
committed
Add linear referencing methods to LineString
1 parent e45d343 commit ac7ff2a

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

doc/api/geom/linestring.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,33 @@ Methods
5353

5454
Linestring geometries have the methods common to all :class:`geom.Geometry`
5555
subclasses.
56+
57+
.. function:: LineString.interpolatePoint
58+
59+
:arg position: ``Number`` The position between 0 and 1.
60+
:returns: :class:`geom.Point`
61+
62+
Returns a Point placed on the LineString at the given percentage along
63+
the LineString.
64+
65+
.. function:: LineString.locatePoint
66+
67+
:arg point: :class:`geom.Point` The Point
68+
:returns: ``Number`` The position (0-1) or percentage of the Point along the LineString.
69+
70+
Returns a position or percentage between 0 and 1 of the Point along the LineString.
71+
72+
.. function:: LineString.placePoint
73+
74+
:arg point: :class:`geom.Point` The Point.
75+
:returns: :class:`geom.Point` The Point on the LineString.
76+
77+
Places or snaps the Point to the LineString.
78+
79+
.. function:: LineString.subLine
80+
81+
:arg start: ``Number`` The start position between 0 and 1.
82+
:arg end: ``Number`` The end position between 0 and 1.
83+
:returns: :class:`geom.LineString` The sub LineString
84+
85+
Returns a position or percentage between 0 and 1 of the Point along the LineString.

src/main/java/org/geoscript/js/geom/LineString.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.mozilla.javascript.annotations.JSGetter;
1313

1414
import com.vividsolutions.jts.geom.Coordinate;
15+
import com.vividsolutions.jts.linearref.LengthIndexedLine;
1516

1617
public class LineString extends Geometry implements Wrapper {
1718

@@ -112,6 +113,37 @@ public LineString reverse() {
112113
return (LineString) GeometryWrapper.wrap(getParentScope(), getGeometry().reverse());
113114
}
114115

116+
@JSFunction
117+
public Point interpolatePoint(double position) {
118+
LengthIndexedLine indexedLine = new LengthIndexedLine(this.getGeometry());
119+
Coordinate c = indexedLine.extractPoint(position * getLength());
120+
return new Point(getParentScope(), factory.createPoint(c));
121+
}
122+
123+
@JSFunction
124+
public double locatePoint(Point point) {
125+
LengthIndexedLine indexedLine = new LengthIndexedLine(this.getGeometry());
126+
double position = indexedLine.indexOf(point.getGeometry().getCoordinate());
127+
double percentAlong = position / getLength();
128+
return percentAlong;
129+
}
130+
131+
@JSFunction
132+
public Point placePoint(Point point) {
133+
LengthIndexedLine indexedLine = new LengthIndexedLine(this.getGeometry());
134+
double position = indexedLine.indexOf(point.getGeometry().getCoordinate());
135+
Coordinate c = indexedLine.extractPoint(position);
136+
return new Point(getParentScope(), factory.createPoint(c));
137+
}
138+
139+
@JSFunction
140+
public LineString subLine(double start, double end) {
141+
LengthIndexedLine indexedLine = new LengthIndexedLine(this.getGeometry());
142+
double length = getLength();
143+
return new LineString(getParentScope(),
144+
(com.vividsolutions.jts.geom.LineString) indexedLine.extractLine(start * length, end * length));
145+
}
146+
115147
/**
116148
* Returns underlying JTS geometry.
117149
*/

src/test/resources/org/geoscript/js/tests/geoscript/geom/test_linestring.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,62 @@ exports["test: centroid"] = function() {
114114

115115
};
116116

117+
exports["test: interpolatePoint"] = function() {
118+
var line = new GEOM.LineString([
119+
[1137466.548141059, 650434.9943107369],
120+
[1175272.4129268457, 648011.541439853],
121+
[1185935.6055587344, 632986.1336403737]
122+
]);
123+
124+
// Interpolate Point Start
125+
var pt1 = line.interpolatePoint(0);
126+
ASSERT.ok(pt1.equals(line.startPoint), "interpolate 0 should return start point");
127+
128+
// Interpolate Point Middle
129+
var pt2 = line.interpolatePoint(0.5);
130+
ASSERT.ok(pt2.equals(new GEOM.Point([1165562.9204493894, 648633.9448037925])), "interpolate 0.5 should return mid point");
131+
132+
// Interpolate Point End
133+
var pt3 = line.interpolatePoint(1.0);
134+
ASSERT.ok(pt3.equals(line.endPoint), "interpolate 1 should return end point");
135+
};
136+
137+
exports["test: locatePoint"] = function() {
138+
var line = new GEOM.LineString([
139+
[1137466.548141059, 650434.9943107369],
140+
[1175272.4129268457, 648011.541439853],
141+
[1185935.6055587344, 632986.1336403737]
142+
]);
143+
var point = new GEOM.Point([1153461.34, 649950.30]);
144+
var position = line.locatePoint(point);
145+
ASSERT.deepEqual(0.284, position.toFixed(3), "locate point position should be 0.284");
146+
};
147+
148+
exports["test: placePoint"] = function() {
149+
var line = new GEOM.LineString([
150+
[1137466.548141059, 650434.9943107369],
151+
[1175272.4129268457, 648011.541439853],
152+
[1185935.6055587344, 632986.1336403737]
153+
]);
154+
var point = new GEOM.Point([1153461.34, 649950.30]);
155+
var placedPoint = line.placePoint(point) ;
156+
ASSERT.ok(placedPoint.equals(new GEOM.Point([1153426.8271476042, 649411.899502625])),
157+
"placed point should be POINT (1153426.8271476042 649411.899502625)");
158+
};
159+
160+
exports["test: subLine"] = function() {
161+
var line = new GEOM.LineString([
162+
[1137466.548141059, 650434.9943107369],
163+
[1175272.4129268457, 648011.541439853],
164+
[1185935.6055587344, 632986.1336403737]
165+
]);
166+
var subLine = line.subLine(0.33, 0.67);
167+
ASSERT.ok(
168+
new GEOM.LineString([[1156010.153864557, 649246.3016361536], [1175115.6870342216, 648021.5879714314]]).equals(subLine),
169+
"subline should be LINESTRING (1156010.153864557 649246.3016361536, 1175115.6870342216 648021.5879714314)"
170+
)
171+
};
172+
117173

118174
if (require.main == module.id) {
119175
system.exit(require("test").run(exports));

0 commit comments

Comments
 (0)