Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Queries++ in RavenDB: Spatial Searches

DZone's Guide to

Queries++ in RavenDB: Spatial Searches

With RavenDB 4.0, you can now query spatial data with much greater reach than you could before. Come check out how it works!

· Database Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

Spatial queries are fun when you look at them from the outside — but not so fun when you are working to implement them. But that is probably not your concern.

RavenDB has had support for spatial queries for many years now, but the RavenDB 4.0 release has touched on that, as well. And now, you can query spatial data with much greater reach. Here is a small sample of how this works:

from Employees 
where spatial.within(
    spatial.point(Address.Location.Latitude, Address.Location.Longitude), 
    spatial.wkt("POLYGON((-122.32246398925781 47.643055992166275,-122.32795715332031 47.62917538239487,-122.33207702636719 47.60904194838943,-122.32109069824219 47.595846873927044,-122.31422424316406 47.594920778814824,-122.30701446533203 47.58959541384278,-122.28538513183594 47.59029005739745,-122.27989196777344 47.620382422330565,-122.28401184082031 47.62454769305083,-122.27645874023438 47.632414521155376,-122.27577209472656 47.6421307328982,-122.29328155517578 47.64536906863988,-122.32246398925781 47.643055992166275))")
)

This query is doing a polygon search for all the employees located inside that polygon. You can visualize the query on the map. We have four employees (in yellow) in the viewport and two of them are included within the specified polygon (in blue).

image

And here is what this looks like in the studio:

image

In this case, you can see how we support automatic indexing of spatial data. You can also define your own spatial indexes if you need greater control but it is as easy as pie to just go ahead and start running it.

From code, this is just as easy:

  var events = session.Query<Events_SpatialIndex.Result, Events_SpatialIndex>()
      .Spatial(x => x.Coordinates, x => x.WithinRadius(1243.0, 10.1230, 10.1230))
      .OrderBy(x => x.Name)
      .OfType<Event>()
      .ToArray();

I’m not sure why, but when looking at the results, this just feels like magic.

Do you pay to use your database? What if your database paid you? Learn more with RavenDB.

Topics:
database ,queries ,spatial search

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}