资讯专栏INFORMATION COLUMN

mongo的geo查询

Anchorer / 3622人阅读

摘要:不过这样的顺序对于使用弧度查询,很容易出错,即查询要求顺序是经度纬度,即数据和参数都是这样的顺序。对于要指定之类的入参时,使用非要注意单位换算对于使用查询的时候,以及自动设置,无需关心入参单位转换。

maven

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-data-mongodb
domain

</>复制代码

  1. @Document(collection="coffeeShop")
  2. public class CoffeeShop {
  3. @Id
  4. private String id;
  5. private String name;
  6. @GeoSpatialIndexed
  7. private double[] location;
  8. //....
  9. }
near查询

</>复制代码

  1. spherical为true则距离单位为空间弧度,false则距离单位为水平单位度
度查询

</>复制代码

  1. spherical为false,参数为公里数除以111

</>复制代码

  1. public GeoResults near2(double[] poi){
  2. NearQuery near = NearQuery
  3. .near(new Point(poi[0],poi[1]))
  4. .spherical(false)
  5. .num(1);
  6. GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
  7. return results;
  8. }

输出

</>复制代码

  1. GeoResults: [averageDistance: 0.08294719588991498, results: GeoResult [content: com.codecraft.domain.CoffeeShop@747f6c5a, distance: 0.08294719588991498, ]]

</>复制代码

  1. 不指定spherical,默认为false,结果中的dis需要乘以111换算为km

</>复制代码

  1. public GeoResults near2(double[] poi){
  2. NearQuery near = NearQuery
  3. .near(new Point(poi[0],poi[1]))
  4. .spherical(false)
  5. .distanceMultiplier(111)
  6. .num(1);
  7. GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
  8. return results;
  9. }

输出

</>复制代码

  1. 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, ]]

</>复制代码

  1. 即北京阿里绿地中心距离三里屯星巴克距离9km

若要设置最大距离,则

</>复制代码

  1. public GeoResults near2(double[] poi){
  2. NearQuery near = NearQuery
  3. .near(new Point(poi[0],poi[1]))
  4. .spherical(false)
  5. .maxDistance(5/111.0d)
  6. .distanceMultiplier(111)
  7. .num(1);
  8. GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
  9. return results;
  10. }

</>复制代码

  1. 结果为空
弧度查询

</>复制代码

  1. 需要数据存储为(经度,纬度),不然报错

</>复制代码

  1. 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 ]"
  2. at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:85)

使用

</>复制代码

  1. public GeoResults nearRadian(double[] poi){
  2. NearQuery near = NearQuery
  3. .near(new Point(poi[0],poi[1]))
  4. .spherical(true)
  5. .maxDistance(10,Metrics.KILOMETERS) //MILES以及KILOMETERS自动设置spherical(true)
  6. .distanceMultiplier(6371)
  7. .num(1);
  8. GeoResults results = mongoTemplate.geoNear(near, CoffeeShop.class);
  9. return results;
  10. }
test

</>复制代码

  1. @Test
  2. public void testInitGeo() {
  3. //http://map.yanue.net/toLatLng/
  4. CoffeeShop shop1 = new CoffeeShop("深圳市南山区星巴克(海岸城店)",new double[]{113.943442,22.52395});
  5. CoffeeShop shop2 = new CoffeeShop("广州市白云区星巴克(万达广场店)",new double[]{113.274643,23.180251});
  6. CoffeeShop shop3 = new CoffeeShop("北京市朝阳区星巴克(三里屯店)",new double[]{116.484385,39.923778});
  7. CoffeeShop shop4 = new CoffeeShop("上海市浦东新区星巴克(滨江店)",new double[]{121.638481,31.230895});
  8. CoffeeShop shop5 = new CoffeeShop("南京市鼓楼区星巴克(山西路店)",new double[]{118.788924,32.075343});
  9. CoffeeShop shop6 = new CoffeeShop("厦门市思明区星巴克(中华城店)",new double[]{118.089813,24.458157});
  10. CoffeeShop shop7 = new CoffeeShop("杭州市西湖区星巴克(杭州石函店)",new double[]{120.143005,30.280273});
  11. coffeeShopDao.save(Lists.newArrayList(shop1,shop2,shop3,shop4,shop5,shop6,shop7));
  12. }
  13. @Test
  14. public void testNear(){
  15. //经度纬度
  16. double[] bjAli = new double[]{116.492644,40.006313};
  17. double[] szAli = new double[]{113.950723,22.558888};
  18. double[] shAli = new double[]{121.387616,31.213301};
  19. double[] hzAli = new double[]{120.033345,30.286398};
  20. Arrays.asList(bjAli,szAli,shAli,hzAli).stream().forEach(d -> {
  21. GeoResults results = locationService.nearRadian(d);
  22. System.out.println(results);
  23. });
  24. }
小结

经度、纬度的坐标顺序很容易搞错,x轴是纬度,轴是经度,这也是Point定义的顺序。不过这样的顺序对于使用弧度spherical查询,很容易出错,即spherical查询要求顺序是(经度,纬度),即数据和参数都是这样的顺序。

对于只需要取最近N个的场景,使用num即可;

要使用结果中的距离时,需要注意单位换算。

对于要指定maxDistance之类的入参时,使用非spherical要注意单位换算;对于使用spherical查询的时候,MILES以及KILOMETERS自动设置spherical(true),无需关心入参单位转换。

</>复制代码

  1. 另外,对于spherical与非spherical查询,貌似没啥区别,就是spherical在使用时入参无需关心单位换算,稍微方便点。
doc

mongo.geospatial

深入浅出Symfony2 - 结合MongoDB开发LBS应用

Units to use for maxdistance and MongoDB?

Spring Data – Part 4: Geospatial Queries with MongoDB

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/18970.html

相关文章

  • 【戴嘉乐】基于IPFS和GeoHash构建具有地理位置价值服务DDApp(理论篇)

    摘要:数据将具有如下个特点将二维的经纬度转换成字符串,比如下图展示了北京个区域的字符串,分别是,等等,每一个字符串代表了某一矩形区域。例如,坐标对,位于北京安定门附近,后形成的值为。 作者简介:戴嘉乐( Mr.Maple ) | 前百度高级研发工程师 | IPFS应用实践者&布道师|个人网站:https://www.daijiale.cn联系方式:微信号:daijiale6239。 show...

    lmxdawn 评论0 收藏0
  • Elixir Ecto: 使用Geo库操作空间数据(地理坐标)

    摘要:简介数据格式空间数据的文本标识空间数据的二进制标识基于对象表示法的地理空间信息数据交换格式的库提供了上述三种格式的相互转换函数配置添加依赖配置扩展创建删除扩展的移植脚本脚本内容执行移植插入数据轨迹点粤获取经纬度粤查询联系如何查询字段例 简介 数据格式 Abbr Fullname Description WKT Well Known Text 空间数据的文本标识 WKB ...

    Blackjun 评论0 收藏0

发表评论

0条评论

Anchorer

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<