Bounding Box Size

52 views
Skip to first unread message

Olivia

unread,
Apr 30, 2013, 12:30:59 AM4/30/13
to nzp...@googlegroups.com
Hi all,
 
I am using PHP 5.3.9 and I am trying to figure out how to calculate the size of a bounding box based on the passed in coordinates of the 4 corners of a geographical map.
The reason for this is that I need to check that the size/area of the bounding box is not larger than 20 Kilometres.
 
The bounding box format is defined as:

$southwest_lat = $_GET['swlat'];
$southwest_lng = $_GET['swlng'];
$northeast_lat =  $_GET['nelat'];
$northeast_lng = $_GET['nelng'];

Your help is much appreciated.

Kind regards,
Olivia

 

Aaron Singline - Turboweb Limited

unread,
Apr 30, 2013, 12:37:02 AM4/30/13
to nzp...@googlegroups.com
Yay an actual question about PHP! :D

I think you'll need more data at the moment you seem to be only able to calculate a straight line as you have essentially got a x and y location for a start and finish.

http://www.movable-type.co.uk/scripts/latlong.html

Gives some yummy formulae to work with.  Javascript examples but I'm sure you can convert it easy enough. :)

Cheers,


Aaron

Aaron Singline (about me)
Co-Founder / Support
Turboweb Limited
Office: +64 3 474 5953
Cell: 022 657 8302
Web: www.turboweb.co.nz
Twitter: twitter.com/turboweb
Facebook: facebook.com/turboweb
Turboweb Limited
23 Vogel St
Dunedin 9016
New Zealand

Need help?  Please email sup...@turboweb.co.nz

--
--
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to nzp...@googlegroups.com
To unsubscribe, send email to
nzphpug+u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "NZ PHP Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nzphpug+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jason Kang

unread,
Apr 30, 2013, 12:49:26 AM4/30/13
to nzp...@googlegroups.com
I've found a good answer for you! :-D

http://bit.ly/11SaJ04



Or

$southwest_lat = $_GET['swlat'];
$southwest_lng = $_GET['swlng'];
$northeast_lat =  $_GET['nelat'];
$northeast_lng = $_GET['nelng'];

$result = distance($southwest_lat, $southwest_lng, $northeast_lat, $northeast_lng, "K");
if($result > 20) {
  echo "over 20!";
} else {
  echo "over 20!";
}
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}


--

Tim Oliver

unread,
Apr 30, 2013, 12:57:57 AM4/30/13
to nzp...@googlegroups.com
Area/size of the bounding box may not be distance between the two points - do you mean a bounding box that's aligned with edges parallel to latitude and longitude? And did you want distance less than 20km, area less than 20 square kilometers or some dimension less than 20km? This gets pretty hairy if the box can be large since the area of a 'rectangular' region on the sphere is larger than on a flat surface.

Jason Kang

unread,
Apr 30, 2013, 1:10:11 AM4/30/13
to nzp...@googlegroups.com
oops sorry. My answer was about the distance, not space.


anru

unread,
Apr 30, 2013, 2:33:50 AM4/30/13
to nzp...@googlegroups.com
This article may help to calculate size of bounding box. it is PHP based.

http://sgowtham.net/ramblings/2009/08/04/php-calculating-distance-between-two-locations-given-their-gps-coordinates/


regards,

anru


On 30/04/2013 4:30 p.m., Olivia wrote:
--

Olivia Brown

unread,
Apr 30, 2013, 3:06:25 AM4/30/13
to nzp...@googlegroups.com
Hi Tim,
 
Yes I need to work out the area size of a rectangular (bounding box) on a geographical map given a Southwest latitude/longitude and a Northeast latitude/longitude geographic coordinate.
 
If the size of the bounding box is <= 20 kilometres than I retrieve all the rows from the database that are within that bounding box but if the bounding box is larger than 20 Kilometres than I display an error message (i.e. Area too large)
 
Thank you for everyone's help so far


On Tue, Apr 30, 2013 at 4:57 PM, Tim Oliver <t...@xi.co.nz> wrote:
You received this message because you are subscribed to a topic in the Google Groups "NZ PHP Users Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nzphpug/CxLD69RZC-Y/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to nzphpug+u...@googlegroups.com.

Dmitrii Ruban

unread,
Apr 30, 2013, 6:21:53 AM4/30/13
to nzp...@googlegroups.com
Because in your case 20km is not much you can neglect that you are measuring the area on a sphere and consider it is flat. So just calculate the distance between two point on axel X and Y and then multiply them as you would do for basic rectangle.  

Using code below;
Reply all
Reply to author
Forward
0 new messages