Skip to content

Fix broken polyline rendering on looping routes and optimize shape processing #296

@ARCoder181105

Description

@ARCoder181105

File: internal/restapi/shapes_handler.go

Description:
I found a logic bug that causes visual artifacts (lines "shooting off" the map) for routes that loop back on themselves, along with a performance inefficiency.

The Issue:

  1. Broken Rendering: The current implementation splits a shape into multiple segments if it detects a repeated edge (e.g., a round trip). It then concatenates the encoded polyline strings. Since polyline encoding relies on delta offsets from the previous point, concatenating separate strings resets this offset. The decoder interprets the start of the second segment (an absolute coordinate) as a delta, causing massive coordinate jumps.
  2. Performance: The loop appends points to currentLine one by one without pre-allocation, causing unnecessary memory re-allocations for large shapes.

Proposed Solution:
Standard GTFS shapes are defined as a single continuous sequence. We should remove the edge-splitting logic and encode the entire shape in one pass. This fixes the rendering bug and allows us to pre-allocate memory.

Suggested Fix:
Refactor the loop to pre-allocate the slice and remove the edges map check:

// Optimization: Pre-allocate slice to avoid resizing
lineCoords := make([][]float64, 0, len(shapes))

for _, point := range shapes {
    lineCoords = append(lineCoords, []float64{point.Lat, point.Lon})
}

// Encode as a single continuous polyline to ensure valid delta offsets
encodedPoints := string(polyline.EncodeCoords(lineCoords))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions