0

I have a set of documents in CouchDB that are geolocalized - that is, they have a structure like

{
  _id: ...,
  ...
  coords: {
    lat: 45.204149214,
    long: 40.284712972
  }
}

I want to be able to display them on a map. This requires me to get all documents in a given square. It is easy to get all documents with longitude in a given interval:

// map function
function(doc) {
  emit(doc.coords.long, doc.other_stuff);
}

using a query like

// query
/path/to/view/?startkey=40&endkey=50

The problem is that I do not see how to do multidimensional constraints. How to get all documents with latitude between, say, 20 and 30 and longitude between 40 and 50? I see I can specify multiple keys, but that will or the results.

One option would be to do further filtering on the client, but given the size of the data, this may be unfeasible.

Disclaimer: there are a few similar questions on SO, basically stating that this is not possible. Since they all look dated, I would like to know whether something has changed in the latest releases of CouchDB.

Community
  • 1
  • 1
Andrea
  • 20,253
  • 23
  • 114
  • 183

1 Answers1

2

You are correct, you cannot do multidimensional queries. But there is GeoCouch. It pretty easy to add to couch db through build-couchdb, or you can use iriscouch hosting which has it baked in by default.

As you can see Using Geocouch is fairly easy, and a query takes a bounding box like this :

http://localhost:5984/places/_design/main/_spatial/points?bbox=0,0,180,90

If you dont want to use geocouch (for some reason), I have successfully used a GeoHash which maps the two dimensions into a one dimensional space. Here is the view code, and here is the query. As you can see, it is not as elegant, but it is portable across couchdb versions, and it also works.

Ryan Ramage
  • 2,606
  • 18
  • 17