get item by longitude and latitude inside a square

0 views
Skip to first unread message

Stéphane Mourey

unread,
Mar 15, 2010, 12:14:36 PM3/15/10
to user
Hi all,

I have a database where cities (villes in french) are stored, with longitude
and latitude values. I'm trying to get some of them, inside a square.
Here is one request:

/sofitex/_design/all/_view/getVillesByLatAndLong?startkey=%5B6.84505190241%2C47.6459660001%5D&endkey=%5B8.64494809759%2C49.4624339999%5D&include_docs=true

Here is the view code:

"getVillesByLatAndLong": {
"map": "function(doc) {if (doc.type=='ville')
{emit([doc.lat,doc.long], doc);};}"
}
The result is empty, but I know I have some cities that match. Could anyone
tell what's wrong?

Best regards,

Stephane Mourey

Alex P

unread,
Mar 15, 2010, 12:32:06 PM3/15/10
to us...@couchdb.apache.org
more than likely it's an issue with actual collation vs what you're
expecting. care to show some sample view output?

2010/3/15 Stéphane Mourey <stephan...@gmail.com>

Stéphane Mourey

unread,
Mar 15, 2010, 12:44:23 PM3/15/10
to us...@couchdb.apache.org
Here is the first lines of the view output, without startkey and endkey
(i.e. sofitex/_design/all/_view/getVillesByLatAndLong)

{"total_rows":73751,"offset":0,"rows":[
{"id":"2ab960eb967f8d14f5b6bc1837c2c7fa","key":["",""],"value":{"_id":"2ab960eb967f8d14f5b6bc1837c2c7fa","_rev":"1-bbd075701f8fb02ec7578c71dd631113","type":"ville","id":"142787","nom":"Monaco","cp":"98000","long":"","lat":"","country":"fr"}},
{"id":"6f27cc15079e4317cb6488741abfdec1","key":["-0.015","48.1033"],"value":{"_id":"6f27cc15079e4317cb6488741abfdec1","_rev":"1-435bccb2ad1a150099dd4dd2c0f412c0","type":"ville","id":"133962","nom":"La
Chapelle St Fray","cp":"72240","long":"48.1033","lat":"-0.015","country":"fr"}},

Here is the output, given startkey and endkey (i.e.
/sofitex/_design/all/_view/getVillesByLatAndLong?startkey=%5B6.84505190241%2C47.6459660001%5D&amp;endkey=%5B8.64494809759%2C49.4624339999%5D&amp;include_docs=true):

{"total_rows":73751,"offset":0,"rows":[]}

I get the same when startkey is [0,0] and endkey is [999,999] (i.e.
/sofitex/_design/all/_view/getVillesByLatAndLong?startkey=[0%2C0]&amp;endkey=[999%2C999]&amp;include_docs=true),
but what is expected is all the cities.

2010/3/15 Alex P <aped...@kolosy.com>

Christopher O'Connell

unread,
Mar 15, 2010, 12:50:24 PM3/15/10
to us...@couchdb.apache.org
You cannot do true "bounding box" queries with couch. You'll need to use one
of teh geo-indexing solutions for that.

2010/3/15 Stéphane Mourey <stephan...@gmail.com>

Jan Lehnardt

unread,
Mar 15, 2010, 12:54:41 PM3/15/10
to us...@couchdb.apache.org
For a simple solution, you could compute the geo-hash from the lat/lon values and put that into the key of the view.

Cheers
Jan
--

Robert Newson

unread,
Mar 15, 2010, 12:54:25 PM3/15/10
to us...@couchdb.apache.org
couchdb-lucene 0.5 can also do bounding box searches;

?q=pizza AND lat<double>:[51.4707 TO 51.5224] AND
long<double>:[-0.6622 TO -0.5775]

0.5 isn't released (yet) but it's fairly stable at this point.

B.

Volker Mische

unread,
Mar 15, 2010, 1:24:38 PM3/15/10
to us...@couchdb.apache.org
Hi Stephane,

the problem is that CouchDB views sort by first part of the key, then by
the second. So the keys of your view might look like:

[1.45,2.45]
[20.96,19.75]
[38.58,3.18]
[45.09,60.30]
[45.09,72.20]
[47.92,23.78]

If you use startkey/endkey only once slice will be cut out. For
startkey=[35.0,1.0],endkey=[46.0,65.0] you'll only get
[38.58,3.18]
[45.09,60.30]
back (and not all the other ones where the second part of the key would
match.

As the others already proposed you need to use an geo-index solution
like geocouch, couchdb-lucene or a geohash.

Cheers,
Volker

Reply all
Reply to author
Forward
0 new messages