Spring-Data-MongoDB coordinate operation

1. In the coordinate field entity class annotate @GeoSpatialIndexed

@Document(collection="location")
public class LocationPO extends BaseEntity {

    @Id
    private ObjectId id;
    @GeoSpatialIndexed
    private double[] loc;

    public LocationPO(){}

    public LocationPO(double x, double y){
        loc = new double[]{x, y};
    }

    public ObjectId getId() {
        return this.id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public double[] getLoc() {
        return loc;
    }

    public void setLoc(double[] loc) {
        this.loc = loc;
    }
}

 

2. Use in DAO

@Repository
public class LocationDAO {
    @Resource
    protected MongoTemplate mongoTemplate;

    /**
     * 添加
     */
    public void addLocation(){
        List<LocationPO> locationList = new ArrayList<>();
        LocationPO location = new LocationPO(1,1);
        locationList.add(location);

        location = new LocationPO(2,2);
        locationList.add(location);

        location = new LocationPO(5,5);
        locationList.add(location);

        location = new LocationPO(5,10);
        locationList.add(location);

        location = new LocationPO(10,5);
        locationList.add(location);

        location = new LocationPO(10,10);
        locationList.add(location);

        mongoTemplate.insertAll(locationList);
    }

    /**
     * 圆形查询(几何)
     */
    public void getCircle(){
        Circle circle = new Circle(6, 6, 5);
        List<LocationPO> locationList = mongoTemplate.find(new Query(Criteria.where("loc").within(circle)), LocationPO.class);
        for(LocationPO location : locationList){
            System.out.println("坐标: [" + location.getLoc()[0] + ", " + location.getLoc()[1] + "]");
        }
    }

    /**
     * 球形查询(坐标)
     */
    public void getSphere() {
        Circle circle = new Circle(6,6, 0.08);
        List<LocationPO> locationList = mongoTemplate.find(new Query(Criteria.where("loc").withinSphere(circle)), LocationPO.class);
        for(LocationPO location : locationList){
            System.out.println("坐标: [" + location.getLoc()[0] + ", " + location.getLoc()[1] + "]");
        }
    }

    /**
     * 矩形查询(几何)
     */
    public void getBox() {
        Box box = new Box(new Point(5, 5), new Point(10, 10));
        List<LocationPO> locationList = mongoTemplate.find(new Query(Criteria.where("loc").within(box)), LocationPO.class);
        for(LocationPO location : locationList){
            System.out.println("坐标: [" + location.getLoc()[0] + ", " + location.getLoc()[1] + "]");
        }
    }

    /**
     * 直线查询(几何)
     */
    public void getPoint() {
        Point point = new Point(6, 6);
        List<LocationPO> locationList = mongoTemplate.find(new Query(Criteria.where("loc").near(point).maxDistance(5)), LocationPO.class);
//        List<LocationPO> locationList = mongoTemplate.find(new Query(Criteria.where("loc").near(point).minDistance(5)), LocationPO.class);
        for(LocationPO location : locationList){
            System.out.println("坐标: [" + location.getLoc()[0] + ", " + location.getLoc()[1] + "]");
        }
    }

    /**
     * 球面直线查询(坐标)
     */
    public void getPointSphere() {
        Point point = new Point(6, 6);
        List<LocationPO> locationList = mongoTemplate.find(new Query(Criteria.where("loc").nearSphere(point).maxDistance(0.08)), LocationPO.class);
        for(LocationPO location : locationList){
            System.out.println("坐标: [" + location.getLoc()[0] + ", " + location.getLoc()[1] + "]");
        }
    }

    /**
     * 距离查询(坐标)
     */
    public void getNearest(){
        Point point = new Point(6, 6);
        NearQuery query = NearQuery.near(point).maxDistance(new Distance(500, Metrics.KILOMETERS));
        GeoResults<LocationPO> result = mongoTemplate.geoNear(query, LocationPO.class);
        List<GeoResult<LocationPO>> resultList = result.getContent();
        for(GeoResult<LocationPO> geoResult : resultList){
            LocationPO location = geoResult.getContent();
            System.out.println("坐标: [" + location.getLoc()[0] + ", " + location.getLoc()[1] + "], 距离: [" + geoResult.getDistance() + "]");
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/vettel0329/p/10973353.html