设为首页 收藏本站
查看: 1046|回复: 0

[经验分享] mongodb-mongotemplate进行地理坐标操作

[复制链接]

尚未签到

发表于 2017-12-15 20:06:25 | 显示全部楼层 |阅读模式
  因为项目中使用的springboot + mongotemplate, 所以还是需要mongotemplate的操作方式
  首先建立一个bean:
  

package com.iwhere.easy.travel.entity;  

  
import java.io.Serializable;
  
import java.util.Arrays;
  

  
import org.springframework.data.annotation.Id;
  
import org.springframework.data.annotation.PersistenceConstructor;
  
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
  
import org.springframework.data.mongodb.core.index.Indexed;
  
import org.springframework.data.mongodb.core.mapping.Document;
  

  

/**  * 收费poi
  *
  * @author wenbronk
  * @time 2017年7月19日 下午4:46:39
*/  

  
@Document(collection
= "charge_poi")  

public>private static final long serialVersionUID = 2653147280472201924L;  

  @Id
private String _id;  

  @Indexed
private String poi_id;private String poi_name;  @GeoSpatialIndexed
private Double[] location;private String media_url;private Double price;  

public ChargePoi() {  super();
  }
  

  @PersistenceConstructor
  ChargePoi(String _id, String poi_id, String poi_name, Double[] location, String media_url, Double price) {
  super();
this._id = _id;this.poi_id = poi_id;this.poi_name = poi_name;this.location = location;this.media_url = media_url;this.price = price;  }
  

public ChargePoi(String _id, String poi_id, String poi_name, Double x, Double y, String media_url, Double price) {  super();
this._id = _id;this.poi_id = poi_id;this.poi_name = poi_name;this.location = new Double[]{x, y};this.media_url = media_url;this.price = price;  }
  

public String get_id() {return _id;  }
  

public void set_id(String _id) {this._id = _id;  }
  

public String getPoi_id() {return poi_id;  }
  

public void setPoi_id(String poi_id) {this.poi_id = poi_id;  }
  

public String getPoi_name() {return poi_name;  }
  

public void setPoi_name(String poi_name) {this.poi_name = poi_name;  }
  

public Double[] getLocation() {return location;  }
  

public void setLocation(Double[] location) {this.location = location;  }
  

public String getMedia_url() {return media_url;  }
  

public void setMedia_url(String media_url) {this.media_url = media_url;  }
  

public Double getPrice() {return price;  }
  

public void setPrice(Double price) {this.price = price;  }
  

  @Override
public String toString() {return "ChargePoi [_id=" + _id + ", poi_id=" + poi_id + ", poi_name=" + poi_name + ", location="  + Arrays.toString(location) + ", media_url=" + media_url + ", price=" + price + "]";
  }
  
}
  

  注:
  

@GeoSpatialIndexed  注解的作用:   

  Mark a field to be indexed using MongoDB's geospatial indexing feature.
  开始没有加这个注解, 然后计算距离的时候就会报错
  

  1, 准备测试数据:
  

    /**  * save
*/  @Test
public void test1() {for (int x = 100; x < 131; x++) {for (int y = 30; y < 61; y++) {  Double loca[]
= new Double[]{Double.valueOf(x), Double.valueOf(y)};  ChargePoi chargePoi
= new ChargePoi();  chargePoi.setPoi_id(
"poiid" + x);  chargePoi.setMedia_url(
"http://www.baidu.com?params=" + x);  chargePoi.setPoi_name(
"vini" + Arrays.toString(loca));  chargePoi.setPrice(Math.random()
* 100);  chargePoi.setLocation(loca);
  mongoTemplate.insert(chargePoi);
  }
  }
  }
  

  2, 圆形查询
  

    /**  * circle
*/  @Test
public void test2() {  Circle circle
= new Circle(30, 20, 20);  List
<ChargePoi> find = mongoTemplate.find(new Query(Criteria.where("location").within(circle)), ChargePoi.class);  System.
out.println(find);  System.
out.println(find.size());  }
  

  3, 球星查询
  

    /**  * spherical
*/  @Test
public void test3() {  Circle circle
= new Circle(30,20, 20);  List
<ChargePoi> find = mongoTemplate.find(new Query(Criteria.where("location").withinSphere(circle)), ChargePoi.class);  System.
out.println(find.size());  System.
out.println(find);  }
  

  4, 矩形查询, box
  

    /**  * box
*/  @Test
public void test4() {  Box box
= new Box(new Point(10, 11), new Point(10, 20));  List
<ChargePoi> find =  mongoTemplate.find(
new Query(Criteria.where("location").within(box)), ChargePoi.class);  System.
out.println(find.size());  System.
out.println(find);  }
  

  5, 按距离由近到元查询
  

    /**  * near
*/  @Test
public void test5() {  Point point
= new Point(12, 12);  List
<ChargePoi> venues =  mongoTemplate.find(
new Query(Criteria.where("location").near(point).maxDistance(20)), ChargePoi.class);  System.
out.println(venues.size());  System.
out.println(venues);  }
  

  6, 空间距离查询
  

    /**  * nearSphere
*/  @Test
public void test6() {  Point point
= new Point(12, 12);  List
<ChargePoi> venues =  mongoTemplate.find(
new Query(Criteria.where("location").nearSphere(point).maxDistance(20)), ChargePoi.class);  System.
out.println(venues.size());  System.
out.println(venues);  }
  

  7, 最近点查询
  

    @Testpublic void test7() {  Point location
= new Point(12, 12);  NearQuery query
= NearQuery.near(location).maxDistance(new Distance(100000, Metrics.KILOMETERS));  GeoResults
<ChargePoi> result = mongoTemplate.geoNear(query, ChargePoi.class);  System.
out.println(result);  }
  

  我是勤劳的搬运工:->http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.RELEASE/reference/html/mongo.core.html#mongo.geospatial
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-424488-1-1.html 上篇帖子: 分布式缓存学习之四:MongoDB 下篇帖子: Mongodb 认证鉴权那点事
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表