UNPKG

laneify

Version:

Create offset GeoJSON LineString features (lanes) based on an OSM way (road)

77 lines (63 loc) 1.59 kB
'use strict'; var Point = function (x, y) { return { x: x, y: y } } var Line = function (point1, point2) { return { p1: point1, p2: point2 } } function add(point1, point2) { return new Point(point1.x + point2.x, point1.y + point2.y) } function multiply(point, scalar) { return new Point(point.x * scalar, point.y * scalar) } function getVector(line) { var p1 = line.p1, p2 = line.p2 return new Point((p1.x - p2.x), (p1.y - p2.y)) } function vectorLength(vector) { return Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2)) } /** * In the simple case of 90 degree rotation, the vector rotation * transform simplifies to x' = -y, y' = x * @param {[type]} vec [description] * @return {[type]} [description] */ function normalVector(vec) { var newX = -vec.y var newY = vec.x return new Point(newX, newY) } /** * Normalising a vector turns it into the unit vector * representation of itself, i.e. it's length is 1 * @param {Point} vector * @return {Point} Unit vector */ function normalise(vector) { var length = vectorLength(vector) return new Point(vector.x/length, vector.y/length) } var geoTools = { createLine: function getLine(p1, p2) { return new Line(p1, p2) }, createPoint: function getPoint(x, y) { return new Point(x, y) }, unitNormal: function getUnitNormal(line) { return normalise(normalVector(getVector(line))) }, translate: function translatePoint(point, unitVector, distance) { return add(point, multiply(unitVector, distance)) } } module.exports = geoTools