Turfjs / Turfjs/turf

@turf/along throws an "Coord is required" error

Open
#1,802 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

@turf/along easy-fix
Dominant language
TypeScript
Stars
10.5k
Forks
1k
Avg merge
1h 11m
Merged PRs (30d)
4

Description

@turf/along throws this https://github.com/Turfjs/turf/blob/master/packages/turf-invariant/index.ts#L18 error.

My usage:

const currentPoint = along(animData.line, stepValue, { units: 'kilometers' })

Error/stacktrace:
Screenshot_20191210_150001_app turnout
Screenshot_20191210_150004_app turnout

My debugging. Note that coords[i - 1] is undefined.

function along(line, distance, options) {
    if (options === void 0) { options = {}; }
    // Get Coords
    var geom = invariant_1.getGeom(line);
    var coords = geom.coordinates;
    var travelled = 0;
    for (var i = 0; i < coords.length; i++) {
        if (distance >= travelled && i === coords.length - 1) {
            break;
        }
        else if (travelled >= distance) {
            var overshot = distance - travelled;
            if (!overshot) {
                return helpers_1.point(coords[i]);
            }
            else {
                // error happens here. 
                // "coords[i - 1]" is undefined
                // "bearing" function throws an error because "coords[i - 1]" is undefined
                var direction = bearing_1.default(coords[i], coords[i - 1]) - 180;
                var interpolated = destination_1.default(coords[i], overshot, direction, options);
                return interpolated;
            }
        }
        else {
            travelled += distance_1.default(coords[i], coords[i + 1], options);
        }
    }
    return helpers_1.point(coords[coords.length - 1]);
}

Unfortunately I don't have GeoJSON data file. The issue is intermittent and only happens for some arcane GPS stream values. I pinpointed issue to coords[i - 1] being undefined, but I don't know why it could be undefined because I don't understand the algorithm along function uses.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the @turf/along implementation and the turf-invariant index.ts and bearing calls shown in the report. The missing GeoJSON prevents direct reproduction, so first obtain a minimal failing LineString and distance value, then trace where coords[i - 1] becomes undefined. Done means identifying the failing input or algorithm boundary and adding a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.