Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a latitude and longitude, and distance, I want to find a bounding box

Given a latitude and longitude, and distance, I want to find a bounding box where the distances are less than the given distance.

This questions was asked here: How to calculate the bounding box for a given lat/lng location?

I donot want this partcularly accurate, so I have modified and simplified it to

def boundingBox(latitudeInDegrees, longitudeInDegrees, halfSideInKm):
    lat = math.radians(latitudeInDegrees)
    lon = math.radians(longitudeInDegrees)
    halfSide = 1000*halfSideInKm

    RADIUS_OF_EARTH  = 6371
    # Radius of the parallel at given latitude
    pradius = radius*math.cos(lat)

    latMin = lat - halfSide/radius
    latMax = lat + halfSide/radius
    lonMin = lon - halfSide/pradius
    lonMax = lon + halfSide/pradius
    rad2deg = math.degrees
    return (rad2deg(latMin), rad2deg(lonMin), rad2deg(latMax), rad2deg(lonMax))

But I cant understand how this is working, in particular this line makes no sense to me halfSide = 1000*halfSideInKm

like image 883
agiliq Avatar asked Oct 30 '09 10:10

agiliq


People also ask

How do you calculate bounding box?

The area of the box is the width times height. Here, 29 times 50, or 1450. The perimeter of the box is twice the width plus height. Here, that is 2(29+50), or 158.

How do you find the latitude and longitude of a bounding box?

It is very simple just go to panoramio website and then open World Map from panoramio website. Then go to specified location whichs latitude and longitude required. Then you found latitude and longitude in address bar for example in this address.

How do you find the bounding box of a city?

You can find the bounding box of those cities at www.mapdevelopers.com/geocode_bounding_box.php. Entering a city will display a box on a map and give the latitude and longitude of each side. Hopefully that will give you the information you need.

How do you calculate distance using latitude and longitude?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.


1 Answers

This code didn't quite work, it jumps between KM and M.

Fixed code, made names more PEP8 style, and added a simple box object:

class BoundingBox(object):
    def __init__(self, *args, **kwargs):
        self.lat_min = None
        self.lon_min = None
        self.lat_max = None
        self.lon_max = None


def get_bounding_box(latitude_in_degrees, longitude_in_degrees, half_side_in_miles):
    assert half_side_in_miles > 0
    assert latitude_in_degrees >= -90.0 and latitude_in_degrees  <= 90.0
    assert longitude_in_degrees >= -180.0 and longitude_in_degrees <= 180.0

    half_side_in_km = half_side_in_miles * 1.609344
    lat = math.radians(latitude_in_degrees)
    lon = math.radians(longitude_in_degrees)

    radius  = 6371
    # Radius of the parallel at given latitude
    parallel_radius = radius*math.cos(lat)

    lat_min = lat - half_side_in_km/radius
    lat_max = lat + half_side_in_km/radius
    lon_min = lon - half_side_in_km/parallel_radius
    lon_max = lon + half_side_in_km/parallel_radius
    rad2deg = math.degrees

    box = BoundingBox()
    box.lat_min = rad2deg(lat_min)
    box.lon_min = rad2deg(lon_min)
    box.lat_max = rad2deg(lat_max)
    box.lon_max = rad2deg(lon_max)

    return (box)
like image 50
Ted Avatar answered Nov 09 '22 20:11

Ted