Issues working with complex math

This is a discussion on Issues working with complex math within the PHP Language forums, part of the PHP Programming Forums category; Hello everyone, I'm having a little bit of trouble trying to implement some arithmetic logic into an application that ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-03-2006
Michael Trausch
 
Posts: n/a
Default Issues working with complex math

Hello everyone,

I'm having a little bit of trouble trying to implement some arithmetic
logic into an application that I'm working on, and I'm hoping that
somebody can possibly point me in the right direction. I am working
with a database with ZIP codes, latitudes, and longitudes, and am
working to implement the Haversine formula alongside with another
formula for creating a "box" that I can use to get locations from within
a square (or as close as you can get with the Earth, anyway).

While I was reading up on the math and working through the problem, I
wrote a function (well, two) in the bc calculator language to process
this information manually while initially working with the concepts.
Now that I have a semi-functional understanding of the problem, I
rewrote the logic in PHP, and am having a problem actually computing
longitude portions of coordinates for the "box" that I want to use to
pull data about a given region.

The problem as best as I can tell resides in the latlong_box($lat,
$long, $miles) function that I've written in PHP, but I'm not seeing
anything different, computationally, from what I've implemented in bc;
of course, I could just be suffering from staring at it too long, too.
:-) I'm not sure if I am expecting something that PHP won't provide, or
if I'm handling something not quite right in PHP's arithmetic eyes, or
what, really. The latitudes being computed are correct, it's just the
longitudes that are way off.

I will go ahead and give a link to the code and include a sample run of
the script. I appreciate any help/pointers!

Thanks in advance,
Mike

The source:
http://www.staffasap.com/zip_test.phps

The sample run:

> ./zip_test.php

Array
(
[zipcode] => 30034
[latitude] => +33.6907570
[longitude] => -084.2511710
[city] => DECATUR
[state] => GEORGIA
[abbr] => GA
)

Query SELECT * FROM dataZipCode WHERE latitude <= 33.980220566111 AND
latitude >= 33.401293433889 AND longitude <= -122.70422048692 AND
longitude >= -122.70422048692
Returned 0 rows
>

Reply With Quote
  #2 (permalink)  
Old 03-03-2006
Mitul
 
Posts: n/a
Default Re: Issues working with complex math

Hello friend,

I hope that following function will helpful to your. This function will
use to calculate circular distance

function Calculate_distance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2."<br>";
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);

return $miles = round($dist * 60 * 1.1515);

}

With Regards,
Mitul Patel
mitul [at] siliconinfo [dot] com

Reply With Quote
  #3 (permalink)  
Old 03-03-2006
Sjoerd
 
Posts: n/a
Default Re: Issues working with complex math

Michael Trausch wrote:
> The problem as best as I can tell resides in the latlong_box($lat,
> $long, $miles) function that I've written in PHP, but I'm not seeing
> anything different, computationally, from what I've implemented in bc;


This could be a case of rounding errors, try the BC functions in PHP:
http://www.php.net/manual/en/ref.bc.php

Reply With Quote
  #4 (permalink)  
Old 03-03-2006
Michael Trausch
 
Posts: n/a
Default Re: Issues working with complex math

Sjoerd wrote:
> Michael Trausch wrote:
>> The problem as best as I can tell resides in the latlong_box($lat,
>> $long, $miles) function that I've written in PHP, but I'm not seeing
>> anything different, computationally, from what I've implemented in bc;

>
> This could be a case of rounding errors, try the BC functions in PHP:
> http://www.php.net/manual/en/ref.bc.php
>


Interesting. Is there any way to decrease the rounding error, or, to
have the functions asin, sin, cos, and friends all work with the bcmath
thing?

Thanks,
Mike
Reply With Quote
  #5 (permalink)  
Old 03-05-2006
noone
 
Posts: n/a
Default Re: Issues working with complex math

