mongo的geo查詢

maven

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

domain

@Document(collection="coffeeShop")
public class CoffeeShop {

    @Id
    private String id;

    private String name;

    @GeoSpatialIndexed
    private double[] location;
    
    //....
    
}

near查詢

spherical爲true則距離單位爲空間弧度,false則距離單位爲水平單位度

度查詢

spherical爲false,參數爲千米數除以111
public GeoResults<CoffeeShop> near2(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(false)
                .num(1);
        GeoResults<CoffeeShop> results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }

輸出html

GeoResults: [averageDistance: 0.08294719588991498, results: GeoResult [content: com.codecraft.domain.CoffeeShop@747f6c5a, distance: 0.08294719588991498, ]]
不指定spherical,默認爲false,結果中的dis須要乘以111換算爲km
public GeoResults<CoffeeShop> near2(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(false)
                .distanceMultiplier(111)
                .num(1);
        GeoResults<CoffeeShop> results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }

輸出java

GeoResults: [averageDistance: 9.207138743780563 org.springframework.data.geo.CustomMetric@28768e25, results: GeoResult [content: com.codecraft.domain.CoffeeShop@310d57b1, distance: 9.207138743780563 org.springframework.data.geo.CustomMetric@28768e25, ]]
即北京阿里綠地中心距離三里屯星巴克距離9km

若要設置最大距離,則spring

public GeoResults<CoffeeShop> near2(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(false)
                .maxDistance(5/111.0d)
                .distanceMultiplier(111)
                .num(1);
        GeoResults<CoffeeShop> results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }
結果爲空

弧度查詢

須要數據存儲爲(經度,緯度),否則報錯
org.springframework.dao.DataIntegrityViolationException: Write failed with error code 16755 and error message 'Can't extract geo keys: { _id: ObjectId('58df9c50b45cbc069f6ff548'), _class: "com.codecraft.domain.CoffeeShop", name: "深圳市南山區星巴克(海岸城店)", location: [ 22.52395, 113.943442 ] }  can't project geometry into spherical CRS: [ 22.52395, 113.943442 ]'; nested exception is com.mongodb.WriteConcernException: Write failed with error code 16755 and error message 'Can't extract geo keys: { _id: ObjectId('58df9c50b45cbc069f6ff548'), _class: "com.codecraft.domain.CoffeeShop", name: "深圳市南山區星巴克(海岸城店)", location: [ 22.52395, 113.943442 ] }  can't project geometry into spherical CRS: [ 22.52395, 113.943442 ]'

    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:85)

使用mongodb

public GeoResults<CoffeeShop> nearRadian(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(true)
                .maxDistance(10,Metrics.KILOMETERS) //MILES以及KILOMETERS自動設置spherical(true)
                .distanceMultiplier(6371)
                .num(1);
        GeoResults<CoffeeShop> results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }

test

@Test
    public void testInitGeo() {
        //http://map.yanue.net/toLatLng/
        CoffeeShop shop1 = new CoffeeShop("深圳市南山區星巴克(海岸城店)",new double[]{113.943442,22.52395});
        CoffeeShop shop2 = new CoffeeShop("廣州市白雲區星巴克(萬達廣場店)",new double[]{113.274643,23.180251});
        CoffeeShop shop3 = new CoffeeShop("北京市朝陽區星巴克(三里屯店)",new double[]{116.484385,39.923778});
        CoffeeShop shop4 = new CoffeeShop("上海市浦東新區星巴克(濱江店)",new double[]{121.638481,31.230895});
        CoffeeShop shop5 = new CoffeeShop("南京市鼓樓區星巴克(山西路店)",new double[]{118.788924,32.075343});
        CoffeeShop shop6 = new CoffeeShop("廈門市思明區星巴克(中華城店)",new double[]{118.089813,24.458157});
        CoffeeShop shop7 = new CoffeeShop("杭州市西湖區星巴克(杭州石函店)",new double[]{120.143005,30.280273});

        coffeeShopDao.save(Lists.newArrayList(shop1,shop2,shop3,shop4,shop5,shop6,shop7));

    }

    @Test
    public void testNear(){
        //經度\緯度
        double[] bjAli = new double[]{116.492644,40.006313};
        double[] szAli = new double[]{113.950723,22.558888};
        double[] shAli = new double[]{121.387616,31.213301};
        double[] hzAli = new double[]{120.033345,30.286398};
        Arrays.asList(bjAli,szAli,shAli,hzAli).stream().forEach(d -> {
            GeoResults<CoffeeShop> results = locationService.nearRadian(d);
            System.out.println(results);
        });
    }

小結

  • 經度、緯度的座標順序很容易搞錯,x軸是緯度,軸是經度,這也是Point定義的順序。不過這樣的順序對於使用弧度spherical查詢,很容易出錯,即spherical查詢要求順序是(經度,緯度),即數據和參數都是這樣的順序。
  • 對於只須要取最近N個的場景,使用num便可;
  • 要使用結果中的距離時,須要注意單位換算。
  • 對於要指定maxDistance之類的入參時,使用非spherical要注意單位換算;對於使用spherical查詢的時候,MILES以及KILOMETERS自動設置spherical(true),無需關心入參單位轉換。
另外,對於spherical與非spherical查詢,貌似沒啥區別,就是spherical在使用時入參無需關心單位換算,稍微方便點。

doc

相關文章
相關標籤/搜索