二维几何计算 - 在平面上

2D geometry calculation - on flat surface

我正在尝试在二维平面上进行一些几何计算(例如点到线的距离)。

目前正在研究turfjs计算点到线的距离。


代码

distance.js:

// hello
var turf = require('@turf/turf')

init();

// init,
function init() {
    distance();
}

function distance() {
    // line
    let line = turf.lineString([
    [0, 0],
    [3, 4]
    ]);
    console.log(line);

    // point
    let point = turf.point([3, 0]);
    console.log(point);

    // calculate distance from point to line, using planar,
    let dis = turf.pointToLineDistance(point, line, {
    units: 'degrees',
    method: 'planar'
    });
    console.log("distance: %f", dis);
}

输出

distance: 2.398995395932417

如果我将点更改为 [30, 0] 并将行更改为 [0, 0], [30, 40],则输出为:

distance: 25.741472914575986

程序使用 degrees 作为单位,planar 作为方法。

预期的结果是 2.424,但事实并非如此。


问题

如果您查看 turf.js source code for point to line distance, you'll see that it calculates planar distances along a rhumb line,那不是您在平面几何中所期望的。

要实现您想要的效果,您可以自己实现 equation,例如

function pointToLineDistance(line, p0) {
    var p1 = line[0],
        p2 = line[1];

    var x0 = p0[0], y0 = p0[1],
        x1 = p1[0], y1 = p1[1],
        x2 = p2[0], y2 = p2[1]


    return Math.abs(
        (y2 - y1) * x0
        - (x2 - x1) * y0
        + x2 * y1
        - y2 * x1
    ) 
    /
    Math.sqrt(
        (y2 - y1) * (y2 - y1)
        +
        (x2 - x1) * (x2 - x1)
    );
}

console.log(
    pointToLineDistance([[0, 0], [3, 4]], [3, 0]),
    pointToLineDistance([[0, 0], [30, 40]], [30, 0]),
);

或者,如果您更喜欢图书馆,flatten.js 之类的东西可以帮助您:

let Flatten = require('flatten-js');

let {point, line} = Flatten;

let l = line(point(0, 0), point(3, 4));
let p = point(3, 0)
let d = p.distanceTo(l);

console.log(d[0]);

Turf.js 旨在用于墨卡托(网络,2D)地图,但对于计算,它基于 WSG84 (3D) standards, i.e. considers the Earth a spheroid. Some times though, particularly on wide areas, representations and operations on spherical features on a 2D map return unintuitive or unexpected results; in these cases we adopted Rhumb 甚至欧几里德运算(因此 planar 选项 pointToLineDistance 例如),这有时会导致准确性降低。

因此,为了回答您的第一个问题,Turf 会考虑您在曲面上的点并回答您的第二个问题,无法指示 Turf 在平面上工作。

至于替代库,您可能要考虑使用多个;除了已经建议的 flatten.js 之外,您还可以查看 jsts 以了解并集和相交等操作。

如果您不限于 Javascript,您可能想看看 Python Shapely,在我看来这是一个非常强大和可靠的工具。

以上两个库都在少数 Turf 包中使用或用作实现 and/or 结果的参考。 关于 jsts 的注释,它已在 Turf 中显示 some issues,但您可能会接受它,具体取决于您的案例的复杂程度。