Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the center of the bounding box? [closed]

Im trying to calculate the center of the bounding box with these given points

(50.607041876988994, -1.3187316344406208, 52.40735812301099, 1.5737316344406207)

This is the code i done in python

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(y, x), atan2(z, sqrt(x * x + y * y)))

However i keep on getting this error

line 64, in center_geolocation
    for lat, lon in geolocations:
TypeError: 'float' object is not iterable

Can someone explain what im doing wrong or can someone correct the error i may have done,

thank you :)

like image 231
Mr_Shoryuken Avatar asked Oct 15 '25 16:10

Mr_Shoryuken


1 Answers

from math import *

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(y, x), atan2(z, sqrt(x * x + y * y)))


center_geolocation( 
 ((50.607041876988994, -1.3187316344406208),   
  (52.40735812301099, 1.5737316344406207)))

There is not float error in the example you have given .... I do not like the output - but that was not the question.

like image 57
Tim Seed Avatar answered Oct 17 '25 07:10

Tim Seed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!