The Wayback Machine - https://web.archive.org/web/20200519104833/https://github.com/Turfjs/turf/issues/1802
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
noway opened this issue Dec 10, 2019 · 4 comments
Open

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

noway opened this issue Dec 10, 2019 · 4 comments
Labels

Comments

@noway
Copy link

@noway noway commented Dec 10, 2019

@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.

@rowanwins
Copy link
Member

@rowanwins rowanwins commented Jan 2, 2020

Hi @noway

It's pretty tricky to debug with seeing some data... I've tried a few things but can't replicate, if you can supply a sample input then we can look at it further

@noway
Copy link
Author

@noway noway commented Feb 29, 2020

@rowanwins finally reproduced!!!

along({ "type": "Feature", "properties": {}, "geometry": {"type": "LineString", "coordinates": [[174, -36], [174.1, -36.1] ] } }, -0.1, { units: 'kilometers' })

happens when second argument is negative!!!

@noway
Copy link
Author

@noway noway commented Feb 29, 2020

@rowanwins I fixed the bug in my own code, just had to replace stepValue with Math.max(stepValue, 0)

I think a line of warning in the reference docs would be sufficient to put this issue to rest!

@rowanwins
Copy link
Member

@rowanwins rowanwins commented Mar 7, 2020

Thanks for reporting back @noway !

@rowanwins rowanwins added the easy-fix label Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.