如何计算MongoDB中许多GeoJSON点之间的路由距离? [英] How do I calculate route distance between many GeoJSON points in MongoDB?

查看:351
本文介绍了如何计算MongoDB中许多GeoJSON点之间的路由距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算MongoDB中许多GeoJSON点之间的路由距离?我是否可以通过数据库查询按日期字段对项目进行排序,然后计算点之间的距离,最后对所有结果求和以计算总距离?

How do I calculate route distance between many GeoJSON points in MongoDB? Can I have a database query that sort items by their date field, then calculates the distance between points and finally sums all of then to calculate the total distance?

以下是我的数据的一些示例:

Here are some examples of my data:

{ 
 _id: 599cfc236ed0d81c98007f66
 tracerId: 59a07ea26ed0d81d78001acd
 loc { 
      type: "2dsphere",
      coordinates: [ 159.9, -37.92 ]
     },
 date: 2017-08-26 00:16:42,
 speed: 58,
}
{ 
 _id: 59a074d46ed0d81d78001acc
 tracerId: 59a07ea26ed0d81d78001acd
 loc { 
      type: "2dsphere",
      coordinates: [ 160, -38.20 ]
     },
 date: 2017-08-26 00:18:42,
 speed: 75,
}
{ 
 _id: 59a074d46ed0d81d78ac11cc
 tracerId: 59a07ea26ed0d81d78001acd
 loc { 
      type: "2dsphere",
      coordinates: [ 160.222, -38.92 ]
     },
 date: 2017-08-26 00:20:42,
 speed: 60,
}

正如注释中所指出的,

推荐答案

也将尝试使用Java在此处绘制类似的图片.假设您的数据库名称为db,集合名称为col,文档类型为GeoData,可以将其建模为:

As pointed out in the comments as well, would try to draw a similar picture here using Java. Assuming your database name db and collection name as col and the document type as GeoData which could be modelled as :

public class GeoData {
    String tracerId;
    Location loc;
    Date date;
    Integer speed;
    ...getters, setters and other overrides
}

public class Location {
    String type;
    Coordinate coordinates;
}

public class Coordinate {
    double x;
    double y;
}

它将进行如下操作:

  1. 按日期字段对项目进行排序(例如,按升序排列)

  1. Sort items by date field (let's say in ascending order)

MongoDatabase database = getDatabase("db");
MongoCollection<GeoData> collection = database.getCollection("col", GeoData.class);
Bson sortFilter = Filters.eq("date", "1"); //sort ascending
List<GeoData> geoData = Lists.newArrayList(collection.find().sort(sortFilter));

  • 使用c = square root of [(xA-xB)^2+(yA-yB)^2]

    private static double distanceBetweenCoordinates(Coordinate a, Coordinate b) {
        return Math.sqrt(Math.pow(b.getX() - a.getX(), 2) + Math.pow(b.getY() - a.getY(),2));
    }
    

  • 将它们全部求和以计算路线距离

  • Sum all of them to calculate route distance

    double routeDist = 0.0;
    for (int i = 0; i < geoData.size()-1; i++) {
        routeDist += distanceBetweenCoordinates(geoData.get(i+1).getLoc().getCoordinates(), geoData.get(i+1).getLoc().getCoordinates());
    } 
    

  • 这篇关于如何计算MongoDB中许多GeoJSON点之间的路由距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