Skip to content

Commit 1e16440

Browse files
committed
Add create points along methods to linestring and multilinestring
1 parent f1d779e commit 1e16440

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/main/groovy/geoscript/geom/LineString.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package geoscript.geom
22

33
import org.locationtech.jts.geom.LineString as JtsLineString
44
import org.locationtech.jts.geom.Coordinate
5+
import org.locationtech.jts.linearref.LengthIndexedLine
56

67
/**
78
* A LineString Geometry.
@@ -236,6 +237,24 @@ class LineString extends Geometry {
236237
Geometry.wrap(indexedLine.extractLine(start * length, end * length))
237238
}
238239

240+
/**
241+
* Create Points along the LineString with the given interval distance.
242+
* @param distance The interval distance of distance between points.
243+
* @return A MultiPoint
244+
*/
245+
MultiPoint createPointsAlong(double distance) {
246+
LengthIndexedLine lengthIndexedLine = new LengthIndexedLine(this.g)
247+
List<Point> points = []
248+
points.add(this.startPoint)
249+
double distanceAlongLine = distance
250+
while (distanceAlongLine < this.length) {
251+
Coordinate coordinate = lengthIndexedLine.extractPoint(distanceAlongLine)
252+
points.add(new Point(coordinate.x, coordinate.y))
253+
distanceAlongLine = distanceAlongLine + distance
254+
}
255+
new MultiPoint(points)
256+
}
257+
239258
/**
240259
* Create a LineString from a List of List of Doubles.
241260
* <p><blockquote><pre>

src/main/groovy/geoscript/geom/MultiLineString.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ class MultiLineString extends GeometryCollection {
142142
results
143143
}
144144

145+
/**
146+
* Create Points along this MultiLineString with the given interval distance.
147+
* @param distance The interval distance of distance between points.
148+
* @return A MultiPoint
149+
*/
150+
MultiPoint createPointsAlong(double distance) {
151+
createPointsAlongGeometry(this, distance)
152+
}
153+
154+
private MultiPoint createPointsAlongGeometry(Geometry geometry, double distance) {
155+
List<Point> points = []
156+
geometry.geometries.each { Geometry subGeometry ->
157+
if (subGeometry instanceof MultiLineString) {
158+
MultiLineString multiLineString = subGeometry as MultiLineString
159+
points.addAll(createPointsAlongGeometry(multiLineString, distance).points)
160+
} else if (subGeometry instanceof LineString) {
161+
LineString lineString = subGeometry as LineString
162+
points.addAll(lineString.createPointsAlong(distance).points)
163+
}
164+
}
165+
new MultiPoint(points)
166+
}
167+
168+
145169
/**
146170
* Create a MultiLineString from a List of List of Doubles
147171
*/

src/test/groovy/geoscript/geom/LineStringTestCase.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ class LineStringTestCase {
135135
assertEquals "LINESTRING (1156010.153864557 649246.3016361536, 1175115.6870342216 648021.5879714314)", subLine.wkt
136136
}
137137

138+
@Test void createPointsAlong() {
139+
def line = new LineString([
140+
[1137466.548141059, 650434.9943107369],
141+
[1175272.4129268457, 648011.541439853],
142+
[1185935.6055587344, 632986.1336403737]
143+
])
144+
MultiPoint points = line.createPointsAlong(2000)
145+
assertEquals(29, points.numGeometries)
146+
}
147+
138148
@Test void addPoint() {
139149
def line = new LineString([new Point(0,0), new Point(5,5)])
140150
def newLine = line.addPoint(1, new Point(3,3))

src/test/groovy/geoscript/geom/MultiLineStringTestCase.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,15 @@ class MultiLineStringTestCase {
8585
assertTrue results.invalidRingLines.empty
8686
}
8787

88+
@Test void createPointsAlong() {
89+
def lines = new MultiLineString(
90+
new LineString ([-5.70068359375, 45.1416015625], [2.47314453125, 53.9306640625]),
91+
new LineString ([-1.21826171875, 53.9306640625], [8.88916015625, 46.1962890625]),
92+
new LineString ([0.71533203125, 42.63671875], [7.13134765625, 50.37109375]),
93+
new LineString ([-5.83251953125, 46.943359375], [4.45068359375, 42.98828125])
94+
)
95+
MultiPoint points = lines.createPointsAlong(1)
96+
assertEquals(49, points.numGeometries)
97+
}
98+
8899
}

0 commit comments

Comments
 (0)