MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
Geospatial Enhancements in MongoDB 2.4
1. Map the Globe, simply
Polygon indexing and GeoJSON support
in MongoDB
Greg Studer
greg@10gen.com
mongoNYC 2013
2. ● 2d vs 2dsphere
● (Quick) overview of previous geospatial features
in MongoDB
● New line/polygon indexing on spherical globe
GeoJSON - lines and polygons
● Geo in aggregation
● Future Roadmap
3. Why use (2dsphere) geospatial indexing?
1. Have earth-like coordinates and regions tagged
to other data over large areas
2. Want to query this data
based on location
3. Don't necessarily want to
think a lot about the fact
that the earth is round
4. [ from Jpatokal at wikimedia.org ]
https://secure.wikimedia.org/wikipedia/commons/wiki/File:World-airline-routemap-2009.png
5. [ from Jpatokal at wikimedia.org ]
https://secure.wikimedia.org/wikipedia/commons/wiki/File:World-airline-routemap-2009.png
7. Why use (2d) geospatial indexing?
1. Have regional or virtual world coordinates
embedded in other data
2. (For now) Only indexing
locations
3. Don't want spherical
overhead
8. Overview of geo features
● 2.2 - "2d"
Index only (multi-)points
Query near points or within simple polygons
9. // Make a flat 2d index [-180 -> 180)
> db.coll.ensureIndex(
{ location : "2d", type : 1 } )
// Insert some point data
> db.coll.insert({ name : "Shake Shack",
type : "rest",
location : [ -73.9881353, 40.7415064 ]
})
> db.coll.insert({ name : "Park Avenue
Tavern", type : "bar",
location : [ -73.9786924, 40.7502231 ]
})
11. Overview of geo features
● 2.2 - "2d"
DEMO - NYC OpenStreetMaps data
github.com/mongodb/mongo-snippets/tree/master/2dsphere
12. Overview of geo features
● Current 2.4 - "2dsphere"
Index points, lines, and polygons (on sphere)
Understands simple GeoJSON
Query with points, lines, and polygons
Within-region, Intersect-region, Near-point
queries
13. GeoJSON
● Widely supported open JSON standard
http://www.geojson.org/
● Simple language:
{ type : 'Point',
coordinates : [<lon>, <lat>] }
{ type : 'LineString',
coordinates : [[<lon>, <lat>],...] }
{ type : 'Polygon',
coordinates : [[[<lon>,<lat>],...],...]}
+ other MultiXXX types (future) ...
14. // Make a spherical 2d compound index
> db.coll.ensureIndex(
{ type : 1, location : "2dsphere" } )
// Insert Park Avenue
> db.coll.insert(
{ name : "Park Avenue", type : "road",
location : { type : 'LineString',
coordinates : [
[ -73.9359712600708, 40.80942429324451],
[ -73.93676519393921, 40.80820620367037],
... ]} });
15. // Insert Great Hill
> db.coll.insert(
{ name : "Great Hill", type : "park",
location : { type : 'Polygon',
coordinates : [[
[ -73.95841598510742, 40.79724239132546],
[ -73.95817995071411, 40.79691751000055],
... ]]} });
16. // Find docs with geometry that intersects
// a route
> db.coll.find({ location :
{ $geoIntersects : { $geometry :
{ type : 'LineString',
// Run coordinates
coordinates : [
[ -73.95586252212524, 40.77964777966238 ],
[ -73.95886659622192, 40.78091513739611 ],
... ]} }}})
17. Overview of geo features
● Current 2.4 - "2dsphere"
DEMO - NYC OpenStreetMaps data
github.com/mongodb/mongo-snippets/tree/master/2dsphere
18. Overview of geo features
● Current 2.4 - "2dsphere"
Reduced shape distortion
DEMO - NYC OpenStreetMaps data
github.com/mongodb/mongo-snippets/tree/master/2dsphere
19. Technology
● GeoJSON
http://www.geojson.org
● Google S2 Geometry Library
Apache License 2.0
http://code.google.com/p/s2-geometry-library
image by user geek3 @ http://en.
wikipedia.org/wiki/File:
Sphere_wireframe_10deg_6r.svg
22. ● 2d vs 2dsphere
● (Quick) overview of previous geospatial features
in MongoDB
● New line/polygon indexing on spherical globe
GeoJSON - lines and polygons
● Geo in aggregation
● Future Roadmap
23. Overview of geo features
● Current 2.4 - aggregation support
Geospatial predicates in aggregation
Near sorting in aggregation, custom output
fields
30. [ from Jpatokal at wikimedia.org ]
https://secure.wikimedia.org/wikipedia/commons/wiki/File:World-airline-routemap-2009.png
31. Future
● Further support for other predicates
OGC Simple Features
● GeoJSON composite shapes
● Indexing API to allow similar enhancements for
"flat plane" work
● Space open for non-default CRSes