8

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

Community
  • 1
  • 1
agiliq
  • 7,518
  • 14
  • 54
  • 74
  • 1
    The posted code does not work for great distances or near the poles. – Nikolai Ruhe Oct 30 '09 at 10:12
  • Nicolai: Only want it to work over short distances, so can assume a flat earth etc. Also for longitude comparison we are doing a `pradius` division, so doesnt it take into account the change near the poles? – agiliq Oct 30 '09 at 10:17
  • halfSideInKm is the distance from the point or not? Can you explain what to take here? – Seenu S Jun 06 '16 at 11:22

2 Answers2

11

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)
Ted
  • 12,122
  • 4
  • 31
  • 39
  • 1
    This worked perfectly. Thank you. One question: what is half_side_in_miles? How do I calculate that? I've searched the intrewebs and have found nothing. – tmthyjames May 29 '15 at 17:05
  • BTW, the 2nd `assert` is incorrect. Latitude ranges from -90° to 90°, not -180° to 180°. – PM 2Ring Mar 25 '16 at 08:01
2

That line is converting the bounding box units from kilometres to metres.

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • 1
    Heh.. obvious now that you mention it, but then `latMin = lat - halfSide/radius` seems wrong. 1Km != 1000/6371 latitude distance as 1deg change in latitude is ~110 km change in distance. – agiliq Oct 30 '09 at 10:15