Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

@

Posts: 5,949 | Comments: 44,546

filter by tags archive

Geo Location & Spatial Searches with RavenDB–Part V-Spatial Searching


So, all of this have gone quite far. We have seen that we can quite easily go from having the user’s IP address to figuring out its location using our Geo Location database.

The next step is to actually do something about it. Usually, when doing geo location, you care to know what the human readable name for the user’s location is, but in our case, what we most care about is the user’s physical location, so it is lucky that the geo location database that we found also include longitude and latitude information.

With that, we can define the following index, on our events. The Longitude & Latitude information is actually calculated by the browser using the Google Geocoder API, so we just plug in the address, and the site does the rest by figuring out where on the globe we are.

This index allows us to search by spatial query as well as by time:

image

Using that, we can do:

image

First, we try to find the location of the user based on the user IP, then we are making a spatial query on top of the events, based on the user location.

What we are doing essentially is asking, “show me the next 2 future events within 200 miles of the user’s location”. Just a tiny little bit of code, but it produces this:

image

And hopefully, this will narrow things down for you to the obvious: “Of course I am going to the next RavenDB Course”!


Comments

Arjen Smits

What is SpatialIndex, is it part of the RavenDB framework? If so, does it also have build in support for working with the metric system? For example, what if I want to specify 200km, or 15 meters for that matter..

Ayende Rahien

Arjen, Yes, SpatialIndex is part of RavenDB. It allows you to do geo-spatial queries on the data. And in 1.2 we have metric system support as well\

Kaveh Shahbazian

Is it possible to get "nearest neighbor" with RavenDB? (To be more pragmatic "nearest neighbor in a radius").

Ayende Rahien

Kaveh, You can get, "nearest from point", yes.

Kaveh Shahbazian

Thanks! It would be great if you lead me to some sample code. And a question: Does this algorithm calculate spherical distance (based on spherical shape of the earth) or Cartesian distance? Because Cartesian distance is not accurate (at least for me) especially in 1/4 latitude near poles.

Ayende Rahien

Kaveh, Please ask those questions in the mailing list. The code is open and you can look at that. We use a port of sptial4j, so you can check the docs on that. Distance is done using spherical math

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. The RavenDB Comic Strip: Part III – High availability & sleeping soundly - 3 hours from now

There are posts all the way to May 28, 2015

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    20 May 2015 - Part II – a team in trouble!
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats