Skip to main content

Overview

The Stop class extends the Point class to represent a bus stop with additional metadata. Each stop has a unique identifier, belongs to a specific bus branch (route), and has a name.

Constructor

new Stop(data)

Parameters

data
object
required
An object containing stop information with the following properties:

Implementation

class Stop extends Point{
    constructor(data){
        super(data.latitude, data.longitude)
        this.id = data.id
        this.branch_id = data.branch_id
        this.name = data.name
    }
}
The Stop class inherits the distance() method from the Point class, allowing distance calculations between stops or from stops to arbitrary geographic points.

Properties

id
string | number
Unique identifier for this bus stop
branch_id
string | number
Identifier for the bus route/branch. Stops with the same branch_id are sequential stops on the same bus route.
name
string
The display name of the bus stop
latitude
number
Latitude coordinate (inherited from Point)
longitude
number
Longitude coordinate (inherited from Point)

Usage Examples

Creating a Stop

const stopData = {
    id: "stop_123",
    branch_id: "route_a",
    name: "Main Street & 5th Ave",
    latitude: 40.7589,
    longitude: -73.9851
};

const busStop = new Stop(stopData);

Calculating Distance to a Stop

const stop1 = new Stop({
    id: "stop_1",
    branch_id: "route_1",
    name: "Downtown Station",
    latitude: 40.7128,
    longitude: -74.0060
});

const stop2 = new Stop({
    id: "stop_2",
    branch_id: "route_1",
    name: "Midtown Plaza",
    latitude: 40.7580,
    longitude: -73.9855
});

const distance = stop1.distance(stop2);
console.log(`Distance between stops: ${distance.toFixed(2)} km`);

Finding Nearby Stops

The route-finding algorithm uses stops to find which bus stops are within walking distance:
function getCloseStops(from){
    return allStops
            .filter(stop => from.distance(stop) <= maxWalkKm)
}

// Find stops within 0.8 km of a point
const currentLocation = new Point(40.7128, -74.0060);
const nearbyStops = getCloseStops(currentLocation);

Stop Sequences

Stops with the same branch_id form a sequence along a bus route. The algorithm uses this to determine the next stop on a route:
1

Find Current Stop

Locate the stop in the global stops array using its id
2

Check Next Position

Get the stop at the next index in the array
3

Verify Same Branch

Ensure the next stop has the same branch_id - if not, the current stop is the end of the route
function getNextStop(point){
    if(! point instanceof Stop ){
        return null
    }
    const stopIndx = allStops.findIndex( s => point.id === s.id)
    if( allStops.length -1 === stopIndx ){
        return null
    } else if( allStops[stopIndx + 1].branch_id !== point.branch_id ){
        return null
    } 
    return allStops[stopIndx + 1]
}
Stops must be ordered correctly in the input data. The algorithm assumes that stops with the same branch_id appear sequentially in the array, representing the order in which a bus visits them.

Role in Route Finding

The Stop class plays a critical role in the route-finding algorithm:
  1. Starting Points: Users begin their journey by walking to a nearby stop
  2. Route Building: Routes are constructed as sequences of stops
  3. Transfers: Users can walk between stops (different branch_ids) to transfer buses
  4. Validation: The algorithm prevents routes from visiting the same stop twice
When preparing bus stop data, ensure that stops are ordered correctly within each branch_id, as the algorithm relies on array ordering to determine the sequence of stops along a route.
  • Point Class - Parent class providing geographic coordinate functionality
  • Route Class - Uses Stop instances to construct routes
  • Find Logic - Main algorithm that processes stops

Build docs developers (and LLMs) love