question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lineOffset function returns random numbers for coordinates for polylines with 180 degree turns

See original GitHub issue

Hi! Your lib is a life saver for my work. So first of all thank you 😃

And now let’s go down to the business. I have found a wierd behaviour of lineOffset functions for some polylines. I have narrowed problem to problems with floating points precision.

Small example for reproduce (line has a 180 degrees turn on third point):

turf.lineOffset(turf.lineString([
    [
      30.59357475489378,
      50.4496289184317
    ],
    [
      30.593773238360882,
      50.44968524482101
    ],
    [
      30.593875162303448,
      50.44971743132919
    ],
    [
      30.593773238360882,
      50.44968524482101
    ],
    [
      30.59357475489378,
      50.4496289184317
    ],
]), 5, {units: 'meters'})

Result

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [
        30.59358703078247,
        50.44958566053822
      ],
      [
        30.593786149427643,
        50.44964216718077
      ],
      [
        -6465589.797091965,  <-- Problem is
        -2041724.4105408227  <-- here
      ],
      [
        30.593760330107745,
        50.44972832325972
      ],
      [
        30.59356247900509,
        50.44967217632518
      ]
    ]
  }
}

I tried to find the problem by myself. And looks like the issue is in function isParallel. For inputs

A = [ [ 30.593867037896306, 50.44974315861847 ], [ 30.59376511395374, 50.44971097211029 ] ]
B = [ [ 30.593780985000908, 50.449659398236854 ], [ 30.59388328671059, 50.449691704039914 ] ]

It calculates angle between them

CrossProduct = -3.049318610115481e-19

And to be parallel this crossProduct should be equal to 0. If I try some delta instead (0.000000001 for example), looks like nothing breaks and it fixes the problem. I didn’t spend too much time on sources exploration so I don’t know if it is a valid fix. turf.truncate can help in some cases, but not always.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aj8kcommented, Jul 17, 2018

@rowanwins Here is result that I got with applied fix capture Result

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [
        30.59358703078247,
        50.44958566053822
      ],
      [
        30.593786149427643,
        50.44964216718077
      ],
      [
        30.59386162162488,
        50.449760310144654
      ],
      [
        30.59376032729412,
        50.449728322461254
      ],
      [
        30.59356247900509,
        50.44967217632518
      ]
    ]
  }
}

Here is my variant of the fix (has changed custom delta to more fundamental constant Number.EPSILON) Original function: https://github.com/Turfjs/turf/blob/master/packages/turf-line-offset/lib/intersection.js#L98-L102

function isParallel(a, b) {
    var r = ab(a);
    var s = ab(b);
    return (crossProduct(r, s) === 0);
}

Fixed variant:

function isParallel(a, b) {
    var r = ab(a);
    var s = ab(b);
    return (Math.abs(crossProduct(r, s)) < Number.EPSILON);
}
0reactions
aj8kcommented, Oct 1, 2018

@rowanwins Looks like https://github.com/Turfjs/turf/pull/1432 is delaying. If I push fix in separate branch will it be merged before this big update? Will npm module be updated?

For now I have created some workaround fix for this issue (don’t want to use custom library build) but it would be great to fix it in the turf library so everything start working in my project as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced geospatial analysis - Turf.js
Returns. number - bearing in decimal degrees, between -180 and 180 degrees (positive clockwise). npm install @turf/bearing ...
Read more >
INetworkMarkupElement - AnyLogic Help
Returns the randomly chosen point inside the shape area. This method utilises Random Number Generator of the Presentable object containing this shape.
Read more >
CEDRA-AVcad
Vertex coordinates and optionally, the polyline/polygon layer and OID, are stored as ... Handle circular arcs whose central angle exceeds 180 degrees.
Read more >
The CEDRA Corporation, Bridging Engineering and GIS
Vertex coordinates and optionally, the polyline/polygon layer and OID, are stored as ... Handle circular arcs whose central angle exceeds 180 degrees.
Read more >
Carlson Survey 2020 Embedded AutoCAD
the coordinate point number format, object linking options, and settings for the COGO portion of Carlson Software.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found