views:

316

answers:

1

I need a MySQL query (or function) to calculate the geographic midpoint of an arbitrary number of latitude/longitude coordinates. I want to use Method C (Average latitude/longitude) as described on this page http://www.geomidpoint.com/calculation.html but can't figure out how to convert this into a SQL query. I'm looking for something of the form:

select LATITUDE_AVG_FORMULA(points.latitude),LONGITUDE_AVG_FORMULA(points.longitude) from points;

Where each point in the points table has as associated latitude and longitude in decimal format. I'm hoping someone either already has a MySQL query (or function) they are using for this or have a lot more experience with geospacial calculations than I have. Thanks in advance for your help!

A: 

Method C appears to be basically a weighted average. Your question is close to MySQL syntax. Given that these are decimal format.

If you have weights:

SELECT
    SUM( longitude * weight ) / SUM( weight ) AS Avg_Long,
    SUM( latitude * weight ) / SUM( weight ) AS Avg_Lat
FROM
    points

Or unweighted:

SELECT
    AVG( longitude ) AS Avg_Long,
    AVG( latitude ) AS Avg_Lat
FROM
    points

Your reference alludes to baseline adjustments, but I'm not sure that's needed.

HTH

martin clayton
Russell C.