MongoDB for java and geoSpatial空间操作

A 2d sphere index supports queries that calculate geometries on an earth-like sphere. The index supports data stored as both GeoJSON objects and as legacy coordinate pairs. The index supports legacy coordinate pairs by converting the data to the GeoJSON Point type.
翻译: 2d sphere index 支持球体几何查询。数据存储以GeoJSON objects为主。

Store your location data as GeoJSON objects with this coordinate-axis order: longitude, latitude. The coordinate reference system for GeoJSON uses the WGS84 datum.

/**
       * 转换为GeoJSON对象
       */
      public  String getGeoJson(String lon,String lat) throws JSONException{
            JSONObject point = new JSONObject();
        point.put( "type", "Point");
        JSONArray coord = new JSONArray( "["+lon+ ","+lat+ "]");
        point.put( "coordinates", coord);
        return point.toString();
      }
      
      /**
       * 插入空间数据
       * @param collection
       * @throws JSONException
       */
      public void insert(DBCollection collection) throws JSONException{
                        
            DBObject point1 = new BasicDBObject();
            point1.put( "name", "001");
            point1.put( "geo", getGeoJson( "116.342176", "39.995376"));
            
            DBObject point2 = new BasicDBObject();
            point2.put( "name", "002");
            point2.put( "geo", getGeoJson( "116.348694", "39.990965"));
            
            DBObject point3 = new BasicDBObject();
            point3.put( "name", "003");
            point3.put( "geo", getGeoJson( "116.343318", "39.991184"));
            
            DBObject point4 = new BasicDBObject();
            point4.put( "name", "004");
            point4.put( "geo", getGeoJson( "116.359590", "39.982762"));
            List<DBObject> list = new ArrayList<DBObject>();
            list.add(point1);
            list.add(point2);
            list.add(point3);
            list.add(point4);
            collection.insert(list).getN();
      }
      
      /**
       * 创建空间索引
       * @param collection
       */
      public void makeSpatialIndexs(DBCollection collection){
            collection.ensureIndex( new BasicDBObject( "geo", "2dsphere"), "geospatialIdx");
      }
      
      /**
       * 查询矩形内的所有要素
       * @param collection
       */
      public void query(DBCollection collection){
            List<Double[]> box = new ArrayList<Double[]>();
            box.add( new Double[] {116.341795,39.992277}); //左上角 coordinate
            box.add( new Double[]{116.350122,39.989251}); // 右下角 coordinate
            BasicDBObject query = new BasicDBObject( "loc", new BasicDBObject("$within",new BasicDBObject("$box" , box)));
            DBCursor cur1 = collection.find(query);
             while (cur1.hasNext()) {
                  BasicDBObject o = (BasicDBObject) cur1.next();
                  System. out.println(o.get( "name"));
                  System. out.println(o.get( "geometry"));
            }
      }


更多的空间操作参考:
http://java.dzone.com/articles/writing-geospatial-queries
参考资料:
geojson: http://www.geojson.org/geojson-spec.htmlGeospatial
Indexes and Queries:http://docs.mongodb.org/manual/applications/geospatial-indexes/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值