Lucene and geographic location search to find events that have occurred within 5 miles of the sea, land, and Air

Source: Internet
Author: User
Geographical searches with latitude and long1_part I

Calculating the distance between two geographical locations is a common feature of Web 2.0 applications. this is usually a two-step process. firstly, obtaining the latitudes and longitudes of the locations from their addresses, a process known as geocoding. secondly, calculating the great-circle distance between these two points and doing something meaningful with this value.

In this first of a 2-part article, we'll be looking at the design and concept of the solution, and in the second part, we'll examine the actual implementation.

Geocoding

The problem of geocoding is one that has been quite comprehensively addressed by several companies, amongst themMapQuestAndYahoo.

Usage of these rest Web Services is fairly straightforward-You supply either a zip code or an address, and a latitude and longpolling is returned in XML format. all that's needed is to parse the output XML to obtain the LAT/long.

We will therefore examine how to calculate the distance between two points and how to integrate this with Lucene to produce some cool geographical searches.

The Design

Suppose we have a database of events, each with a location address. Given any arbitrary address, we need to use Lucene to produce a list of events which are within 50 miles of this address.

We start by creating the Lucene index of the events, with each event corresponding to a document. in addition to the Event-related fields such as name, date, address, organizer, ETC we will also attempt to geocode the event's location address into latitude and longdistance and add these as fields.

When a search is saved med, the input address will also be geocoded into latitude/longpolling. the rectangle (or circle if you wish) containing the required search area will be calculated (50 miles from the given address ). this rectangle will then be supplied to a custom Query Class latlongquery, which will do the job of scoring the hits such that only clients with a latitude/longpolling within the desired area will return, and that sort ents closer to the center will have a higher score.

Calculating distance between two points

To calculate the distance between two points, we need to calculate the great-circle distance between the two points because the earth is not flat but spherical.

Http://www.meridianworlddata.com/Distance-Calculation.aspContains a simple formula for calculating great-circle distance (but has up to 10% error). You're welcome to use a more accurate formula if you wish.

 
An approximate formula for distance bet. lat and long is d = SQRT (x ^ 2 + y ^ 2) where x = 69.1 (LAT2-LAT1) and Y = 69.1 * (lon2-lon1) * Cos (LAT1/57.3)

We want to find Max and Min LAT/long to obtain the bounding rectangle, which basically means alternately setting X and Y = 0

To be continued...

We 've mapped the application to the Lucene model, decided we're going to use a custom query and scorer to perform the actual searching, and also looked at how to calculate great-circle distance.

In the second part of this article, we'll examine the actual implementation for noteworthy points.

Credits

The code and concept used in this article was developed whilst working forSearch engine for realtors, Homethinking.com. You can seeCode in action.

KudosNiki, Homethinking head-honcho for allowing me to release this code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.