成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

mongo的geo查詢

Anchorer / 3610人閱讀

摘要:不過這樣的順序?qū)τ谑褂没《炔樵?,很容易出錯(cuò),即查詢要求順序是經(jīng)度緯度,即數(shù)據(jù)和參數(shù)都是這樣的順序。對于要指定之類的入?yún)r(shí),使用非要注意單位換算對于使用查詢的時(shí)候,以及自動(dòng)設(shè)置,無需關(guān)心入?yún)挝晦D(zhuǎn)換。

maven
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
domain
@Document(collection="coffeeShop")
public class CoffeeShop {

    @Id
    private String id;

    private String name;

    @GeoSpatialIndexed
    private double[] location;
    
    //....
    
}    
near查詢
spherical為true則距離單位為空間弧度,false則距離單位為水平單位度
度查詢
spherical為false,參數(shù)為公里數(shù)除以111
public GeoResults near2(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(false)
                .num(1);
        GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }

輸出

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

輸出

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

若要設(shè)置最大距離,則

public GeoResults near2(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(false)
                .maxDistance(5/111.0d)
                .distanceMultiplier(111)
                .num(1);
        GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }
結(jié)果為空
弧度查詢
需要數(shù)據(jù)存儲為(經(jīng)度,緯度),不然報(bào)錯(cuò)
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: "深圳市南山區(qū)星巴克(海岸城店)", 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: "深圳市南山區(qū)星巴克(海岸城店)", 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)

使用

public GeoResults nearRadian(double[] poi){
        NearQuery near = NearQuery
                .near(new Point(poi[0],poi[1]))
                .spherical(true)
                .maxDistance(10,Metrics.KILOMETERS) //MILES以及KILOMETERS自動(dòng)設(shè)置spherical(true)
                .distanceMultiplier(6371)
                .num(1);
        GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
        return results;
    }
test
@Test
    public void testInitGeo() {
        //http://map.yanue.net/toLatLng/
        CoffeeShop shop1 = new CoffeeShop("深圳市南山區(qū)星巴克(海岸城店)",new double[]{113.943442,22.52395});
        CoffeeShop shop2 = new CoffeeShop("廣州市白云區(qū)星巴克(萬達(dá)廣場店)",new double[]{113.274643,23.180251});
        CoffeeShop shop3 = new CoffeeShop("北京市朝陽區(qū)星巴克(三里屯店)",new double[]{116.484385,39.923778});
        CoffeeShop shop4 = new CoffeeShop("上海市浦東新區(qū)星巴克(濱江店)",new double[]{121.638481,31.230895});
        CoffeeShop shop5 = new CoffeeShop("南京市鼓樓區(qū)星巴克(山西路店)",new double[]{118.788924,32.075343});
        CoffeeShop shop6 = new CoffeeShop("廈門市思明區(qū)星巴克(中華城店)",new double[]{118.089813,24.458157});
        CoffeeShop shop7 = new CoffeeShop("杭州市西湖區(qū)星巴克(杭州石函店)",new double[]{120.143005,30.280273});

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

    }

    @Test
    public void testNear(){
        //經(jīng)度緯度
        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 results = locationService.nearRadian(d);
            System.out.println(results);
        });
    }
小結(jié)

經(jīng)度、緯度的坐標(biāo)順序很容易搞錯(cuò),x軸是緯度,軸是經(jīng)度,這也是Point定義的順序。不過這樣的順序?qū)τ谑褂没《萻pherical查詢,很容易出錯(cuò),即spherical查詢要求順序是(經(jīng)度,緯度),即數(shù)據(jù)和參數(shù)都是這樣的順序。

對于只需要取最近N個(gè)的場景,使用num即可;

要使用結(jié)果中的距離時(shí),需要注意單位換算。

對于要指定maxDistance之類的入?yún)r(shí),使用非spherical要注意單位換算;對于使用spherical查詢的時(shí)候,MILES以及KILOMETERS自動(dòng)設(shè)置spherical(true),無需關(guān)心入?yún)挝晦D(zhuǎn)換。

另外,對于spherical與非spherical查詢,貌似沒啥區(qū)別,就是spherical在使用時(shí)入?yún)o需關(guān)心單位換算,稍微方便點(diǎn)。
doc

mongo.geospatial

深入淺出Symfony2 - 結(jié)合MongoDB開發(fā)LBS應(yīng)用

Units to use for maxdistance and MongoDB?

Spring Data – Part 4: Geospatial Queries with MongoDB

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/18970.html

相關(guān)文章

  • 【戴嘉樂】基于IPFS和GeoHash構(gòu)建具有地理位置價(jià)值服務(wù)DDApp(理論篇)

    摘要:數(shù)據(jù)將具有如下個(gè)特點(diǎn)將二維的經(jīng)緯度轉(zhuǎn)換成字符串,比如下圖展示了北京個(gè)區(qū)域的字符串,分別是,等等,每一個(gè)字符串代表了某一矩形區(qū)域。例如,坐標(biāo)對,位于北京安定門附近,后形成的值為。 作者簡介:戴嘉樂( Mr.Maple ) | 前百度高級研發(fā)工程師 | IPFS應(yīng)用實(shí)踐者&布道師|個(gè)人網(wǎng)站:https://www.daijiale.cn聯(lián)系方式:微信號:daijiale6239。 show...

    lmxdawn 評論0 收藏0
  • Elixir Ecto: 使用Geo庫操作空間數(shù)據(jù)(地理坐標(biāo))

    摘要:簡介數(shù)據(jù)格式空間數(shù)據(jù)的文本標(biāo)識空間數(shù)據(jù)的二進(jìn)制標(biāo)識基于對象表示法的地理空間信息數(shù)據(jù)交換格式的庫提供了上述三種格式的相互轉(zhuǎn)換函數(shù)配置添加依賴配置擴(kuò)展創(chuàng)建刪除擴(kuò)展的移植腳本腳本內(nèi)容執(zhí)行移植插入數(shù)據(jù)軌跡點(diǎn)粵獲取經(jīng)緯度粵查詢聯(lián)系如何查詢字段例 簡介 數(shù)據(jù)格式 Abbr Fullname Description WKT Well Known Text 空間數(shù)據(jù)的文本標(biāo)識 WKB ...

    Blackjun 評論0 收藏0

發(fā)表評論

0條評論

Anchorer

|高級講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<