I’ve been toying around in my spare time with HTML5 and building a geographically enabled web app (possibly making it into a full blown mobile app down the track using PhoneGap or Appcelerator). Anyway, I started off with the back end.
I chose MongoDB as the data store (a nosql database with really simple out of the box geographic indexing). There are a few threads about mongo’s geohashing algorithms not coping at very fine scales, but for my purposes it has all the accuracy I need and the geohashing mechanism is really fast.
I needed a REST interface that my mobile app could use to retrieve nearest locations and to add new locations – both fairly simple requirements as the bulk of the computational work is elegantly handled by the backend database. All I needed was something to build the routes and add in my own validation – this is where Slim comes in.
Slim is a micro framework to build REST services and it does this one thing very well. It allows me to build routes for GET POST PUT and DELETE requests and hand them off to appropriate functions in my data model. minimal example:
<?php require 'Slim/Slim.php'; require 'models/LocationStore.php'; $app = new Slim(); $ls = new LocationStore(); $app->get('/near/:lat,:lon', function ($lat, $lon) { header("Content-Type: application/json"); echo json_encode($ls->getNear($lat, $lon)); }); $app->run(); ?>
So a GET request to http://myserver/near/-37.8,143.2
would be routed to a function that queries my database for locations near the latitude and longitude passed in. I can also use some neat features built in to Slim to validate the passed in values against a regular expression.
There is more to it of course and a number of templating tools can be plugged in to make it into a more fully featured web framework.
Next up is the front end HTML5 code, but that’s a subject for another post.
Slim framework website: http://www.slimframework.com/
Leave a Reply