Last active
October 15, 2021 23:14
-
-
Save amites/3718961 to your computer and use it in GitHub Desktop.
Center Geolocations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
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 <[email protected]<mailto:[email protected]>> 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<https://gist.github.com/3718961?email_source=notifications&email_token=AMUNGVRKHEJDUSI6IY2CBULRFO7O5A5CNFSM4IEQKRHKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGDCTC#gistcomment-3196209>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AMUNGVVXTWDLITBCNPI43Z3RFO7O5ANCNFSM4IEQKRHA>.
@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. :-)
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 <[email protected]<mailto:[email protected]>> wrote:
@AdamEyreWalker<https://github.com/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<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<https://gist.github.com/3718961?email_source=notifications&email_token=AMUNGVVRQC2SCBCWPTUR7P3RFT2UPA5CNFSM4IEQKRHKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGDFZ4#gistcomment-3197854>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AMUNGVUOLILEHRTLB4VIXYDRFT2UPANCNFSM4IEQKRHA>.
@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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?