I recently changed my geolocation queries from Couchbase GeoCouch to Elasticsearch since Elasticsearch offers more flexibility with the search parameters. However, the switch was not as easy as I thought so with some help from Clinton I wrote together some information on how to replicate from Couchbase to Elasticsearch and write a first geo query.

There is a good tutorial on how to install the Couchbase-Elasticsearch-Plugin and configure the XDCR (Cross Datacenter Replication), which is required to get started.

The first thing that didn’t really work for me in Elasticsearch was the PUT update of an index with the geolocation properties, so it is required to DELETE the current index and create (POST) a new one with the JSON below:

<pre>{ "mappings" : { "couchbaseDocument" : { "properties" : { "doc" : { "type" : "object", "properties" : { "location" : { "lat_lon" : 1, "type" : "geo_point" } } } } } } }</pre>

Response: <pre>{ "ok": true, "acknowledged": true }</pre>

After that re-create the replication from Couchbase to make sure the index is up to date.

This maps the “location” attribute to a lat_lon index in Elasticsearch. Your Couchbase document should look something like this:

<pre>{ "type": "location", "location": { "lat": 41.387008, "lon": 2.170036 } }</pre>

To query for objects around Pl. Catalunya in Barcelona, use the following query:

<pre>{ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "geo_distance": { "distance": "5km", "doc.location": { "lat": 41.387008, "lon": 2.170036 } } } } } }</pre>

Hope this helps.

For my last app I ran into the problem of scrolling an area within an HTML5 App while other areas, for example the header remains at a static position. There are a couple options:

I started with iScroll which seems to be the most popular option but ran into major troubles and headaches with it. Digging into the topic, I found Scrollability, which is unfortunately not maintained any more (last commit 2 years ago?). And including a major framework into an existing App just for scrolling was kind of using a sledge-hammer to crack a nut. Since the App is supposed to run on iOS first, I figured I’ll try the native way: -webkit-overflow-scrolling: touch.

However, this is also kind of tricky. Here’s what you need:

Change UIWebViewBounce in your config.xml to false:

Unfortunately, the height of the wrapper is 50px too short, the height of the header. The only solution I found was resizing the height with some JavaScript.

This can be done after rendering, for example with mustache.js.

For the Firefox Open App Days I wrote a chat demo to introduce various technologies for mobile Apps including a native iPhone application created with Apache Cordova, an HTML5 App for the brand new Firefox OS and a HTML5 web site for mobile Browsers. To broadcast chat messages between users, socket.io is used. The source code can be downloaded and forked from GitHub: OpenAppChat.

To run the demo, you just need node.js installed on your computer. In your Terminal, cd to the OpenAppChat directory and run:

node app.js

Navigate with your web browser of choice to http://localhost:3000 to see the chat. Inside your local network, you might need to figure out your local IP and change the corresponding JavaScript files to access this IP:

iOS: iOS-OpenAppChat/www/js/index.js
Firefox OS: fOS-OpenAppChat/www/js/lib/app.js

Change:

io.connect('http://192.168.0.1:3000');

I can also recommend to have a look at nodejitsu, those guys are doing a pretty good job in hosting node.js applications, so you can just change the file package.json, change the subdomain and run

jitsu deploy

to deploy the app to your account. To run one of the apps, change the socket.io connect line to your nodejitsu subdomain which you can specify in package.json.

To install the Firefox OS App in the simulator or on a device, go to the directory and run volo serve.

Feel free to modify the code and please share your experience and learnings with the community.

Couchbase is open source NoSQL for mission-critical systems. If you switch from CouchDB to Couchbase or get started with CouchBase with CouchDB as key-value store, you can use the node-memcache library from Tim Eggert to get and set documents.

The memcache library only handles strings, so you have to use JSON.stringify(data) to add a document to the server and JSON.parse(data) after retrieving one.

Unfortunately, the memcache library doesn't work with design documents and views, so you can use baseview to add and delete design documents and retrieve view data.

npm install baseview

var baseview = require('baseview')('http://127.0.0.1:8092');
baseview.view('design_doc', 'view_name', function(error, data) {
  console.log(error, data);
});

