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.

line offset is misaligned

See original GitHub issue

I used https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js

      var line = {'type': 'LineString', 'coordinates': [
        [
          19.925,
          50.0555
        ],
        [
          19.924,
          50.055
        ]
      ]}
      var a = turf.lineOffset(line, 50/1000, {units: 'kilometers'})
      var b = turf.lineOffset(line, -50/1000, {units: 'kilometers'})
      console.warn({'type': 'MultiLineString', 'coordinates': [line.coordinates, a.geometry.coordinates, b.geometry.coordinates]})

produces

screen04

while I would expect what is here marked with a blue lines to be produced as offsets

screen04 Not sure whether it is a bug or I misunderstood documentation http://turfjs.org/docs/#lineOffset

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
Uolccommented, Sep 27, 2021

Here’s my code to get correct parallel line with more than 2 points :

const getLineOffset = (line, distance, { units = 'kilometers' } = {}) => {
    const lineCoords = line.geometry.coordinates;
    const transformAngle = distance < 0 ? -90 : 90;
    if (distance < 0) distance = -distance;

    const offsetLines = [];
    for (let i = 0; i < lineCoords.length - 1; i++) { // Translating each segment of the line to correct position
        const angle = bearing(lineCoords[i], lineCoords[i + 1]) + transformAngle;
        const firstPoint = transformTranslate(point(lineCoords[i]), distance, angle, { units })?.geometry.coordinates;
        const secondPoint = transformTranslate(point(lineCoords[i + 1]), distance, angle, { units })?.geometry.coordinates;
        offsetLines.push([firstPoint, secondPoint]);
    }

    const offsetCoords = [offsetLines[0][0]]; // First point inserted
    for (let i = 0; i < offsetLines.length; i++) { // For each translated segment of the initial line
        if (offsetLines[i + 1]) { // If there's another segment after this one
            const firstLine = transformScale(lineString(offsetLines[i]), 2); // transformScale is useful in case the two segment don't have an intersection point
            const secondLine = transformScale(lineString(offsetLines[i + 1]), 2); // Which happen when the resulting offset line is bigger than the initial one
            // We're calculating the intersection point between the two translated & scaled segments
            offsetCoords.push(lineIntersect(firstLine, secondLine).features[0].geometry.coordinates);
        } else offsetCoords.push(offsetLines[i][1]); // If there's no other segment after this one, we simply push the last point of the line
    }

    return lineString(offsetCoords);
};
0reactions
nzjonycommented, May 16, 2022

I had the same issue, I even tried the code above, i.e. the getLineOffset function and when compared to the lineOffset function, there is actually very little difference, see the attached screenshots. Note, this is using a mercator projection. Screenshot 2022-05-16 at 20 48 06 Screenshot 2022-05-16 at 20 46 38

I then graphed the output of the above function getLineOffset in desmos and I get the following: Screenshot 2022-05-16 at 20 54 34

So the problem I’m having in my case is that I’m displacing the units in geographic coordinates and that shift doesn’t work in Mercator. So I will need to shift in mercator space. Unless I’ve missed something.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Types of Common Misalignment: How to Recognise & Fix Them
This is a compound misalignment that occurs when one shaft is both offset and angled differently than the other shaft along a horizontal...
Read more >
misaligned offset notes | MuseScore
When adjusting a second voice to beam between two staves, the alignment of the first and second voices superimposes them instead of offsetting...
Read more >
help with misaligned offset : r/cricut - Reddit
Another thing to try, and I know it sounds strange, but when you go to make the project, in the preview, rotate it...
Read more >
Shaft Alignment Concepts: Offset & Angularity - YouTube
Shaft alignment of rotating equipment is an essential part of machinery maintenance. This machine shaft alignment video presents the ...
Read more >
4 Ways to Diagnose and Correct Shaft Misalignment
Misalignment occurs when two rotating shafts are not parallel to one another, whether it be an offset or an angular gap at the...
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