【问题标题】:Calculate distance from Polygon to Point in MongoDB在MongoDB中计算从多边形到点的距离
【发布时间】:2018-09-25 12:39:42
【问题描述】:

我可以使用 2dsphere 索引在 MongoDB 中计算点到点的距离。

但是,现在我需要计算从给定点到多边形的距离。

我正在使用以下代码:

  Document commandNoIntersect = new Document("geoNear", "Location")
                            .append("near", new com.mongodb.client.model.geojson.Point(new Position(Longitude, Latitude)))
                            .append("spherical", true)
                            .append("query", new Document(new Document("location", locationId)))
                            .append("distanceMultiplier", 0.001);

database.runCommand(commandNoIntersect);

我的 2dsphere 索引是多边形的坐标。结果与点到点距离不一致。

感谢您的反馈,

【问题讨论】:

    标签: mongodb polygon distance geonear


    【解决方案1】:

    我使用 mongo 驱动解决了这个问题:

    mongoTemplate.geoNear( NearQuery.near(new Point(new Point(pin.getDinamycPoint().getPointId().getY(), pin.getDinamycPoint().getPointId().getX()))).spherical(true) .inKilometers().query(new Query().addCriteria(Criteria.where("locationId").is(locationId))), 位置.class);

    这为我提供了从点到 GeoJsonPolygon 中最近点的正确距离。

    必须在集合的 GeoJsonPolygon 字段中创建 2dsphere 索引。

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 2010-10-28
      • 2023-03-25
      • 2012-06-14
      • 2017-02-21
      • 2018-06-25
      • 2022-08-06
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多