首页 > 数据库技术 > 详细

SpringDataMongoDB坐标操作

时间:2019-06-04 14:30:39      阅读:125      评论:0      收藏:0      [点我收藏+]

1.在实体类的坐标字段加上注解@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.在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() + "]");
        }
    }
}

 

SpringDataMongoDB坐标操作

原文:https://www.cnblogs.com/vettel0329/p/10973353.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!