Leaflet, Node.js, and MongoDB for an Easy and Fun Web Mapping Experience


http://talks.thesteve0.com/nodemap

Presented by:

Steven Pousty

@TheSteve0 on Twitter, Ingress, SmugMug, IRC, and Github

Agenda

  1. A little about MongoDB and its spatial features
  2. A little about Node.js
  3. A little about Leaflet.js
  4. Learn a bit about PaaS
  5. Make a cool mapping app

Assumptions

  1. You have worked with data
  2. You write some JS code
  3. You will ask questions

What's the scenario?

  1. We work for a Vendor that sells souvenirs at National Parks in the United States and Canada
  2. We are making a check-in service to help drive traffic to our stands
  3. We want FOSS on the whole stack - NO LOCK IN
  4. Due next week

By the end of this talk, you will be able to build this!

MAPPY!

http://bit.ly/1h5DJal

or

http://fur.ly/0/nodemap

On to Mongo

 

A bit about MongoDB

  • A document is just a bunch of attributes and values - think of it like a HashMap
  • Can be nested - helps avoid joins
  • Schemaless
  • Really good at fast writes - but you give up immediate consistency
  • Easy to horizontally scale

Now some spatial

MongoDB spatial functionality:

  1. Near ($near)
  2. Containment ($geoWithin)
  3. Intersection ($geoIntersects)


Two types of indices:

  1. 2d - for flat surfaces
  2. 2dsphere - for earth-like spheres

Good to Know

Making it work

1a. Put coordinates into an array


    { loc : [ 50 , 30 ] }
        
    If you use latitude and longitude with a default index, 
    make sure to use correct ordering: [lon, lat]
            

1b. Put coordinates in a GeoJSON format


    { loc : 
        { 
            type : "Point" ,
            coordinates : [ 40, 5 ]
        } 
    }
            

Making it work (cont.)

2a. Make a 2d index


              db.places.ensureIndex( { loc : "2d" } )
              

2b. Make a 2dsphere index


              db.places.ensureIndex( { loc : "2dsphere" } )
              
              Can be a compound index
              

On to Node.js

 

Why all the fuss?

  1. It's all in JavaScript
  2. You get great async behavior out of the box
  3. Fast and Lightweight for certain use cases - where you are not DB bound

Steve's Unofficial "When to use Node.js" Guide

  1. You are a team of JS programmers
  2. You want to serve up a lot of things really fast
  3. You don't need all the libraries that some other languages bring

Just some perspective on the hype

Time for Leaflet

 

Quick intro

  1. Lightweight JS client side library for doing great maps
  2. Works out of the box with OpenStreetMap (OSM) and others
  3. Mobile ready
  4. Has a rich library of Plugins

Enough Talk - CODE!

 

Bring it up fast!


 

How ya feeling?

 

Let's wrap it up

  1. Spatial is easy and fun with MongoDB, Node, & Leaflet
  2. Openshift makes life great for you
  3. You can do all this for free in a matter of minutes
  4. The price ($0) makes it so you can do this for any project you want
  5. @ghaff - So simple even a marketing person can do it :-) http://wwos-bitmason.rhcloud.com

Come hang out with us:
#openshift on freenode irc
OR
users@lists.openshift.redhat.com