Top Banner
Geoindexing with MongoDB Leszek Krupiński WebClusters 2012
88

Geoindexing with MongoDB

Jan 15, 2015

Download

Technology

leafnode

Presentation from WebClusters 2012 conference
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: Geoindexing with MongoDB

Geoindexingwith

MongoDBLeszek Krupiński

WebClusters 2012

Page 2: Geoindexing with MongoDB

About me

Page 3: Geoindexing with MongoDB

On-line since 1997

Page 4: Geoindexing with MongoDB

Funny times

Page 5: Geoindexing with MongoDB

1 hr of internet for 1 USD

Page 6: Geoindexing with MongoDB
Page 7: Geoindexing with MongoDB
Page 8: Geoindexing with MongoDB

First social site:geocities

Page 9: Geoindexing with MongoDB

My first web page

Page 10: Geoindexing with MongoDB

What do I do now

Page 11: Geoindexing with MongoDB

Day-time jobManaging team of developers for Polish Air Force

Page 12: Geoindexing with MongoDB

Side:consulting, optimizing,

desiging

Page 13: Geoindexing with MongoDB

Buzzwords incoming!

Page 14: Geoindexing with MongoDB

The Internet2008

Page 15: Geoindexing with MongoDB

Web 2.0

Page 16: Geoindexing with MongoDB

http://en.wikipedia.org/wiki/File:Web_2.0_Map.svg CC-BY-SA-2.5

Page 17: Geoindexing with MongoDB

Be social in your bedroom

Page 18: Geoindexing with MongoDB

alone.

Page 19: Geoindexing with MongoDB

The Internet2012

Page 20: Geoindexing with MongoDB

Web 3.0

Page 21: Geoindexing with MongoDB
Page 22: Geoindexing with MongoDB

Why geospatial?

Page 23: Geoindexing with MongoDB

Needs shifted

Page 24: Geoindexing with MongoDB

Why?Because they could.

Page 25: Geoindexing with MongoDB
Page 26: Geoindexing with MongoDB
Page 27: Geoindexing with MongoDB
Page 28: Geoindexing with MongoDB

How to implement?

Page 29: Geoindexing with MongoDB

Database. Duh.

Page 30: Geoindexing with MongoDB

Keep, but also query

Page 31: Geoindexing with MongoDB

Is there a person at

53.438522,14.52198?

Nope.

Is there a person at

53.438522,14.52199?

Nope.

Is there a person at

53.438522,14.52199?

Yeah, here’s Johnny!

Page 32: Geoindexing with MongoDB

Not too useful.

Page 33: Geoindexing with MongoDB

Give me nearby homies.

Within the range of 1 km there is:

• Al Gore (53.438625,14.52103)• Bill Clinton (53.432531,14.55127)• Johnny Bravo

(53.438286,14.52363)

Page 34: Geoindexing with MongoDB

Now that’s better.

Page 35: Geoindexing with MongoDB

Geoindexing.Nothing new.

Page 36: Geoindexing with MongoDB

Oracle, PostreSQL, Lucene/Solr, even

MySQL (via extensions)

Page 37: Geoindexing with MongoDB

SELECT c.holding_company, c.location FROM competitor c, bank bWHERE b.site_id = 1604 AND SDO_WITHIN_DISTANCE(c.location, b.location, ’distance=2 unit=mile’) = ’TRUE’

ORACLE

Page 38: Geoindexing with MongoDB

SQL is so last year

Page 39: Geoindexing with MongoDB

Let’s use something cool

Page 40: Geoindexing with MongoDB

MongoDB.Because all the cool kids use NoSQL now

Page 41: Geoindexing with MongoDB
Page 42: Geoindexing with MongoDB

Why MongoDB?

Page 43: Geoindexing with MongoDB

Choose your NoSQL wise.

Page 44: Geoindexing with MongoDB

NoSQL in MongoDB

• Document –based• Queries (JS-like syntax)• JSON-like storage

Page 45: Geoindexing with MongoDB

Why MongoDB?Use Cases• Archiving• Event logging• Document and CMS• Gaming• High volume sites• Mobile• Operational datastore• Agile development• Real-time stats

Features• Ad hoc queries• Indexing• Replication• Load Balancing• File Storage• Aggregation• Server-side JavaScript• Capped collections

http://en.wikipedia.org/wiki/Mongodb

Page 46: Geoindexing with MongoDB

Back to geo.

Page 47: Geoindexing with MongoDB

{loc: [ 52.0, 21.0 ],name: ”Warsaw”,type: ”City”

}

Page 48: Geoindexing with MongoDB

