webinar: mongodb 2.4 feature demo and q&a on geo capabilities

31
Web Engineer, 10gen Ian Bentley @mongodb @idbentley @10gen MongoDB 2.4 Geo Features

Upload: mongodb

Post on 12-May-2015

2.746 views

Category:

Technology


0 download

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

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

2.4 Geospatial features – Ian Bentley

Planar Geometry

Page 3: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

2.4 Geospatial features – Ian Bentley

Spherical Geometry

xkcd.com/977

Page 4: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

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

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

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

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

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

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

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

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

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

Demo Example

Page 14: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

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

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

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

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

How 2dsphere works

Page 19: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

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

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

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

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

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

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

2.4 Geospatial features – Ian Bentley

Near search

Page 26: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

2.4 Geospatial features – Ian Bentley

Near search

Page 27: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

2.4 Geospatial features – Ian Bentley

Near search

Page 28: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

2.4 Geospatial features – Ian Bentley

Near search

Page 29: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

2.4 Geospatial features – Ian Bentley

Near search

Page 30: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

2.4 Geospatial features – Ian Bentley

Near search

Page 31: Webinar: MongoDB 2.4 Feature Demo and Q&A on Geo Capabilities

Q & A