Skip to content
Advertisement

Postgres aggregrate function for calculating vector average of wind speed (vector magnitude) and wind direction (vector direction)

I have a table with two columns wind_speed and wind_direction. I want to have a custom aggregrate function that would return average wind_speed and wind_direction. wind_speed and wind_direction in combination defines a vector where wind_speed is the magnitude of the vector and wind_direction is the direction of the vector. avg_wind_direction function should return average wind_speed as magnitude and wind_direction as direction of the average vector.

Related question: Custom PostgreSQL aggregate for circular average

Advertisement

Answer

So I have been able to create an aggregrate function that does the vector averaging. It makes the assumption that the vector is in polar co-ordinates and the angle is in degrees, as opposed to radian.

Test:

Test:

Result:

Removing all the rows:

Result:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement