CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Proximity search doesn't seem to be accurate
Pages: [1]

Author Topic: Proximity search doesn't seem to be accurate  (Read 610 times)

aeszq

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 0
Proximity search doesn't seem to be accurate
October 16, 2013, 10:26:35 pm
Hi,

The proximity search based on address doesn't seem to be very accurate( in comparison to Goolge MAP's result). When I do a search to get contacts from an radius of 500km area, it returns addresses even 800km away from the target address. Have anyone experienced the same?

Cheers,
George

aeszq

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 0
Re: Proximity search doesn't seem to be accurate
December 03, 2013, 09:12:09 pm
I have changed the calculation in civicrm/CRM/Contact/BAO/ProximityQuery.php to improve the accuracy. Below is the details of what I have done based on version 4.4

Original - line 244:
$where = "
{$tablePrefix}.geo_code_1  >= $minLatitude  AND
{$tablePrefix}.geo_code_1  <= $maxLatitude  AND
{$tablePrefix}.geo_code_2 >= $minLongitude AND
{$tablePrefix}.geo_code_2 <= $maxLongitude AND
$earthDistanceSQL  <= $distance
";

modification - line 244:  $where = "
{$tablePrefix}.geo_code_1  >= $minLatitude  AND
{$tablePrefix}.geo_code_1  <= $maxLatitude  AND
{$tablePrefix}.geo_code_2 >= $minLongitude AND
{$tablePrefix}.geo_code_2 <= $maxLongitude AND
acos(
      cos(radians( {$tablePrefix}.geo_code_1 ))
    * cos(radians( $latitude ))
    * cos(radians( {$tablePrefix}.geo_code_2 ) - radians( $longitude ))
    + sin(radians( {$tablePrefix}.geo_code_1 ))
    * sin(radians( $latitude ))
  )*6378137  <= $distance
";

I have tested the results against http://www.movable-type.co.uk/scripts/latlong.html . The difference are within 2%

Hope this would be helpful for whoever are having the similar issues with proximity Search

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Proximity search doesn't seem to be accurate
December 04, 2013, 06:19:47 am

hey george

any reference links to the new equation that you are using? Would be good to note that in our code.

I'll create an issue and PR for this change. seems like a good step forward. Thanx for looking into this and improving it significantly :)

Can you try this patch here: https://github.com/dlobo/civicrm-core/commit/a3b80194747be3b0bc359971464a0fe57c4a4f45.patch

which is basically your patch incorporated into the file. Would be good to get a reference so we can change the comments at top of the file

thanx

lobo
« Last Edit: December 04, 2013, 06:32:20 am by Donald Lobo »
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

aeszq

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 0
Re: Proximity search doesn't seem to be accurate
December 04, 2013, 08:38:07 pm
Hi Lobo,

Thanks for doing the patch!

The equation is the Great-circle distance, here is the wiki page http://en.wikipedia.org/wiki/Great-circle_distance

The change I made was based on it's Excel verion:   Distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(lon2-lon1))*6371
I found this at http://www.movable-type.co.uk/scripts/latlong.html

The only difference between my code and the excel code above is the radius of earth. I put 6378137m while the code above uses 6371km. I'm not too sure which one of them is more accurate.

Cheers,
George




Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Proximity search doesn't seem to be accurate

This forum was archived on 2017-11-26.