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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM