Beispiel #1
0
    def find_closest(cls,
                     generic_asset_type_name: str,
                     sensor_name: str,
                     n: int = 1,
                     **kwargs) -> Union["Sensor", List["Sensor"], None]:
        """Returns the closest n sensors within a given asset type (as a list if n > 1).
        Parses latitude and longitude values stated in kwargs.

        Can be called with an object that has latitude and longitude properties, for example:

            sensor = Sensor.find_closest("weather_station", "wind speed", object=generic_asset)

        Can also be called with latitude and longitude parameters, for example:

            sensor = Sensor.find_closest("weather_station", "temperature", latitude=32, longitude=54)
            sensor = Sensor.find_closest("weather_station", "temperature", lat=32, lng=54)

        Finally, pass in an account_id parameter if you want to query an account other than your own. This only works for admins. Public assets are always queried.
        """

        latitude, longitude = parse_lat_lng(kwargs)
        account_id_filter = kwargs[
            "account_id"] if "account_id" in kwargs else None
        query = query_sensors_by_proximity(
            latitude=latitude,
            longitude=longitude,
            generic_asset_type_name=generic_asset_type_name,
            sensor_name=sensor_name,
            account_id=account_id_filter,
        )
        if n == 1:
            return query.first()
        else:
            return query.limit(n).all()
Beispiel #2
0
def find_closest_weather_sensor(
        sensor_type: str,
        n: int = 1,
        **kwargs) -> Union[WeatherSensor, List[WeatherSensor], None]:
    """Returns the closest n weather sensors of a given type (as a list if n > 1).
    Parses latitude and longitude values stated in kwargs.

    Can be called with an object that has latitude and longitude properties, for example:

        sensor = find_closest_weather_sensor("wind_speed", object=asset)

    Can also be called with latitude and longitude parameters, for example:

        sensor = find_closest_weather_sensor("temperature", latitude=32, longitude=54)
        sensor = find_closest_weather_sensor("temperature", lat=32, lng=54)

    """

    latitude, longitude = parse_lat_lng(kwargs)
    sensors = WeatherSensor.query.filter(
        WeatherSensor.weather_sensor_type_name == sensor_type).order_by(
            WeatherSensor.great_circle_distance(lat=latitude,
                                                lng=longitude).asc())
    if n == 1:
        return sensors.first()
    else:
        return sensors.limit(n).all()
Beispiel #3
0
    def great_circle_distance(self, **kwargs):
        """Query great circle distance (in km).

        Can be called with an object that has latitude and longitude properties, for example:

            great_circle_distance(object=asset)

        Can also be called with latitude and longitude parameters, for example:

            great_circle_distance(latitude=32, longitude=54)
            great_circle_distance(lat=32, lng=54)

        """
        other_location = geo_utils.parse_lat_lng(kwargs)
        if None in other_location:
            return None
        return geo_utils.earth_distance(self.location, other_location)
Beispiel #4
0
    def great_circle_distance(self, **kwargs):
        """Query great circle distance (unclear if in km or in miles).

        Can be called with an object that has latitude and longitude properties, for example:

            great_circle_distance(object=asset)

        Can also be called with latitude and longitude parameters, for example:

            great_circle_distance(latitude=32, longitude=54)
            great_circle_distance(lat=32, lng=54)

        """
        other_latitude, other_longitude = parse_lat_lng(kwargs)
        if other_latitude is None or other_longitude is None:
            return None
        return func.earth_distance(
            func.ll_to_earth(self.latitude, self.longitude),
            func.ll_to_earth(other_latitude, other_longitude),
        )
Beispiel #5
0
    def great_circle_distance(self, **kwargs):
        """Query great circle distance (in km).

        Can be called with an object that has latitude and longitude properties, for example:

            great_circle_distance(object=asset)

        Can also be called with latitude and longitude parameters, for example:

            great_circle_distance(latitude=32, longitude=54)
            great_circle_distance(lat=32, lng=54)

        """
        r = 6371  # Radius of Earth in kilometers
        other_latitude, other_longitude = parse_lat_lng(kwargs)
        if other_latitude is None or other_longitude is None:
            return None
        other_cos_rad_lat = math.cos(math.radians(other_latitude))
        other_sin_rad_lat = math.sin(math.radians(other_latitude))
        other_rad_lng = math.radians(other_longitude)
        return (math.acos(self.cos_rad_lat * other_cos_rad_lat *
                          math.cos(self.rad_lng - other_rad_lng) +
                          self.sin_rad_lat * other_sin_rad_lat) * r)