MonogDB-index (iii) GIS

Source: Internet
Author: User

A 2d Index, such as the left System of the plane, has four points in it: A, B, C, D

1. insert four points using mongo

C:\dev\bin\mongodb-2.0.2\bin]]>mongoMongoDB shell version: 2.0.2connecting to: test
> db.createCollection("location"){ "ok" : 1 }
> db.location.save( {_id: "A", position: [0.001, -0.002]} )
> db.location.save( {_id: "B", position: [1.0, 1.0]} )
> db.location.save( {_id: "C", position: [0.5, 0.5]} )
> db.location.save( {_id: "D", position: [-0.5, -0.5]} )
2. Create a 2d Index> Db. location. ensureIndex ({position: "2d "})

3. Execute the query
Query the points (adjacent relationships) within 0.75 of the center)
> db.location.find( {position: 
                        { $near: [0,0], $maxDistance: 0.75  } 
                     } )
{ "_id" : "A", "position" : [ 0.001, -0.002 ] }
{ "_id" : "D", "position" : [ -0.5, -0.5 ] }
{ "_id" : "C", "position" : [ 0.5, 0.5 ] }
 
Query the vertex (Inclusion relationship) in the rectangle [0.25, 0.25], [1.0, 1.0] As the vertex)
> db.location.find( {position:
                       { $within: 
                          { $box: [ [0.25, 0.25], [1.0,1.0] ] } 
                       } 
                     } ) 
{ "_id" : "C", "position" : [ 0.5, 0.5 ] }
{ "_id" : "B", "position" : [ 1, 1 ] }

2. sphere Index
1. Prepare a GeoJSON object for the data format.
> Db. baidugis. find () {"_ id": ObjectId ("528ecd42b8ff14e242ac3129"), "id": 1, "loc": {"type": "Point", "coordinates ": [73.5, 39.31]}, "xoffset": 0.009962485610003569, "yoffset": 0.005924861040000451} {"_ id": ObjectId ("528ecd42b8ff14e242ac312a"), "id": 2, "loc": {"type": "Point", "coordinates": [73.5, 39.32]}, "xoffset": 0.009956379369995716, "yoffset ": 0.005926044680002462} {"_ id": ObjectId ("528ecd42b8ff14e242ac312b"), "id": 3, "loc": {"type": "Point", "coordinates ": [73.5, 39.33]}, "xoffset": 0.009954659179996384, "yoffset": 0.005929157739998914} {"_ id": ObjectId ("528ecd42b8ff14e242ac312c"), "id": 4, "loc": {"type": "Point", "coordinates": [73.5, 39.34]}, "xoffset": 0.009957397069996432, "yoffset": 0.00593507193999443}
2. Create 2 sphere Index> Db. baidugis. ensureIndex ({loc: "2 dsphere"}) Note: The 2dphere index can be a composite index, and the location field is not required to be the first index.

3. View Indexes> Db. baidugis. getIndexes () [{"v": 1, "key": {"_ id": 1}, "ns": "GISNew. baidugis "," name ":" _ id _ "},{" v ": 1," key ": {" loc ":" 2 dsphere "}," ns ": "GISNew. baidugis "," name ":" loc_2dsphere "}]

4. query operationsIn the correction database, query the number of points with the longitude and latitude [116.86, 40.40] as the center, and the maximum distance is 20 km, 200 km, and 2000 km> db. baidugis. find ({loc :{$ near :{$ geometry: {type: "Point", coordinates: [116.86, 40.40] },$ maxDistance: 20000 }}}). count () 1325
> Db. baidugis. find ({loc :{$ near :{$ geometry: {type: "Point", coordinates: [116.86, 40.40] },$ maxDistance: 200000 }}}). count () 133158
> Db. baidugis. find ({loc :{$ near :{$ geometry: {type: "Point", coordinates: [116.86, 40.40] },$ maxDistance: 2000000 }}}). count () 6239311


The above $ near syntax is used:

Point the queried longitude and latitude point on the map

SummaryIn actual projects, we can find that the results are still very good by using the geographical location index of MongoDB. After the query results are displayed on the map, they are basically a circle, for example, the display effect, it indicates that the computation is accurate. The query performance is acceptable. In a correction database with around 10 million points, the speed of querying points within 20 kilometers is about 1-2 seconds in a single machine, it takes about 6 to 10 seconds to query a 200 km point. It takes about 20 minutes to query a 2000 km (half a Chinese) Point.

Reference: http://docs.mongodb.org/manual/tutorial/query-a-2dsphere-index/ GIS Query Method http://geojson.org/geojson-spec.html#id2 GeoJSON object Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.