Spring-Data-MongoDB 坐标操作——Geo2d

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() + "]");
        }
    }
}

 

posted @ 2019-06-04 14:24  晨M风  阅读(1419)  评论(0编辑  收藏  举报