Python code to calculate angle between three points (lat long coordinates)

2024/10/11 23:20:21

Can anybody suggest how to calculate angle between three points (lat long coordinates)

A : (12.92473, 77.6183)
B : (12.92512, 77.61923)
C : (12.92541, 77.61985)
Answer

I see two main ways to solve your problem, assuming you want angle ABC (B is the vertex of the angle). Since your three points are close to each other (less than 0.0007° latitude and 0.002° longitude apart), we can approximate the earth as a plane and use two-dimensional vector calculations. A degree of longitude and of latitude are not the same distance when we are away from the equator, but we can adjust for that. Another solution is to treat your points as in three-dimensional space and use three-dimensional vector calculations. Here we just need to convert the given spherical coordinates to 3D Cartesian coordinates.

Here is my code for your problem. I use the numpy module here for convenience, but this can be done pretty easily without it. This code is fairly wordy so you can see better just what is being done.

import numpy as np
import mathdef latlong_to_3d(latr, lonr):"""Convert a point given latitude and longitude in radians to3-dimensional space, assuming a sphere radius of one."""return np.array((math.cos(latr) * math.cos(lonr),math.cos(latr) * math.sin(lonr),math.sin(latr)))def angle_between_vectors_degrees(u, v):"""Return the angle between two vectors in any dimension space,in degrees."""return np.degrees(math.acos(np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))))# The points in tuple latitude/longitude degrees space
A = (12.92473, 77.6183)
B = (12.92512, 77.61923)
C = (12.92541, 77.61985)# Convert the points to numpy latitude/longitude radians space
a = np.radians(np.array(A))
b = np.radians(np.array(B))
c = np.radians(np.array(C))# Vectors in latitude/longitude space
avec = a - b
cvec = c - b# Adjust vectors for changed longitude scale at given latitude into 2D space
lat = b[0]
avec[1] *= math.cos(lat)
cvec[1] *= math.cos(lat)# Find the angle between the vectors in 2D space
angle2deg = angle_between_vectors_degrees(avec, cvec)# The points in 3D space
a3 = latlong_to_3d(*a)
b3 = latlong_to_3d(*b)
c3 = latlong_to_3d(*c)# Vectors in 3D space
a3vec = a3 - b3
c3vec = c3 - b3# Find the angle between the vectors in 2D space
angle3deg = angle_between_vectors_degrees(a3vec, c3vec)# Print the results
print('\nThe angle ABC in 2D space in degrees:', angle2deg)
print('\nThe angle ABC in 3D space in degrees:', angle3deg)

This gives the results

The angle ABC in 2D space in degrees: 177.64369006The angle ABC in 3D space in degrees: 177.643487338

Note that the results are very close (off by about one five-thousandth of a degree), as expected for three points so close together.

https://en.xdnf.cn/q/69718.html

Related Q&A

z3: solve the Eight Queens puzzle

Im using Z3 to solve the Eight Queens puzzle. I know that each queen can be represented by a single integer in this problem. But, when I represent a queen by two integers as following:from z3 import *X…

Image skewness kurtosis in python

Is there a python package that will provide me a way to clacluate Skewness and Kurtosis of an image?. Any example will be great.Thanks a lot.

Python: Getting all the items out of a `threading.local`

I have a threading.local object. When debugging, I want to get all the objects it contains for all threads, while I am only on one of those threads. How can I do that?

Why tuple is not mutable in Python? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why th…

Display Django form fields on the same line

I would like to display two form fields on the same line and not each one after the other one.For the moment, I get:Choice a theme :. Datasystems. CamerounBut I would like to display this form like:Cho…

How can I use the index array in tensorflow?

If given a matrix a with shape (5,3) and index array b with shape (5,), we can easily get the corresponding vector c through,c = a[np.arange(5), b]However, I cannot do the same thing with tensorflow,a …

cython with array of pointers

I have a list of numpy.ndarrays (with different length) in python and need to have very fast access to those in python. I think an array of pointers would do the trick. I tried:float_type_t* list_of_ar…

How to skip blank lines with read_fwf in pandas?

I use pandas.read_fwf() function in Python pandas 0.19.2 to read a file fwf.txt that has the following content:# Column1 Column2123 abc456 def# #My code is the following:import pandas as pd fil…

Pandas rolling std yields inconsistent results and differs from values.std

Using pandas v1.0.1 and numpy 1.18.1, I want to calculate the rolling mean and std with different window sizes on a time series. In the data I am working with, the values can be constant for some subse…

How to change attributes of a networkx / matplotlib graph drawing?

NetworkX includes functions for drawing a graph using matplotlib. This is an example using the great IPython Notebook (started with ipython3 notebook --pylab inline):Nice, for a start. But how can I in…