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

Error in ratio estimation on locateOnLine and interpolateOnLine #80

Open
alexroat opened this issue Apr 9, 2019 · 1 comment
Open

Comments

@alexroat
Copy link

alexroat commented Apr 9, 2019

I've found an inconsistency in the results if locateOnLine and interpolateOnline.

I've tried to compute the ratio of a point along a road of 320 km ca then convert the ratio to a latlon again. There is a variable misplacement, pretty high in my opinion, it could go above 1km.

I've fixed implementing my own version of the functions


interpolateOnLine = function (map, latLngs, ratio) {
    latLngs = (latLngs instanceof L.Polyline) ? latLngs.getLatLngs() : latLngs;
    var maxzoom = map.getMaxZoom();
    if (maxzoom === Infinity)
        maxzoom = map.getZoom();
    var pts = latLngs.map(function (x) {
        return map.project(x);
    });

    if (ratio <= 0)
        return latLngs[0];
    if (ratio >= 1)
        return latLngs[latLngs - 1];

    var dt = 0.;
    for (var i = 1; i < pts.length; i++)
        dt += pts[i - 1].distanceTo(pts[i]);

    var dp = dt * ratio;


    var dt = 0.;
    for (var i = 1; i < pts.length; i++)
    {
        var d = pts[i - 1].distanceTo(pts[i]);
        if (dt + d > dp)
        {
            //console.log((dp-dt)/d);
            var p = L.GeometryUtil.interpolateOnPointSegment(pts[i - 1], pts[i], (dp - dt) / d);
            return map.unproject(p);
        }
        dt += d;

    }

    return latLngs[latLngs - 1];
};


locateOnLine = function (map, polyline, latlng) {
    var maxzoom = map.getMaxZoom();
    if (maxzoom === Infinity)
        maxzoom = map.getZoom();
    var pts = polyline.getLatLngs().map(function (x) {
        return map.project(x);
    });
    var point = map.project(L.GeometryUtil.closest(map, polyline, latlng, false));


    var d = 0.;
    var dt = 0.;
    for (var i = 1; i < pts.length; i++)
    {
        var dd = pts[i - 1].distanceTo(pts[i]);
        if (L.GeometryUtil.belongsSegment(point, pts[i - 1], pts[i], 0.0001))
            d = dt + pts[i - 1].distanceTo(point);
        dt += dd;
    }
    return d / dt;
};


I've tested converting latlon to ratio to latlon to ratio .... till 10 times and now it seems to be very stable and consistent

PLEASE NOTE THAT interpolateOnLine here is not returning the precedent point , just the interpolated one

@leplatrem
Copy link
Collaborator

Could you please open a PR with the fixes? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants