Top Banner
Map the Globe, simply Polygon indexing and GeoJSON support in MongoDB Greg Studer [email protected] mongoNYC 2013
32
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Geospatial Enhancements in MongoDB 2.4

Map the Globe, simply

Polygon indexing and GeoJSON support in MongoDB

Greg [email protected]

mongoNYC 2013

Page 2: Geospatial Enhancements in MongoDB 2.4

● 2d vs 2dsphere

● (Quick) overview of previous geospatial features in MongoDB

● New line/polygon indexing on spherical globeGeoJSON - lines and polygons

● Geo in aggregation

● Future Roadmap

Page 3: Geospatial Enhancements in MongoDB 2.4

Why use (2dsphere) geospatial indexing?

1. Have earth-like coordinates and regions tagged to other data over large areas

2. Want to query this databased on location

3. Don't necessarily want tothink a lot about the fact that the earth is round

Page 4: Geospatial Enhancements in MongoDB 2.4

[ from Jpatokal at wikimedia.org ]https://secure.wikimedia.org/wikipedia/commons/wiki/File:World-airline-routemap-2009.png

Page 5: Geospatial Enhancements in MongoDB 2.4

[ from Jpatokal at wikimedia.org ]https://secure.wikimedia.org/wikipedia/commons/wiki/File:World-airline-routemap-2009.png

Page 7: Geospatial Enhancements in MongoDB 2.4

Why use (2d) geospatial indexing?

1. Have regional or virtual world coordinates embedded in other data

2. (For now) Only indexinglocations

3. Don't want sphericaloverhead

Page 8: Geospatial Enhancements in MongoDB 2.4

Overview of geo features

● 2.2 - "2d"Index only (multi-)pointsQuery near points or within simple polygons

Page 9: Geospatial Enhancements in MongoDB 2.4

// 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 ] })

Page 10: Geospatial Enhancements in MongoDB 2.4

// Find in Manhattan Mall> db.coll.find({ location : { $geoWithin : { $polygon : [[ -73.9884761, 40.7491204 ], [ -73.988829, 40.7486387 ], [ -73.9901297, 40.7491856 ], [ -73.9897768, 40.7496673 ], [ -73.9884761, 40.7491204 ]] }}}, type : "rest" })

// Find near Washington Square (center)> db.coll.find({ location : { $geoNear : [ -73.7695467, 42.6622728 ] }, type : "rest" });

Page 11: Geospatial Enhancements in MongoDB 2.4

Overview of geo features

● 2.2 - "2d"

DEMO - NYC OpenStreetMaps datagithub.com/mongodb/mongo-snippets/tree/master/2dsphere

Page 12: Geospatial Enhancements in MongoDB 2.4

Overview of geo features

● Current 2.4 - "2dsphere"Index points, lines, and polygons (on sphere)Understands simple GeoJSONQuery with points, lines, and polygonsWithin-region, Intersect-region, Near-point queries

Page 13: Geospatial Enhancements in MongoDB 2.4

GeoJSON

● Widely supported open JSON standardhttp://www.geojson.org/

● Simple language:

{ type : 'Point', coordinates : [<lon>, <lat>] }{ type : 'LineString', coordinates : [[<lon>, <lat>],...] }{ type : 'Polygon', coordinates : [[[<lon>,<lat>],...],...]}

+ other MultiXXX types (future) ...

Page 14: Geospatial Enhancements in MongoDB 2.4

// 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], ... ]} });

Page 15: Geospatial Enhancements in MongoDB 2.4

// Insert Great Hill> db.coll.insert({ name : "Great Hill", type : "park", location : { type : 'Polygon', coordinates : [[ [ -73.95841598510742, 40.79724239132546], [ -73.95817995071411, 40.79691751000055], ... ]]} });

Page 16: Geospatial Enhancements in MongoDB 2.4

// 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 ], ... ]} }}})

Page 17: Geospatial Enhancements in MongoDB 2.4

Overview of geo features

● Current 2.4 - "2dsphere"

DEMO - NYC OpenStreetMaps datagithub.com/mongodb/mongo-snippets/tree/master/2dsphere

Page 18: Geospatial Enhancements in MongoDB 2.4

Overview of geo features

● Current 2.4 - "2dsphere"Reduced shape distortion

DEMO - NYC OpenStreetMaps datagithub.com/mongodb/mongo-snippets/tree/master/2dsphere

