确定两个坐标是否在期望的半径内

时间:2019-07-04 02:14:44

标签: javascript mysql node.js express sequelize.js

我正在将城市的主要交叉路口与城市的碰撞数据进行汇总。要完成的工作是确定在20米半径内的交叉路口及其周围发生的事故数量。

幸运的是,我已经在项目中走得很远了,我的数据库intersectionscollisions中有两个表

我的intersections表:

 --------------------------------------------
 |  id  |  city  |  Latitude  |  Longitude  |
 --------------------------------------------
 |  1   |   1    |   34.44444 |   84.3434   |
 --------------------------------------------
 |  2   |   1    | 42.4666667 | 1.4666667   |
 --------------------------------------------
 |  3   |   1    |  32.534167 | 66.078056   |
 --------------------------------------------
 |  4   |   1    |  36.948889 | 66.328611   |
 --------------------------------------------
 |  5   |   1    |  35.088056 | 69.046389   |
 --------------------------------------------
 |  6   |   1    |  36.083056 |   69.0525   |
 --------------------------------------------
 |  7   |   1    |  31.015833 | 61.860278   |
 --------------------------------------------

MY collisions表:

--------------------------------------------
 |  id  |  cause |  Latitude  |  Longitude  |
 --------------------------------------------
 |  1   |   1    |   44.44444 |   81.3434   |
 --------------------------------------------
 |  2   |   1    | 32.4666667 | 1.4666667   |
 --------------------------------------------
 |  3   |   1    |  42.534167 | 63.078056   |
 --------------------------------------------
 |  4   |   1    |  46.948889 | 62.328611   |
 --------------------------------------------
 |  5   |   1    |  45.088056 | 61.046389   |
 --------------------------------------------
 |  6   |   1    |  46.083056 |   63.0525   |
 --------------------------------------------
 |  7   |   1    |  41.015833 | 69.860278   |
 --------------------------------------------

注意:为简单起见,省略了某些字段

如您所见,两个表都具有latitudelongitude字段。现在要确定 交点 碰撞 坐标是否紧密,我想到了使用一个带有id的MySQL查询 交叉点 的查询,然后查询collisions表以获取 交叉点 20米以内的所有碰撞>。

此查询是什么(在MySQL或Sequelize中)?

这就是我现在拥有的(使用MySQL数据库在Sequelize中)

// api/collisions/{intersection_id}/count

exports.intersection_accident_count = (req, res) => {
    intersection.findOne({where: {equipement: req.params.intersection_id}}).then(intersection => {

        let intersectionLongitude = intersection.longitude;
        let intersectionLatitude = intersection.latitude;

        collision.count({where: {
            longitude: {
                $gt: intersectionLongitude
            },
            latitude: {
                $gt: intersectionLatitude
            },
        }}).then(count => {
            res.status(200).json({'number_of_accidents': count});
        });
    });
};

1 个答案:

答案 0 :(得分:1)

您应该以简单的方式开始:

sort(sequence(rep(length(x), length(x))))
#[1] 1 1 1 2 2 2 3 3 3

这应该可以为您提供结果,但是查询根本不会使用任何索引,并且如果您有很多记录,这将非常慢。

您应该考虑在表中添加几何数据类型:

SELECT
    i.*
FROM
    intersections AS i
    INNER JOIN collisions AS c ON (
        ST_Distance_Sphere(POINT(i.Longitude, i.Latitude), POINT(c.Longitude, c.Latitude)) < @dist
    )

您可以添加触发器,以便在更新经度/纬度时自动填充位置。

然后:

ALTER TABLE `collisions` ADD COLUMN `Location` POINT;
UPDATE `collisions` SET Location = POINT(Longitude, Latitude);
ALTER TABLE `intersections` ADD COLUMN `Location` POINT;
UPDATE `intersections` SET Location = POINT(Longitude, Latitude);

这会更快一点。

如果要使其更快,可以在交点表和“预构建”区域中添加另一个几何形状多边形SELECT i.* FROM intersections AS i INNER JOIN collisions AS c ON ( ST_Distance_Sphere(i.Location, c.Location) < @dist ) 列(可以为精确度为圆形或为速度平方)。 之后,您将可以执行以下操作:

Area