I am stuck with a confusing problem. Here's a little background:
I'm working on qgis/python with coordinate points in Lambert93: one central point (my dict key) and several other points gravitating around it. To simplify, the code I've put down just one example:
import numpy as np
import math
dict = {(355385,6.68906e+06): [(355277,6.68901e+06), (355501,6.68912e+06), (355364,6.6891e+06), (355277,6.68901e+06)]}
for key, values in dict.iteritems():
anglist =[]
print key
i=0
j=1
for sides in values[:-1]:
A = np.array(dict[key][i])
B = np.array(key)
C = np.array(dict[key][j])
BA = A - B
BC = C - B
cosine_angle = np.vdot(BA, BC) / (np.linalg.norm(BA) * np.linalg.norm(BC))
angle = (np.degrees(np.arccos(cosine_angle)))
i+=1
j+=1
anglist.append(angle)
s = sum(anglist)
dict[key]= [values, anglist, s]
print dict
results are :
{(355385, 6689060.0): [[(355277, 6689010.0), (355501, 6689120.0), (355364, 6689100.0), (355277, 6689010.0)], [177.4925133253854, 90.349597027985112, 87.142916297400205], 354.98502665077069]}
As you can see, sum = 354. I have a large set of data and sometimes I get the correct 360, but for the most part I don't. Yet in all logic, by turning around a single point and ending the calculation where it started, the only result i should get is 360.
I have tried a second way just to see if the cosine-angle
and angle
weren't the problem :
from math import sqrt
from math import acos
import numpy
def angle(a, b, c):
# Create vectors from points
ba = [ aa-bb for aa,bb in zip(a,b) ]
bc = [ cc-bb for cc,bb in zip(c,b) ]
# Normalize vector
nba = sqrt ( sum ( (x**2.0 for x in ba) ) )
ba = [ x/nba for x in ba ]
nbc = sqrt ( sum ( (x**2.0 for x in bc) ) )
bc = [ x/nbc for x in bc ]
# Calculate scalar from normalized vectors
scale = sum ( (aa*bb for aa,bb in zip(ba,bc)) )
# calculate the angle in radian
angle = numpy.degrees(acos(scale))
return angle
print angle((355277,6.68901e+06),(355385,6.68906e+06), (355501,6.68912e+06))
print angle((355501,6.68912e+06),(355385,6.68906e+06), (355364,6.6891e+06))
print angle((355364,6.6891e+06),(355385,6.68906e+06), (355277,6.68901e+06))
But the results are still:
177.492513325
90.349597028
87.1429162974
So I think we can cross the math out of the problem... So one possibility is a problem with how qgis (or python?) manages the coordinates. How can I go around this?
I should say, the codes are largely the same as here, here and here