{{ message }}

Instantly share code, notes, and snippets.

# amites/center_geo.py

Last active Apr 25, 2021
Center Geolocations
 from math import cos, sin, atan2, sqrt def center_geolocation(geolocations): """ Provide a relatively accurate center lat, lon returned as a list pair, given a list of list pairs. ex: in: geolocations = ((lat1,lon1), (lat2,lon2),) out: (center_lat, center_lon) """ x = 0 y = 0 z = 0 for lat, lon in geolocations: lat = float(lat) lon = float(lon) x += cos(lat) * cos(lon) y += cos(lat) * sin(lon) z += sin(lat) x = float(x / len(geolocations)) y = float(y / len(geolocations)) z = float(z / len(geolocations)) return (atan2(z, sqrt(x * x + y * y)), atan2(y, x))

### lhejazi commented Aug 20, 2013

 assuming you want your output center location to be in the form (lat, lon), shouldn't you switch the ordering of your return statements? i.e: return (atan2(z, sqrt(x * x + y * y)), atan2(y, x))

### nextcoder commented Apr 21, 2014

 A very useful function, thank you. Just a note, most Geolocation API deals latitude and longitude in degrees. So in most cases, you have to use radians() for the input and degrees() for the output (those two functions are in Python math library).

### AdamEyreWalker commented Jul 17, 2019

 lhejazi is correct; the function returns the results the wrong way round as long, lat - this is easily checked by putting a single set of lat, long coordinates through the function

### amites commented Jul 17, 2019

 been a few years since I looked at this, thank you for the reminder I've updated the gist

### Firzen7 commented Mar 2, 2020

 I don't think this works well. For example: center_geolocation([(56.012, -3.61), (56.016, -3.62)]) returns: (-0.5346732451559053, 2.6681793850777766) That makes no sense to me. I would expect something like (56.014, -3.615), or is my thinking wrong?

### AdamEyreWalker commented Mar 3, 2020

 I think you’ve programmed something incorrectly. The attached is my implementation of this algorithm which allows one to weight the calculation of the average long and lat by an area; this returns the expected result. Adam On 2 Mar 2020, at 14:32, Firzen7 > wrote: I don't think this works well. For example: center_geolocation([(56.012, -3.61), (56.016, -3.62)]) returns: (-0.5346732451559053, 2.6681793850777766) That makes no sense to me. I would expect something like (56.014, -3.615), or is my thinking wrong? — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

### Firzen7 commented Mar 3, 2020

 @AdamEyreWalker I am not sure, because I haven't programmed anything :D Steps I did: Select contents of center_geo.py Go to https://repl.it/languages/python3 Paste source code in the editor Click run Input center_geolocation([(56.012, -3.61), (56.016, -3.62)]) in the console Press ENTER As I said, I didn't change anything. :-)

### AdamEyreWalker commented Mar 3, 2020

 I’m not sure whats happening then, because it works for me. It just might be a difference between Python2 and Python3 but I doubt it. Sorry I can’t be of more help. Adam On 3 Mar 2020, at 12:36, Firzen7 > wrote: @AdamEyreWalker I am not sure, because I haven't programmed anything :D Steps I did: 1. Select contents of center_geo.py 2. Go to https://repl.it/languages/python3 3. Paste source code in the editor 4. Click run 5. Input center_geolocation([(56.012, -3.61), (56.016, -3.62)]) in the console 6. Press ENTER As I said, I didn't change anything. :-) — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

### JamesGDiaz commented May 9, 2020

 @AdamEyreWalker @Firzen7 You have to convert to radians first, this funcion assumes input is in radians: lat_rad = latPi/180 lon_rad = lonPi/180 just convert it back to degrees at the end