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
Web Engineer, 10gen
Ian Bentley
@mongodb @idbentley @10gen
MongoDB 2.4 Geo Features
2.4 Geospatial features – Ian Bentley
Planar Geometry
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)
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
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
Points:
{
geo: {
type: "Point",
coordinates: [100.0, 0.0]
}
}
GeoJSON allows us to encode
2.4 Geospatial features – Ian Bentley
LineStrings:
{
geo: {
type: "LineString",
coordinates: [ [100.0, 0.0], [101.0, 1.0] ]
}
}
GeoJSON allows us to encode
2.4 Geospatial features – Ian Bentley
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
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.
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.
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”
Demo Example
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.
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
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
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
How 2dsphere works
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.
Coarse Grid overlayed on the UK
2.4 Geospatial features – Ian Bentley
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.
Covering of Grid of the UK
2.4 Geospatial features – Ian Bentley
Covering of A4 surrounding Trafalgar Square
2.4 Geospatial features – Ian Bentley
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
2.4 Geospatial features – Ian Bentley
Near search
2.4 Geospatial features – Ian Bentley
Near search
2.4 Geospatial features – Ian Bentley
Near search
2.4 Geospatial features – Ian Bentley
Near search
2.4 Geospatial features – Ian Bentley
Near search
2.4 Geospatial features – Ian Bentley
Near search
Q & A