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

后端 未结 2 1779
-上瘾入骨i
-上瘾入骨i 2020-12-17 00:58

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

相关标签:
2条回答
  • 2020-12-17 01:23

    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)
    
    0 讨论(0)
  • 2020-12-17 01:33

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

    0 讨论(0)
提交回复
热议问题