db.nodes.ensureIndex({loc: '2d'})

Page 49: Geoindexing with MongoDB

That’s it.

Page 50: Geoindexing with MongoDB

Query• Exact

o db.places.find( { loc : [50,50] } )

• Nearo db.places.find( { loc : { $near : [50,50] } } )

• Limito db.places.find( { loc : { $near : [50,50] } } ).limit(20)

• Distanceo db.places.find(

{ loc : { $near : [50,50] , $maxDistance : 5 } }).limit(20)

Page 51: Geoindexing with MongoDB

Compound index• db.places.ensureIndex(

{ location : "2d" , category : 1 });

• db.places.find({

location : { $near : [50,50] },category : 'coffee‚

});

Page 52: Geoindexing with MongoDB

Bound queries• box = [

[40.73083, -73.99756],[40.741404, -73.988135]

]• db.places.find(

{"loc" : {"$within" : {"$box" : box }}

})

Page 53: Geoindexing with MongoDB

Problems

Page 54: Geoindexing with MongoDB

Units

Page 55: Geoindexing with MongoDB

Coordinates in arc units Distance in

kilometers

Page 56: Geoindexing with MongoDB

In query

Page 57: Geoindexing with MongoDB

earthRadius = 6378 // km

multi = earthRadius * PI / 180.0

range = 3000 // km

… maxDistance : range * multi…

Page 58: Geoindexing with MongoDB

In results

Page 59: Geoindexing with MongoDB

pointDistance =

distances[0].dis / multi

Page 60: Geoindexing with MongoDB

Earth is not flat.

Page 61: Geoindexing with MongoDB

Problem: can’t use linear distance

Page 62: Geoindexing with MongoDB

Earth isn’t flat too.

Page 63: Geoindexing with MongoDB

Solution?Use approximation.

Page 64: Geoindexing with MongoDB

MongoDB has it built-in

distances = db.runCommand({ geoNear : "points", near : [0, 0], spherical : true,

maxDistance : range / earthRadius /* to radians */} ).results

Page 65: Geoindexing with MongoDB

Focus: runCommanddistances = db.runCommand({ geoNear : "points" …

Page 66: Geoindexing with MongoDB

Sort by distanceOnly with runCommand

Page 67: Geoindexing with MongoDB

Automatically sorted• db.runCommand(

{ geoNear : "places" , near : [50,50], num : 10 });

• {"ns" : "test.places","results" : [

{ "dis" : 69.29646421910687, "obj" : … },{ "dis" : 69.29646421910687, "obj" : … },…

],…

}

Page 68: Geoindexing with MongoDB

Demo

Page 69: Geoindexing with MongoDB

OpenStreetMaps database of Poland

imported into MongoDB

Page 70: Geoindexing with MongoDB

14.411.552 nodes

Page 71: Geoindexing with MongoDB

3GB of raw XML data

Page 72: Geoindexing with MongoDB

PHP in virtual machine

Page 73: Geoindexing with MongoDB

Imported about 100.000 nodes every

10s.

Page 74: Geoindexing with MongoDB

Pretty cool, eh?

Page 75: Geoindexing with MongoDB

Kudos to Derick Rethans

Part of this talk was inspired by his talk

Page 76: Geoindexing with MongoDB

Questions?

Page 77: Geoindexing with MongoDB

Thanks!Rate me at https://joind.in/talk/view/6475

Page 78: Geoindexing with MongoDB

Geoindexingwith

MongoDBsupplement

Leszek KrupińskiWebClusters 2012

Page 79: Geoindexing with MongoDB

Why MongoDB?

Page 80: Geoindexing with MongoDB

Evaluate.

Page 81: Geoindexing with MongoDB

PostGIS is cool too.(but it’s SQL, meh)

Page 82: Geoindexing with MongoDB

Why MongoDB?Use Cases• Archiving• Event logging• Document and CMS• Gaming• High volume sites• Mobile• Operational datastore• Agile development• Real-time stats

Features• Ad hoc queries• Indexing• Replication• Load Balancing• File Storage• Aggregation• Server-side JavaScript• Capped collections

http://en.wikipedia.org/wiki/Mongodb

Page 83: Geoindexing with MongoDB

If you need other features of MongoDB,

use it

Page 84: Geoindexing with MongoDB

If you don’t, evaluate.

Page 85: Geoindexing with MongoDB

Evaluate.

Page 86: Geoindexing with MongoDB

Demo(hopefully)

Page 87: Geoindexing with MongoDB

Questions?

Page 88: Geoindexing with MongoDB

Please leave feedback!Rate me at https://joind.in/6475