[GIS] How to find 2 parallel lines with given line

geometryjavascriptleaflet

I have a line that is drawn on a map.
I want to find 2 parallel lines with this one, one at 50m above this and one at 50m below this.
Something like in the picture below

1]

In the end I want to transform the line in a rectangle of width 100m.

I have tried some implementations, but I can't get it right.

How can I achieve this, keeping in mind that the line can have a length of hundreds of kilometers?

Edit:

  function getBearing(a, b) {
    const TWOPI = 6.2831853071795865;
    const RAD2DEG = 57.2957795130823209;
    var theta = Math.atan2(b.lat - a.lat, a.lng - b.lng);
    if (theta < 0.0)
      theta += TWOPI;
    console.log(a, b);
    return RAD2DEG * theta;
  }

  function getPolygonForLine(start, end, distance) {
    var bearing = getBearing(start, end);
    distance /= 2;
    var dx = distance * Math.cos(bearing);
    var dy = distance * Math.sin(bearing);

    var delta = {
      lat: dx / (111320 * Math.cos(start.lat)),
      lng: dy / 110540
    };

    var bounds = [
      L.polygon(getBoundsForPoint(start, delta)).getBounds(),
    ];

    var points = [];
    _.forEach(bounds, function(val) {
      points.push(
        val.getNorthEast(),
        val.getNorthWest(),
        val.getSouthWest(),
        val.getSouthEast()
      );
    });
  }

  function getBoundsForPoint(point, delta) {
    var points = [
      [
        point.lat + delta.lat,
        point.lng + delta.lng
      ],
      [
        point.lat - delta.lat,
        point.lng - delta.lng
      ],
      [
        point.lat + delta.lat,
        point.lng - delta.lng
      ],
      [
        point.lat - delta.lat,
        point.lng + delta.lng
      ],
      [
        point.lat - delta.lat,
        point.lng
      ],
      [
        point.lat + delta.lat,
        point.lng
      ],
      [
        point.lat,
        point.lng - delta.lng
      ],
      [
        point.lat,
        point.lng + delta.lng
      ]
    ];
    return points;
  }

This is the code that I have at the moment(I left only the code that calculates one end of the line). The problem is that the rectangle seems to not have the desired width and it also does not form a 90 degree angle with the line (it is parallel with the edge of the screen).

Best Answer

Have you tried creating a 50m buffer around the line? I believe this tool should be available on almost any kind of GIS software.

Related Question