0

I can calculate distances in MongoDB from Point to Point, using 2dsphere index.

However, now I need the calculate the distance from a given point to a Polygon.

I'm using the following code :

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

My 2dsphere index is the coordinates of the Polygon. The results are not consistent as in Point to Point distances.

I appreciate your feedback,

Undroider
  • 21
  • 5

1 Answers1

0

I solved the issue using mongo driver:

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))), Location.class);

This gives me the correct distances from a Point up to the closest Point in a GeoJsonPolygon.

A 2dsphere index must be created in GeoJsonPolygon field in the collection.

Undroider
  • 21
  • 5