webinar: mongodb 2.4 feature demo and q&a on geo capabilities
DESCRIPTION
In version 2.4, there have been significant enhancements to the geospatial indexing capabilities in MongoDB, such as polygon intersections, a more accurate spherical model, and better integration with MongoDB's aggregation framework. In this presentation, you'll learn about the new enhancements and how they are enabling developers to more quickly and easily develop spatially-aware applications.TRANSCRIPT
![Page 1: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/1.jpg)
Web Engineer, 10gen
Ian Bentley
@mongodb @idbentley @10gen
MongoDB 2.4 Geo Features
![Page 2: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/2.jpg)
2.4 Geospatial features – Ian Bentley
Planar Geometry
![Page 4: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/4.jpg)
2.4 Geospatial features – Ian Bentley
MongoDB has had geo for a while
• `2d` index– Store points on 2d plane– Search for points within a:• Rectangle ($box)• Polygon ($polygon)• Circle ($center)• Circle on a sphere ($centerSphere)
– Search for nearest points ($near, $nearSphere)
![Page 5: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/5.jpg)
2.4 Geospatial features – Ian Bentley
Some desirable things!
• Storing non-point geometries
• Within searches on a sphere
• Searching for intersecting geometries on a sphere
• Better support for compound indexes
![Page 6: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/6.jpg)
2.4 Geospatial features – Ian Bentley
Storing non-point geometries
• GeoJSON – A collaborative community project that produced a specification for encoding geometric entities in JSON
• Gaining wide support– OpenLayers– PostGIS– Libraries in several languages
![Page 7: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/7.jpg)
Points:
{
geo: {
type: "Point",
coordinates: [100.0, 0.0]
}
}
GeoJSON allows us to encode
2.4 Geospatial features – Ian Bentley
![Page 8: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/8.jpg)
LineStrings:
{
geo: {
type: "LineString",
coordinates: [ [100.0, 0.0], [101.0, 1.0] ]
}
}
GeoJSON allows us to encode
2.4 Geospatial features – Ian Bentley
![Page 9: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/9.jpg)
Polygons:
{ geo: {
type: "Polygon",
coordinates: [
[ [100.0, 0.0], [101.0, 0.0],
[101.0, 1.0], [100.0, 1.0],
[100.0, 0.0] ]
]
} }
GeoJSON allows us to encode
2.4 Geospatial features – Ian Bentley
![Page 10: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/10.jpg)
2.4 Geospatial features – Ian Bentley
Within searches on a sphere
• $geoWithin operator
• Takes a GeoJSON polygon geometry as a specifier
• Returns any geometries of any type that are fully contained within the polygon
• Works without any index.
![Page 11: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/11.jpg)
2.4 Geospatial features – Ian Bentley
Intersecting geometries on a sphere
• $geoIntersects operator
• Takes any GeoJSON geometry as a specifier
• Returns any geometries that have a non-empty intersection
• Lots of edge cases – intersecting edges, points on lines.
• Works without any index.
![Page 12: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/12.jpg)
2.4 Geospatial features – Ian Bentley
Better support for compound indexes
• Unlike 2d indexes, 2dsphere indexes aren’t required to be the first field of a compound index– Filtering potential documents before doing geo
query can drastically improve the performance of some queries• “Find me Hot Dog Stands within New York
state”• “Find me geometries in New York state that
are Hot Dog Stands”
• Multiple geo fields can be in the same index– “Find routes with start location 50miles from JFK
and end location 100miles from YYC”
![Page 13: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/13.jpg)
Demo Example
![Page 14: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/14.jpg)
2.4 Geospatial features – Ian Bentley
• You can find all the code, and data powering the demo on github, and read about it on my blog
• Let’s take a close look at the python that does the actual work.
![Page 15: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/15.jpg)
def find_within(points):
# When defining a polygon, the first point should
# also appear as the last point.
points.append(points[0])
poly = {
"type": "Polygon",
"coordinates": [points]
}
places = collection.find(
{"geo": { "$within": { "$geometry": poly } } } )
places.limit(500)
return places
It’s this simple - within
2.4 Geospatial features – Ian Bentley
![Page 16: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/16.jpg)
def find_intersects(points):
line = {
"type": "LineString",
"coordinates": points
}
places = collection.find(
{"geo":{ "$geoIntersects":
{ "$geometry": line } } } )
places.limit(50)
return places
It’s this simple - intersects
2.4 Geospatial features – Ian Bentley
![Page 17: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/17.jpg)
def find_nearest(point):
point = {
"type": "Point",
"coordinates": point
}
places = collection.find(
{"geo": { "$near": { "$geometry": point } } })
places.limit(10)
return places
It’s this simple - near
2.4 Geospatial features – Ian Bentley
![Page 18: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/18.jpg)
How 2dsphere works
![Page 19: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/19.jpg)
2.4 Geospatial features – Ian Bentley
How do you index a spherical coordinate?
• Divide the geometry that you are indexing into a grid.
• For each cell in the grid, calculate a key, based upon its position on the sphere.
• Insert each cell into a standard B-tree
• MongoDB uses google’s S2 C++ library for the heavy lifting.
![Page 20: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/20.jpg)
Coarse Grid overlayed on the UK
2.4 Geospatial features – Ian Bentley
![Page 21: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/21.jpg)
2.4 Geospatial features – Ian Bentley
Coverings
• A covering of a geometry is a minimal set of cells that completely cover’s a geometry
• S2 can efficiently generate coverings for arbitrary geometries.
![Page 22: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/22.jpg)
Covering of Grid of the UK
2.4 Geospatial features – Ian Bentley
![Page 23: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/23.jpg)
Covering of A4 surrounding Trafalgar Square
2.4 Geospatial features – Ian Bentley
![Page 24: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/24.jpg)
2.4 Geospatial features – Ian Bentley
Cells
• S2 defines cell sizes from level 1 to level 31
• The higher the level, the smaller the cell
• Different levels are optimized for different queries– If you have densely packed geometries, and you
are doing a $near search, a higher level will be efficient
– If you are doing a $within search with a large polygon, a lower level will be more efficient
• By default we use all levels between 500m and 100km on a side
![Page 25: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/25.jpg)
2.4 Geospatial features – Ian Bentley
Near search
![Page 26: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/26.jpg)
2.4 Geospatial features – Ian Bentley
Near search
![Page 27: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/27.jpg)
2.4 Geospatial features – Ian Bentley
Near search
![Page 28: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/28.jpg)
2.4 Geospatial features – Ian Bentley
Near search
![Page 29: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/29.jpg)
2.4 Geospatial features – Ian Bentley
Near search
![Page 30: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/30.jpg)
2.4 Geospatial features – Ian Bentley
Near search
![Page 31: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities](https://reader033.vdocuments.us/reader033/viewer/2022060108/55515a44b4c905a8768b4bb1/html5/thumbnails/31.jpg)
Q & A