« Image editing with Flex applications | Main | Monitoring the connection against TCP/IP socket endpoints in AIR using JavaScript »

How to build your own geographical based search engine

Our goal: provide to our website users the possibility to find all items (cinemas, restaurants, hotels...) within a defined range of their current locations, sorting results upon distance from them.

What we need: a database containing the latitude and longitude of the items, an interface to allow users to enter their current location (it can be a raw form or an interactive map, as you prefer), various software depending on your next choice.

Now we have at least 2 ways to reach our goal:

- Sql query:
it's the easier method, we already have data in our DB, we need only to execute a relatively simple query with a sub-query. Let's see an example in detail:

SELECT item_name, SQRT(POW((@user_lat - lat) * 111319.5 * COS(RADIANS(@user_lng)),2) + POW((@user_lng - lng)*110946.3,2)) as distance
from ( select item_name, lat, lng
     from items
     where lat >= (@user_lat - @range_degrees/2)
     and lat <= (@user_lat + @range_degrees/2)
     and lng >= (@user_lng - @range_degrees/2)
     and lng <= (@user_lng + @range_degrees/2)
) as boundary
WHERE distance < @range_meters
ORDER by distance;

Using this formula distance is given in meters, and you need to convert user choosen range in meters and degrees. Values 111319.5 and 110946.3 are based on this information.

Patrick O'Leary in his article on GIS based search use an Euclidian formula,I don't know if it's similar or it returns distance in miles etc., I haven't tried it so pay attention when you use his or my formula, do some testing!

- Lucene:
Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java.
Local Lucene is an extension of Lucene providing an implementation of geographical searching. Another similar implementation is GeoLucene but it's still an earlier alpha and his developer is unable to work on this project. Both these software are faster on geographic queries than the unmodified Lucene and for sure faster than Zend_Search_Lucene too.
If you can use Java I recommend to choose Local Lucene, in his whitepaper pjaol shows a comparison beetween Mysql, Lucene and LocalLucene. Instead if you want to use the search capabilities of Lucene in PHP, you can use Solr and, in example, a client like this SolPHP or obtain a JSON response from the server. In this case you can use LocalSolr that is a porting of the LocalLucene library onto the Solr search server.
On GISSearch you can find all information needed to easily build your fast, scalable and reliable geographical search engine.

I hope it will be helpful to you and to your users.

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/60

Listed below are links to weblogs that reference How to build your own geographical based search engine:

» Solr Bookmarks from Post Saver - Website voting and saving system
Bookmarked your page with keywords solr! [Read More]

Comments (4)

or you could just use postGIS.

I'm sure there are many other ways too, so everyone could choose the easier one.
Thank you for your hint, I never used PostgreSQL, have you some benchmarks or comparisons with other software using this GIS based query?

Hi

Thanks for the mention, there are lots of applications out there for GIS, many are faster than LocalLucene / LocalSolr when it comes to radial searching, but the question you have to ask is what are your goals. LocalLucene was developed as a solution to text searching bounded by a geographical area.

PostGIS is fantastic with spatial indexes, but it's t-search capabilities leave a lot to be desired, this is where LocalLucene steps in to help fill the void.

PostGIS will beat some of the best GIS apps out there, but we are solving the "Local Search" problem, and doing it better than PostGIS currently can.

Thanks
P aka (pjaol)

wishing f:

I really had no idea before I started reading here how that thing worked. I guessed it required some location data to be entered but didn't know any details.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on September 4, 2008 12:12 PM.

The previous post in this blog was Image editing with Flex applications.

The next post in this blog is Monitoring the connection against TCP/IP socket endpoints in AIR using JavaScript.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33