Issues working with complex math

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Trausch

    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($la t,
    $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:


    The sample run:
    [color=blue]
    > ./zip_test.php[/color]
    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[color=blue]
    >[/color]
  • Mitul

    #2
    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_dista nce($lat1, $lon1, $lat2, $lon2) {
    $theta = $lon1 - $lon2."<br>";
    $dist = sin(deg2rad($la t1)) * sin(deg2rad($la t2)) +
    cos(deg2rad($la t1)) * cos(deg2rad($la t2)) * cos(deg2rad($th eta));
    $dist = acos($dist);
    $dist = rad2deg($dist);

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

    }

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

    Comment

    • Sjoerd

      #3
      Re: Issues working with complex math

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

      This could be a case of rounding errors, try the BC functions in PHP:


      Comment

      • Michael Trausch

        #4
        Re: Issues working with complex math

        Sjoerd wrote:[color=blue]
        > Michael Trausch wrote:[color=green]
        >> The problem as best as I can tell resides in the latlong_box($la t,
        >> $long, $miles) function that I've written in PHP, but I'm not seeing
        >> anything different, computationally , from what I've implemented in bc;[/color]
        >
        > This could be a case of rounding errors, try the BC functions in PHP:
        > http://www.php.net/manual/en/ref.bc.php
        >[/color]

        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

        Comment

        • noone

          #5
          Re: Issues working with complex math

          Michael Trausch wrote:[color=blue]
          > 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($la t,
          > $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:
          >
          >[color=green]
          >>./zip_test.php[/color]
          >
          > 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[/color]
          [color=blue]
          > Returned 0 rows[/color]

          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

          Comment

          • Michael Trausch

            #6
            Re: Issues working with complex math

            noone wrote:[color=blue]
            >
            > 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
            >[/color]

            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:
            [color=blue]
            > ./zip_test.php 30034 5[/color]

            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[color=blue]
            >[/color]

            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

            Comment

            • Jerry Stuckle

              #7
              Re: Issues working with complex math

              Michael Trausch wrote:[color=blue]
              > noone wrote:
              >[color=green]
              >>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
              >>[/color]
              >
              >
              > 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:
              >
              >[color=green]
              >>./zip_test.php 30034 5[/color]
              >
              >
              > 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[/color]

              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@attgl obal.net
              =============== ===

              Comment

              Working...