Page 19: Geospatial Enhancements in MongoDB 2.4

Technology

● GeoJSONhttp://www.geojson.org

● Google S2 Geometry LibraryApache License 2.0http://code.google.com/p/s2-geometry-library

image by user geek3 @ http://en.wikipedia.org/wiki/File:Sphere_wireframe_10deg_6r.svg

Page 20: Geospatial Enhancements in MongoDB 2.4

S2

Page 21: Geospatial Enhancements in MongoDB 2.4

S2

00

0110

11

10|01

10|0010|11

10|10

Page 22: Geospatial Enhancements in MongoDB 2.4

● 2d vs 2dsphere

● (Quick) overview of previous geospatial features in MongoDB

● New line/polygon indexing on spherical globeGeoJSON - lines and polygons

● Geo in aggregation

● Future Roadmap

Page 23: Geospatial Enhancements in MongoDB 2.4

Overview of geo features

● Current 2.4 - aggregation supportGeospatial predicates in aggregationNear sorting in aggregation, custom output fields

Page 24: Geospatial Enhancements in MongoDB 2.4

> db.ny.aggregate([ // Find everything in MidTown/Hell's Kitchen { $match : { geo: { $geoWithin: { $geometry:

{ type: "Polygon", coordinates: [[ // Midtown/Hell's Kitchen [ -73.9984130859375, 40.78028146155187 ], [ -73.95240783691406, 40.76182096906601 ], [ -73.96888732910156, 40.73945350425846 ], [ -74.01420593261719, 40.75531957477602 ], [ -73.9984130859375, 40.78028146155187 ] ]] } } } } },

...

Page 25: Geospatial Enhancements in MongoDB 2.4

... // Organize stuff we want { $project : { tags : 1, info : { name : "$name", lonLat : "$geo.coordinates" }} }, { $unwind : "$tags" }, // Count by tag types { $group : { _id : "$tags", total : { $sum : 1 }, locs : { $push : "$info" } }}]); // end

Page 26: Geospatial Enhancements in MongoDB 2.4

{ "result" : [ { "_id" : "park", "total" : 18, "locs" : [{ "name" : "Hells Kitchen Park", "lonLat" : [ -73.9925, 40.7630556 ] }, ... { "_id" : "bar", "total" : 28, "locs" : [ { "name" : "Landmark Tavern", "lonLat" : [ -73.9963261, 40.7631922 ] }, { "name" : "Pony Bar", "lonLat" : [ -73.994236, 40.761723 ] },

...

...

Page 27: Geospatial Enhancements in MongoDB 2.4

> db.ny.aggregate([ // Find near Times Square { $geoNear : { near : { type: "Point", // Times Square coordinates: [ -73.98508787155151, 40.75905795418586 ] }, distanceField : "distance", spherical : true, // 2dsphere num : 1000 } },...

Page 28: Geospatial Enhancements in MongoDB 2.4

... // Organize stuff we want { $project : { tags : 1, info : { name : "$name", distance : "$distance", lonLat : "$geo.coordinates" }} }, { $unwind : "$tags" },

// Count by tag types { $group : { _id : "$tags", total : { $sum : 1 }, locs : { $push : "$info" } }}]); // end

...

Page 29: Geospatial Enhancements in MongoDB 2.4

{ "result" : [ { "_id" : "park", "total" : 11, "locs" : [{ "name" : "Ramone Aponte Park", "distance" : 449.7417003854033, // m "lonLat" : [ -73.9894444, 40.7613889 ]}, ... { "_id" : "bar", "total" : 24, "locs" : [{ "name" : "The Lambs Club", "distance" : 275.0270424641914, // m "lonLat" : [ -73.9963261, 40.7631922 ]}, ...

...

Page 30: Geospatial Enhancements in MongoDB 2.4

[ from Jpatokal at wikimedia.org ]https://secure.wikimedia.org/wikipedia/commons/wiki/File:World-airline-routemap-2009.png

Page 31: Geospatial Enhancements in MongoDB 2.4

Future

● Further support for other predicatesOGC Simple Features

● GeoJSON composite shapes

● Indexing API to allow similar enhancements for "flat plane" work

● Space open for non-default CRSes

Page 32: Geospatial Enhancements in MongoDB 2.4

Thanks!

[email protected]