如何计算MongoDB中许多GeoJSON点之间的路径距离?

时间:2017-08-26 04:12:27

标签: mongodb geolocation mongodb-query database

如何计算MongoDB中许多GeoJSON点之间的路径距离?我可以使用数据库查询按日期字段对项目进行排序,然后计算点之间的距离,最后总计所有时间以计算总距离吗?

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

{ 
 _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,
}

1 个答案:

答案 0 :(得分:2)

正如评论中所指出的,将尝试使用Java在此处绘制类似的图片。假设您的数据库名称dput(df) structure(list(X0 = c(1L, 11L, 21L, 31L, 41L), Y0 = c(2L, 12L, 22L, 32L, 42L), X1 = c(3L, 13L, 23L, 33L, 43L), Y1 = c(4L, 14L, 24L, 34L, 44L), X2 = c(5L, 15L, 25L, 35L, 45L), Y2 = c(6L, 16L, 26L, 36L, 46L), X3 = c(7L, 17L, 27L, 37L, 47L), Y3 = c(8L, 18L, 28L, 38L, 48L), X4 = c(9L, 19L, 29L, 39L, 49L), Y4 = c(10L, 20L, 30L, 40L, 50L)), .Names = c("X0", "Y0", "X1", "Y1", "X2", "Y2", "X3", "Y3", "X4", "Y4"), class = "data.frame", row.names = c(NA, -5L)) 和集合名称为db,文档类型为col,可以将其建模为:

GeoData

它将如下进行:

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

    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;
    }
    
  2. 使用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]
  3. 将所有这些相加以计算路线距离

    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));
    }