MongoDB for java and geoSpatial空间操作
2013-11-29 18:21
411 查看
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.
更多的空间操作参考:
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/
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/
相关文章推荐
- Java实现对MongoDB的AND、OR和IN操作 ,大于、小于等判断
- java对mongodb的and, in, or 经常使用操作
- Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- MongoDB学习系列12:Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- MongoDB for Java】Java操作MongoDB
- 【MongoDB】-Java实现对mongodb的And、Or、In操作
- Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- Java 实现对mongodb and or 和and or联合查询操作
- java对mongodb的and, in, or 常用操作
- Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- mongodb dirver for java【写入操作】
- Java实现对MongoDB的AND、OR和IN操作
- Java实现对MongoDB的AND、OR和IN操作
- $within进行空间查询--MongoDB API for Java