baseview also works with spatial views, to retrieve only documents which have a location within a given bounding box. These bounding boxes (bbox) can be imagined as a rectangle on Google Maps. If the document latitude and longitude are within the rectangle, they are returned by the view, otherwise not.

baseview.spatial('geo', 'points', {bbox: bbox}, function(error, points) {
    console.log(error, points);
  });

To get a bounding box from a given location and radius, there's sparta, a small library for geo calculations.

npm install sparta

sparta = require('sparta')
sparta.boundingBox(41.386549,2.170004,1);

This returns a bbox with the center at Plaza Catalunya, Barcelona, Spain with a radius of 1km.

This is an example of a TEI implementation in JSON for CouchDB. Feel free to use it for your own TEI projects. You can read more about the benefits of JSON and how transcription works on mobile devices in my master thesis “A Modern Approach to the Transcription of Vintage Literature using Mobile Technology and Cloud Services”

<pre> { "header": { "sourceDesc": { "biblFull": { "titleStmt": { "author": { "key": "BLYT", "name": "Lord Lytton, Edward G.D. Bulwer Lytton (1803-1873)" }, "title": "Letter from De Erven F. Bohn to Ouida, 1882-1884: A machine-readable transcription", "summary": "De Erven F. Bohn informs Ouida of that he will send a copy of the Dutch translation of her book by bookpost.", "type": "letter", "recipient": { "key": "OUID", "name": "Ouida" }, "date": { "format": "dd-MM-yyyy", "value": "01-01-1882" } }, "publicationStmt": { "idno": [ { "content": "BOH C 7, fol. 133", "type": "callNo" } ] } } }, "publicationStmt": { "availability": "Publicly accessiblenn© Copyright 2007, Leiden University", "pubPlace": "Leiden, Netherlands", "publisher": "Universiteit Leiden (University of Leiden)", "date": "2007", "address": [ { "addrLine": "P.O. Box 9515" }, { "addrLine": "2300 RA Leiden" } ], "idno": [ { "content": "BOH C 7, fol. 133", "type": "callNo" } ] }, "profileDesc": [ { "textClass": [ { "keywords": [ { "term": "Nonfiction" }, { "term": "Prose" }, { "term": "Masculine" } ], "scheme": "http://www.bookstudies.leidenuniv.nl/" } ], "langUsage": [ { "language": "English", "ident": "en-GB", "usage": "100" } ] } ], "fileDesc": { "titleStmt": { "type": "[electronic resource]", "principal": "Adriaan van der Weel" } }, "encodingDesc": { "projectDesc": "Prepared by students of the 2007-2008 Digital Media Technology course, Book and Digital Media Studies, University of Leiden" } }, "references": [ { "attribute": "title", "content": "In Maremma" }, { "attribute": "abbr", "key": "Yrs", "content": "Yours faithfully" }, { "content": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.", "key": "", "attribute": "recipient" } ], "annotations": [ { "author": "Patrick Heneise", "title": "Hello World", "content": "Just a test", "category": "Nonsense" } ], "transcription": { "closer": { "salute": "We remain, dear miss 1", "signature": "Messrs De Erven F. Bohn" }, "opener": { "salute": "Dear miss!", "dateline": { "name": { "content": "Haarlem", "type": "place" }, "date": { "content": "1882-1884" } } }, "text": "Answering to the conditions made for the Dutch translations of 0 1 we have the pleasure to send you by bookprint a copy of our translation.[[0]]nnAnother paragraph" }, "images": [ { "url": "http://dl.dropbox.com/u/634149/bol133.jpeg" }, { "url": "http://dl.dropbox.com/u/634149/BOHC3_fol254.jpg" } ], "predecessor": "", "successor": "", "history": [ { "timestamp": "11-07-28 7:40 PM", "name": "Patrick Heneise", "description": "Edited Transcription" }, { "timestamp": "11-07-28 10:17 PM", "name": "Patrick Heneise", "description": "Edited Metadata" } ], "_attachments": { "preview.jpg": { "content_type": "image/jpeg", "revpos": 10, "digest": "md5-fXBVqj7c4K1tXJuMxx1E/Q==", "length": 77446, "stub": true } } } </pre>