Michael Trausch wrote:
> Hello everyone,
>
> I'm having a little bit of trouble trying to implement some arithmetic
> logic into an application that I'm working on, and I'm hoping that
> somebody can possibly point me in the right direction. I am working
> with a database with ZIP codes, latitudes, and longitudes, and am
> working to implement the Haversine formula alongside with another
> formula for creating a "box" that I can use to get locations from within
> a square (or as close as you can get with the Earth, anyway).
>
> While I was reading up on the math and working through the problem, I
> wrote a function (well, two) in the bc calculator language to process
> this information manually while initially working with the concepts.
> Now that I have a semi-functional understanding of the problem, I
> rewrote the logic in PHP, and am having a problem actually computing
> longitude portions of coordinates for the "box" that I want to use to
> pull data about a given region.
>
> The problem as best as I can tell resides in the latlong_box($lat,
> $long, $miles) function that I've written in PHP, but I'm not seeing
> anything different, computationally, from what I've implemented in bc;
> of course, I could just be suffering from staring at it too long, too.
> :-) I'm not sure if I am expecting something that PHP won't provide, or
> if I'm handling something not quite right in PHP's arithmetic eyes, or
> what, really. The latitudes being computed are correct, it's just the
> longitudes that are way off.
>
> I will go ahead and give a link to the code and include a sample run of
> the script. I appreciate any help/pointers!
>
> Thanks in advance,
> Mike
>
> The source:
> http://www.staffasap.com/zip_test.phps
>
> The sample run:
>
>
>>./zip_test.php

>
> Array
> (
> [zipcode] => 30034
> [latitude] => +33.6907570
> [longitude] => -084.2511710
> [city] => DECATUR
> [state] => GEORGIA
> [abbr] => GA
> )
>
> Query SELECT * FROM dataZipCode WHERE latitude <= 33.980220566111 AND
> latitude >= 33.401293433889 AND longitude <= -122.70422048692 AND
> longitude >= -122.70422048692


> Returned 0 rows


if Decatur GA is at [longitude] => -084.2511710 where is the range in
your query .. should it not be somwhere between -70 and -122

[longitude <= -122.70422048692 AND longitude >= -122.70422048692] =
-122.70422048692

Reply With Quote
  #6 (permalink)  
Old 03-05-2006
Michael Trausch
 
Posts: n/a
Default Re: Issues working with complex math

noone wrote:
>
> if Decatur GA is at [longitude] => -084.2511710 where is the range in
> your query .. should it not be somwhere between -70 and -122
>
> [longitude <= -122.70422048692 AND longitude >= -122.70422048692] =
> -122.70422048692
>


Yeah, the problem was something along the lines of how I implemented the
hunting algorithm to deduce the valid range to look in. for a 20 mile
radius, it works out differently - vastly, actually:

for a five mi radius it works:

> ./zip_test.php 30034 5


Query SELECT * FROM dataZipCode WHERE latitude <= 33.763122891528 AND
latitude >= 33.618391108472 AND longitude <= -84.211503845018 AND
longitude >= -84.290838154982 ORDER BY abbr,city
Returned 3 rows
City: Atlanta State: GA ZIP: 31132 Dist: 3.1496162946772
City: Decatur State: GA ZIP: 30032 Dist: 4.2562533102956
City: Decatur State: GA ZIP: 30034 Dist: 0
>


As well as a 20 mile radius:

Query SELECT * FROM dataZipCode WHERE latitude <= 33.980220566111 AND
latitude >= 33.401293433889 AND longitude <= -84.092501398957 AND
longitude >= -84.409840601043 ORDER BY abbr,city
Returned 65 rows
City: Atlanta State: GA ZIP: 30307 Dist: 7.8779054595022
City: Atlanta State: GA ZIP: 30340 Dist: 13.988388086945
City: Atlanta State: GA ZIP: 30338 Dist: 19.540119063232
City: Atlanta State: GA ZIP: 30335 Dist: 9.0276794496919
City: Atlanta State: GA ZIP: 30315 Dist: 7.6332861615796
City: Atlanta State: GA ZIP: 30332 Dist: 10.300183017737
City: Atlanta State: GA ZIP: 30329 Dist: 10.293572217837
City: Atlanta State: GA ZIP: 30328 Dist: 18.716589259417
City: Atlanta State: GA ZIP: 30326 Dist: 12.656176679345
City: Atlanta State: GA ZIP: 30309 Dist: 10.784558977204
City: Atlanta State: GA ZIP: 30324 Dist: 11.123997032003
City: Atlanta State: GA ZIP: 30322 Dist: 8.3657588395676
City: Atlanta State: GA ZIP: 30319 Dist: 13.608382045463
City: Atlanta State: GA ZIP: 30317 Dist: 7.1986920468663
City: Atlanta State: GA ZIP: 30342 Dist: 15.071788907405
City: Atlanta State: GA ZIP: 30345 Dist: 10.838995754932
City: Atlanta State: GA ZIP: 30306 Dist: 9.1595992448634

