Skip to content

Commit c6c8947

Browse files
authored
Fix lineSliceAlong() dropping altitude of the line's vertices (#1637)
1 parent 53a24f5 commit c6c8947

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Mapbox welcomes participation and contributions from everyone.
44

55
### main
6+
- Fixed `TurfMisc#lineSliceAlong` dropping the altitude of the line's vertices, a regression introduced in v7.10.0.
67

78
### v7.10.0 - February 05, 2026
89
- Added `DirectionsRefreshResponse#fromJson(Reader)`, a static factory method that deserializes a `DirectionsRefreshResponse` from a `java.io.Reader`.

‎scripts/coverage.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -eoux
33
# control sum of the key is const: echo | shasum -a 256 pgp_keys.asc
44
shaSumAscKey="d56942c32a1bb70af75bf972302b6114049fb59cb76193fac349bb9b587b60c2"
55

6-
curl https://keybase.io/codecovsecurity/pgp_keys.asc -o pgp_keys.asc
6+
curl https://uploader.codecov.io/verification.gpg -o pgp_keys.asc
77
# check sum
88
echo "$shaSumAscKey pgp_keys.asc" | shasum -a 256 -c
99

@@ -21,4 +21,4 @@ gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
2121
shasum -a 256 -c codecov.SHA256SUM
2222

2323
chmod +x codecov
24-
./codecov
24+
./codecov

‎services-turf/src/main/java/com/mapbox/turf/TurfMisc.java‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import androidx.annotation.Nullable;
66

77
import com.mapbox.geojson.Feature;
8+
import com.mapbox.geojson.FlattenListOfPoints;
89
import com.mapbox.geojson.LineString;
910
import com.mapbox.geojson.Point;
1011
import com.mapbox.turf.models.LineIntersectsResult;
@@ -188,9 +189,11 @@ public static LineString lineSliceAlong(@NonNull LineString line,
188189
@FloatRange(from = 0) double stopDist,
189190
@NonNull @TurfConstants.TurfUnitCriteria String units) {
190191

191-
double[] coords = line.flattenCoordinates().getFlattenLngLatArray();
192+
FlattenListOfPoints flattenCoordinates = line.flattenCoordinates();
193+
double[] coords = flattenCoordinates.getFlattenLngLatArray();
194+
double[] altitudes = flattenCoordinates.getAltitudes();
192195

193-
int size = line.flattenCoordinates().size();
196+
int size = flattenCoordinates.size();
194197
if (size < 2) {
195198
throw new TurfException("Turf lineSlice requires a LineString Geometry made up of "
196199
+ "at least 2 coordinates. The LineString passed in only contains " + size + ".");
@@ -203,7 +206,9 @@ public static LineString lineSliceAlong(@NonNull LineString line,
203206

204207
double travelled = 0;
205208
for (int i = 0; i < size; i++) {
206-
Point pointAtI = Point.fromLngLat(coords[i * 2], coords[i * 2 + 1]);
209+
// Altitude is kept only for the original vertices: the interpolated start and stop points
210+
// are computed with 2D math (see TurfMeasurement#destination) and have no altitude.
211+
Point pointAtI = pointAt(coords, altitudes, i);
207212

208213
if (startDist >= travelled && i == size - 1) {
209214
break;
@@ -247,6 +252,16 @@ public static LineString lineSliceAlong(@NonNull LineString line,
247252
return LineString.fromLngLats(slice);
248253
}
249254

255+
@NonNull
256+
private static Point pointAt(@NonNull double[] coords, @Nullable double[] altitudes, int index) {
257+
double longitude = coords[index * 2];
258+
double latitude = coords[(index * 2) + 1];
259+
if (altitudes != null && !Double.isNaN(altitudes[index])) {
260+
return Point.fromLngLat(longitude, latitude, altitudes[index]);
261+
}
262+
return Point.fromLngLat(longitude, latitude);
263+
}
264+
250265
/**
251266
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the
252267
* LineString.

‎services-turf/src/test/java/com/mapbox/turf/TurfMiscTest.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,27 @@ public void testLineAlongStopLongerThanLength() throws IOException, TurfExceptio
583583
lineCoordinates.get(lineCoordinates.size() - 1).flattenCoordinates(), DELTA);
584584
}
585585

586+
@Test
587+
public void testLineSliceAlongPreservesAltitude() throws TurfException {
588+
List<Point> input = Arrays.asList(
589+
Point.fromLngLat(113.99414062499999, 22.350075806124867, 10.0),
590+
Point.fromLngLat(115.0, 22.8, 20.0),
591+
Point.fromLngLat(116.76269531249999, 23.241346102386135, 30.0));
592+
LineString line = LineString.fromLngLats(input);
593+
594+
// Slicing the whole line means every returned point is an original vertex.
595+
double stop = TurfMeasurement.length(line, TurfConstants.UNIT_MILES);
596+
LineString sliced = TurfMisc.lineSliceAlong(line, 0, stop, TurfConstants.UNIT_MILES);
597+
598+
List<Point> slicedCoordinates = sliced.coordinates();
599+
assertEquals(input.size(), slicedCoordinates.size());
600+
for (int i = 0; i < input.size(); i++) {
601+
assertTrue("point " + i + " lost altitude", slicedCoordinates.get(i).hasAltitude());
602+
assertArrayEquals(input.get(i).flattenCoordinates(),
603+
slicedCoordinates.get(i).flattenCoordinates(), DELTA);
604+
}
605+
}
606+
586607
@Test
587608
public void testShortLine() throws IOException, TurfException {
588609

0 commit comments

Comments
 (0)