You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
4.0 KiB
171 lines
4.0 KiB
# Mongo - GeoSpacial
|
|
|
|
## Lesson Objectives
|
|
1. Explain GeoSpacial
|
|
1. Explain GeoJSON
|
|
1. Explain containers
|
|
1. Explain How To Create an Index
|
|
1. Explain $near
|
|
1. Explain $geoWithin
|
|
1. Explain $geoIntersects
|
|
|
|
## Explain GeoSpacial
|
|
|
|
## Explain GeoJSON
|
|
1. `{ type: "<GeoJSON type>" , coordinates: <coordinates> }`
|
|
1. Always list coordinates in `longitude`, `latitude` order.
|
|
1. point
|
|
- `{ type: "Point", coordinates: [ 40, 5 ] }`
|
|
1. line
|
|
- `{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }`
|
|
1. polygons
|
|
- array of coordinate arrays (rings)
|
|
- at least four points and have same start/end position
|
|
- one outer ring and 0+ inner rings
|
|
- cannot self intersect
|
|
- single ring
|
|
- `{ type: "Polygon", coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ] }`
|
|
- multiple rings
|
|
- 1st ring is outer ring
|
|
- Any interior ring must be entirely contained by the outer ring
|
|
- Interior rings cannot intersect or overlap each other
|
|
- Interior rings cannot share an edge.
|
|
- http://docs.mongodb.org/manual/_images/index-2dsphere-polygon-with-ring.png
|
|
```
|
|
{
|
|
type : "Polygon",
|
|
coordinates : [
|
|
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
|
|
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
|
|
]
|
|
}
|
|
```
|
|
|
|
## Explain containers
|
|
1. MultiPoint
|
|
```
|
|
{
|
|
type: "MultiPoint",
|
|
coordinates: [
|
|
[ -73.9580, 40.8003 ],
|
|
[ -73.9498, 40.7968 ],
|
|
[ -73.9737, 40.7648 ],
|
|
[ -73.9814, 40.7681 ]
|
|
]
|
|
}
|
|
```
|
|
1. MultiLineString
|
|
```
|
|
{
|
|
type: "MultiLineString",
|
|
coordinates: [
|
|
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
|
|
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
|
|
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
|
|
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
|
|
]
|
|
}
|
|
```
|
|
1. MultiPolygon
|
|
```
|
|
{
|
|
type: "MultiPolygon",
|
|
coordinates: [
|
|
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
|
|
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
|
|
]
|
|
}
|
|
```
|
|
1. GeometryCollection
|
|
```
|
|
{
|
|
type: "GeometryCollection",
|
|
geometries: [
|
|
{
|
|
type: "MultiPoint",
|
|
coordinates: [
|
|
[ -73.9580, 40.8003 ],
|
|
[ -73.9498, 40.7968 ],
|
|
[ -73.9737, 40.7648 ],
|
|
[ -73.9814, 40.7681 ]
|
|
]
|
|
},
|
|
{
|
|
type: "MultiLineString",
|
|
coordinates: [
|
|
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
|
|
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
|
|
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
|
|
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
|
|
]
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
## Explain How To Create an Index
|
|
1. `db.collection.createIndex( { <location field> : "2dsphere" } )`
|
|
|
|
## Explain $near
|
|
```
|
|
db.test_data.insert({name: 'north', position : { type: "Point", coordinates: [0,1] } });
|
|
db.test_data.insert({name: 'east', position : { type: "Point", coordinates: [1,0] } });
|
|
db.test_data.insert({name: 'south', position : { type: "Point", coordinates: [0,-1] } });
|
|
db.test_data.insert({name: 'west', position : { type: "Point", coordinates: [-1,0] } });
|
|
db.test_data.createIndex({position: "2dsphere"});
|
|
|
|
db.test_data.find({
|
|
position: {
|
|
$near : {
|
|
$geometry: {
|
|
type: "Point",
|
|
coordinates: [ 2, 0 ]
|
|
},
|
|
$minDistance: 0,
|
|
$maxDistance: 200000
|
|
}
|
|
}
|
|
});
|
|
```
|
|
|
|
## Explain $geoWithin
|
|
```
|
|
db.test_data.find({
|
|
position: {
|
|
$geoWithin: {
|
|
$geometry: {
|
|
type : "Polygon" ,
|
|
coordinates: [[
|
|
[ 0.5, 0.5 ],
|
|
[ 1.5, 0.5 ],
|
|
[ 1.5, -0.5 ],
|
|
[ 0.5, -0.5 ],
|
|
[ 0.5, 0.5 ], // VERY IMPORTANT
|
|
]]
|
|
}
|
|
}
|
|
}
|
|
});
|
|
```
|
|
## Explain $geoIntersects
|
|
```
|
|
db.test_data.insert({ name : 'line' , "position" : { "type" : "LineString", "coordinates" : [ [1,0], [2,0] ] } } );
|
|
WriteResult({ "nInserted" : 1 });
|
|
|
|
db.test_data.find({
|
|
position: {
|
|
$geoIntersects: {
|
|
$geometry: {
|
|
type : "Polygon" ,
|
|
coordinates: [[
|
|
[ 0.5, 0.5 ],
|
|
[ 1.5, 0.5 ],
|
|
[ 1.5, -0.5 ],
|
|
[ 0.5, -0.5 ],
|
|
[ 0.5, 0.5 ], // VERY IMPORTANT
|
|
]]
|
|
}
|
|
}
|
|
}
|
|
});
|
|
``` |