Pregunta

Dada una latitud, longitud y distancia, quiero encontrar un cuadro delimitador donde las distancias sean menores que la distancia dada.

Estas preguntas se hicieron aquí: ¿Cómo calcular el cuadro delimitador para una ubicación de lat / lng dada?

No quiero que esto sea particularmente preciso, así que lo modifiqué y simplifiqué para

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))

Pero no puedo entender cómo funciona esto, en particular esta línea no tiene sentido para mí halfSide = 1000 * halfSideInKm

¿Fue útil?

Solución

Esa línea está convirtiendo las unidades del cuadro delimitador de kilómetros a metros.

Otros consejos

Este código no funcionó, salta entre KM y M.

Se corrigió el código, hizo que los nombres tuvieran más estilo PEP8 y se agregó un objeto de cuadro simple:

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)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top