Skip to content

Colota - Android GPS Location tracker

perf: merge same-color track segments into single LineString features

Max Dietrichee88e5ce2822
diff --git a/apps/mobile/src/components/features/map/__tests__/mapUtils.test.ts b/apps/mobile/src/components/features/map/__tests__/mapUtils.test.ts
index e6e9d33a..7e718f96 100644
--- a/apps/mobile/src/components/features/map/__tests__/mapUtils.test.ts
+++ b/apps/mobile/src/components/features/map/__tests__/mapUtils.test.ts
@@ -130,7 +130,7 @@ describe("buildTrackSegmentsGeoJSON", () => {
expect(result.features).toHaveLength(0)
})
- it("creates one LineString per segment", () => {
+ it("creates one LineString per distinct color run", () => {
const locs = [
{ latitude: 52.52, longitude: 13.405, speed: 1 },
{ latitude: 52.53, longitude: 13.406, speed: 3 },
@@ -142,6 +142,19 @@ describe("buildTrackSegmentsGeoJSON", () => {
expect(result.features[1].geometry.type).toBe("LineString")
})
+ it("merges consecutive same-color segments into one LineString", () => {
+ const locs = [
+ { latitude: 52.52, longitude: 13.405 },
+ { latitude: 52.53, longitude: 13.406 },
+ { latitude: 52.54, longitude: 13.407 },
+ { latitude: 52.55, longitude: 13.408 }
+ ]
+ const result = buildTrackSegmentsGeoJSON(locs, colors, { defaultColor: "#FF0000" })
+ expect(result.features).toHaveLength(1)
+ // @ts-ignore - LineString coordinates
+ expect(result.features[0].geometry.coordinates).toHaveLength(4)
+ })
+
it("each segment has a color property", () => {
const locs = [
{ latitude: 52.52, longitude: 13.405, speed: 0 },
diff --git a/apps/mobile/src/components/features/map/mapUtils.ts b/apps/mobile/src/components/features/map/mapUtils.ts
index b3f949bc..33cd83a3 100644
--- a/apps/mobile/src/components/features/map/mapUtils.ts
+++ b/apps/mobile/src/components/features/map/mapUtils.ts
@@ -89,7 +89,9 @@ interface TrackSegmentOptions {
defaultColor?: string
}
-/** Build per-segment LineString features with a pre-computed `color` property.
+/** Build LineString features with a pre-computed `color` property.
+ * Consecutive segments of the same color are merged into a single multi-point LineString
+ * to keep feature count low on long trips (O(color changes) instead of O(points)).
* Pass `skipIndices` to leave gaps between trips (indices where a new trip starts).
* Pass `locationColors` to override speed-based coloring with per-location colors. */
export function buildTrackSegmentsGeoJSON(
@@ -99,25 +101,49 @@ export function buildTrackSegmentsGeoJSON(
): GeoJSON.FeatureCollection {
const { skipIndices, locationColors, defaultColor } = options ?? {}
const features: GeoJSON.Feature[] = []
+
+ let currentColor: string | null = null
+ let currentCoords: [number, number][] = []
+
+ const flush = () => {
+ if (currentCoords.length >= 2 && currentColor !== null) {
+ features.push({
+ type: "Feature",
+ properties: { color: currentColor },
+ geometry: { type: "LineString", coordinates: currentCoords }
+ })
+ }
+ currentCoords = []
+ currentColor = null
+ }
+
for (let i = 1; i < locations.length; i++) {
- if (skipIndices?.has(i)) continue
+ if (skipIndices?.has(i)) {
+ flush()
+ continue
+ }
+
const color = defaultColor
? defaultColor
: locationColors
? locationColors[i]
: getSpeedColor(((locations[i - 1].speed ?? 0) + (locations[i].speed ?? 0)) / 2, colors)
- features.push({
- type: "Feature",
- properties: { color },
- geometry: {
- type: "LineString",
- coordinates: [
- [locations[i - 1].longitude, locations[i - 1].latitude],
- [locations[i].longitude, locations[i].latitude]
- ]
- }
- })
+
+ if (currentColor === null) {
+ currentCoords.push([locations[i - 1].longitude, locations[i - 1].latitude])
+ currentCoords.push([locations[i].longitude, locations[i].latitude])
+ currentColor = color
+ } else if (color === currentColor) {
+ currentCoords.push([locations[i].longitude, locations[i].latitude])
+ } else {
+ flush()
+ currentCoords.push([locations[i - 1].longitude, locations[i - 1].latitude])
+ currentCoords.push([locations[i].longitude, locations[i].latitude])
+ currentColor = color
+ }
}
+ flush()
+
return { type: "FeatureCollection", features }
}