[... cutting more of the stuff out]

It doesn't do a perfect radius yet, but I don't think I need it to
unless I start looking at distances > 30 miles... using a square can get
to be covering a bit of a too broad area when dealing with things larger
then that.

thanks for the help guys,
] mike
Reply With Quote
  #7 (permalink)  
Old 03-05-2006
Jerry Stuckle
 
Posts: n/a
Default Re: Issues working with complex math

Michael Trausch wrote:
> noone wrote:
>
>>if Decatur GA is at [longitude] => -084.2511710 where is the range in
>>your query .. should it not be somwhere between -70 and -122
>>
>>[longitude <= -122.70422048692 AND longitude >= -122.70422048692] =
>>-122.70422048692
>>

>
>
> Yeah, the problem was something along the lines of how I implemented the
> hunting algorithm to deduce the valid range to look in. for a 20 mile
> radius, it works out differently - vastly, actually:
>
> for a five mi radius it works:
>
>
>>./zip_test.php 30034 5

>
>
> Query SELECT * FROM dataZipCode WHERE latitude <= 33.763122891528 AND
> latitude >= 33.618391108472 AND longitude <= -84.211503845018 AND
> longitude >= -84.290838154982 ORDER BY abbr,city
> Returned 3 rows
> City: Atlanta State: GA ZIP: 31132 Dist: 3.1496162946772
> City: Decatur State: GA ZIP: 30032 Dist: 4.2562533102956
> City: Decatur State: GA ZIP: 30034 Dist: 0
>
>
> As well as a 20 mile radius:
>
> Query SELECT * FROM dataZipCode WHERE latitude <= 33.980220566111 AND
> latitude >= 33.401293433889 AND longitude <= -84.092501398957 AND
> longitude >= -84.409840601043 ORDER BY abbr,city
> Returned 65 rows
> City: Atlanta State: GA ZIP: 30307 Dist: 7.8779054595022
> City: Atlanta State: GA ZIP: 30340 Dist: 13.988388086945
> City: Atlanta State: GA ZIP: 30338 Dist: 19.540119063232
> City: Atlanta State: GA ZIP: 30335 Dist: 9.0276794496919
> City: Atlanta State: GA ZIP: 30315 Dist: 7.6332861615796
> City: Atlanta State: GA ZIP: 30332 Dist: 10.300183017737
> City: Atlanta State: GA ZIP: 30329 Dist: 10.293572217837
> City: Atlanta State: GA ZIP: 30328 Dist: 18.716589259417
> City: Atlanta State: GA ZIP: 30326 Dist: 12.656176679345
> City: Atlanta State: GA ZIP: 30309 Dist: 10.784558977204
> City: Atlanta State: GA ZIP: 30324 Dist: 11.123997032003
> City: Atlanta State: GA ZIP: 30322 Dist: 8.3657588395676
> City: Atlanta State: GA ZIP: 30319 Dist: 13.608382045463
> City: Atlanta State: GA ZIP: 30317 Dist: 7.1986920468663
> City: Atlanta State: GA ZIP: 30342 Dist: 15.071788907405
> City: Atlanta State: GA ZIP: 30345 Dist: 10.838995754932
> City: Atlanta State: GA ZIP: 30306 Dist: 9.1595992448634
>
> [... cutting more of the stuff out]
>
> It doesn't do a perfect radius yet, but I don't think I need it to
> unless I start looking at distances > 30 miles... using a square can get
> to be covering a bit of a too broad area when dealing with things larger
> then that.
>
> thanks for the help guys,
> ] mike


I did something like this not too long ago.

It would be nice if you could search the database for something within a
circle :-). Unfortunately, it just doesn't work that way.

I did it similar to how you did - created a square and got everything
within it. Then I recalculated the distance and manually filtered out
anything not within the required radius. Worked fine and was quite quick.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:08 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0