heremaps / flexible-polyline

Flexible Polyline encoding: a lossy compressed representation of a list of coordinate pairs or triples
MIT License
89 stars 76 forks source link

Mismatching Polyline decoder between Javascript and dotnet #75

Closed apparecidoo closed 4 months ago

apparecidoo commented 4 months ago

Hello,

I'm having issues with this decoder polyline, the backend is calculating a different result of coordinates. The backend (dotnet) is generating more coordinates than the frontend (javascript) and it's affecting my calculations of distance. So, in frontend and backend there is a need to calculate the total length of this polyline, but they are generating different points and I'm using the same formula to calculate distances in both places.

Polyline: B2FolvpyD2q71U03B3F5pBT7BvWAzFv5BTnB3NAvC3hBA7BvjCATvMAnBn4BTnBzyBAU3_BTAnfAwC77CT4Dn2CoBnBvtCoBjDr7BUjDrnBU7Gr2BUnL3_BoB7L3kC8B3InuBoBzFjwBoB3Dz8BoBAvlBoBArEAoBrdUA7GA8BjcU8BvlBU4DrTAU3DAoBjIA8BjSA4D_nBT8BjSA8B7QAwCrOAwCjNT4DvMAsE3IAsEzFAgFjDA4D7BT4DnBAoB8BA8BUA8BTAoBnBAoBvCTU3DAArEAoB3IAU3IAA3IATzKAnB7LAvCjSAjDnaArEzoBUnGr7BUvHnnC8B7BrOAnB7GAvC4DAnG4IAzU0eUrJ8LA3IoLU_EoGAnB8LAnB0KAAwMAA8GAoBwbToBoVTUgUTwC0UA8BgKAwCgKAkDwMAwH4cAkD8QA8BoQAoBwRAUgUTA8QATkSAnBofAnB8zBA3D0tBAjDgtBTnG4zC7BjD0mC7BAouBnBoBoVTwCkmBnBwH0yBnB4IsxBTkNw-BnBgKo4BnBoG0tBT4D0jBTwCofAwC4_BnBTgoBAnBgzDUT46BAU89BUwC87CU8BgeAsEo9BUsEozBAwC4XA0F8uBU8B0PC8B8LAgFgjBAkDsTA8B0KAkIouBUoBgFAkN8iCAoQ0mCUsE8QA0KgtBUsOsgCoBsOk6BoB4N0mCoBsEsYU8G8pBoBgPg_C8B0Kw-B8BoL46BoBgKwvBoB0FsYUkDkNUgP03BoBoQk1BoBUsOU8BkNAkD8LU0KgjBoBwH0ZU4IwboBoG4SU4I4XU8G8QUoGsOUkIkXoB0F0PUsE4NUwCgKUwC8LAoBgKUU8LUToLUT8LATsJUAsJAA4DUUsEAnB0KAnB8GU7B4DAvC8BAjDUAjD7BA_ErEUzFnLAzF3NUnL7aAjDvHArE_JAvM_dArJvWUvHnQAvH_OA_E3IA7G7LU7GzKArEnGA_J_OUrJzPAnLnVUvMzZoB7LrYUzKvWU_EzKAvH7QAjIvRU3DzKAnBjDArE7LAjD3IAjD3IAT7BAnB7BAnBTAnBAAnBoBAnBwCUTkDAAsEAUwCArEsEA3DgFAvM8QAnG4DUUoLAkDwHA4DTAgKrOU4DnBAoGTAgF0FA4D0FUgKoVA4SkmBoBkrBg6CvC8VgyBnBoGsTToBgPA3DoLA_EgUUU8VA3IsTAjI7BoBnG4DUjDnBU7BrEUWhIU

Javascript code for decoder: https://github.com/heremaps/flexible-polyline/blob/master/javascript/index.js Dotnet code for decoder: https://github.com/heremaps/flexible-polyline/blob/master/dotnet/src/PolylineEncoderDecoder.cs

Difference of results between, getting 3.5 km of difference: Javascript: 231 points with total length 5752.81073851163 meters Dotnet: 320 points with total length 9345.472837660183 meters

Code used for distance calculation:

Javascript

function distance2d(a: Location, b: Location): number {
    const earthRadius = 6371000;
    const dLat = degreesToRadians(b.lat - a.lat);
    const dLon = degreesToRadians(b.lng - a.lng);
    const r = Math.sin(dLat / 2) *
      Math.sin(dLat / 2) +
      Math.cos(degreesToRadians(a.lat)) *
      Math.cos(degreesToRadians(b.lat)) *
      Math.sin(dLon / 2) *
      Math.sin(dLon / 2);
    const c = 2 * Math.atan2(Math.sqrt(r), Math.sqrt(1 - r));
    const d = earthRadius * c;
    return d;
  },

  function degreesToRadians(deg: number): number {
    return deg * Math.PI / 180;
  },

Dotnet

public static double Distance2d(RoutePoint currentPoint, RoutePoint nextPoint)
        {
            Func<double, double> convertDegreesToRadians = deg => deg * Math.PI / 180;

            var earthRadius = 6371000;
            var degreeLatitude = convertDegreesToRadians(nextPoint.Latitude - currentPoint.Latitude);
            var degreeLongitude = convertDegreesToRadians(nextPoint.Longitude - currentPoint.Longitude);
            var radians = Math.Sin(degreeLatitude / 2) *
              Math.Sin(degreeLatitude / 2) +
              Math.Cos(convertDegreesToRadians(currentPoint.Latitude)) *
              Math.Cos(convertDegreesToRadians(nextPoint.Latitude)) *
              Math.Sin(degreeLongitude / 2) *
              Math.Sin(degreeLongitude / 2);
            var centralAngle = 2 * Math.Atan2(Math.Sqrt(radians), Math.Sqrt(1 - radians));
            var distance = earthRadius * centralAngle;
            return distance;
        }

Thanks!

haifeng2013 commented 4 months ago

Hi @apparecidoo Thanks for reporting.

I tried decoding the example polyline with latest version of both JS and dotnet and got following results: JS: 231 points, length 5752.81073851163 Dotnet: 231 points, length 5752.8107385104295 I got the same number of points and the lengths are quite close with your code for distance calculation.

Would you please double check if this is also the case on your side, Thanks

Best, Haifeng

apparecidoo commented 4 months ago

Thanks for replying and helping, I've check and I had an error in the code when I handled multiple polylines.