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.
Comments (4)
or you could just use postGIS.
Posted by PaulH | September 4, 2008 3:26 PM
Posted on September 4, 2008 15:26
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?
Posted by Francesco Rapanà | September 4, 2008 4:33 PM
Posted on September 4, 2008 16:33
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)
Posted by Patrick O'leary | September 15, 2008 10:49 PM
Posted on September 15, 2008 22:49
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.
Posted by wishing f | April 18, 2011 1:16 PM
Posted on April 18, 2011 13:16