Skip to content

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

@aj8k

Description

@aj8k

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions