Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the midpoint of several geolocations in python

Is there's a library or a way to calculate the center point for several geolocations points? This is my list of geolocations based in New York and want to find the approximate midpoint geolocation

L = [
     (-74.2813611,40.8752222),
     (-73.4134167,40.7287778),
     (-74.3145014,40.9475244),
     (-74.2445833,40.6174444),
     (-74.4148889,40.7993333),
     (-73.7789256,40.6397511)
    ]
like image 398
mongotop Avatar asked Jun 17 '16 15:06

mongotop


People also ask

How to do midpoint formula in Python?

Vectorizing the midpoint rule Such coordinates can be calculated by x = linspace(a+h/2, b-h/2, n) . Given that the Python implementation f of the mathematical function f works with an array argument, which is very often the case in Python, f(x) will produce all the function values in an array.

How do you find the distance between two Geolocations?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

How do you find the center of a line in Python?

Python String center() Method The center() method will center align the string, using a specified character (space is default) as the fill character.

How do you find the distance between two latitude longitude points in Python?

import mpu # Point one lat1 = 52.2296756 lon1 = 21.0122287 # Point two lat2 = 52.406374 lon2 = 16.9251681 # What you were looking for dist = mpu. haversine_distance((lat1, lon1), (lat2, lon2)) print(dist) # gives 278.45817507541943.


2 Answers

Based on: https://gist.github.com/tlhunter/0ea604b77775b3e7d7d25ea0f70a23eb

Assume you have a pandas DataFrame with latitude and longitude columns, the next code will return a dictionary with the mean coordinates.

import math

x = 0.0
y = 0.0
z = 0.0

for i, coord in coords_df.iterrows():
    latitude = math.radians(coord.latitude)
    longitude = math.radians(coord.longitude)

    x += math.cos(latitude) * math.cos(longitude)
    y += math.cos(latitude) * math.sin(longitude)
    z += math.sin(latitude)

total = len(coords_df)

x = x / total
y = y / total
z = z / total

central_longitude = math.atan2(y, x)
central_square_root = math.sqrt(x * x + y * y)
central_latitude = math.atan2(z, central_square_root)

mean_location = {
    'latitude': math.degrees(central_latitude),
    'longitude': math.degrees(central_longitude)
    }
like image 88
Elhanan Mishraky Avatar answered Oct 11 '22 23:10

Elhanan Mishraky


Considering that you are using signed degrees format (more), simple averaging of latitude and longitudes would create problems for even small regions near to antimeridian (i.e. + or - 180-degree longitude) due to discontinuity of longitude value at this line (sudden jump between -180 to 180).

Consider two locations whose longitudes are -179 and 179, their mean would be 0, which is wrong.

like image 29
Rahul Shaw Avatar answered Oct 12 '22 01:10

Rahul Shaw