0

I have 3 geographic coordinates

first_coordinate = [55.266346, 25.053268]
second_coordinate = [55.266472, 25.053311]
third_coordinate = [55.266370, 25.0532]

Then I convert them into EPSG:3997:

def convert(point):
  return Point(transformer.transform(point[0], point[1]))

first_point_in_meters = convert(first_coordinate)
second_point_in_meters = convert(second_coordinate)
third_point_in_meter = convert(third_coordinate)

Then tries to determine origin third coordinate using:

def calculate_third_point(Ax, Ay, Cx, Cy, b, c, A, alt):
  uACx = (Cx - Ax) / b
  uACy = (Cy - Ay) / b

  if alt:
      uABx = uACx * math.cos(A) - uACy * math.sin(A)
      uABy = uACx * math.sin(A) + uACy * math.cos(A)

      Bx = Ax + c * uABx
      By = Ay + c * uABy
  else:
      uABx = uACx * math.cos(A) + uACy * math.sin(A)
      uABy = - uACx * math.sin(A) + uACy * math.cos(A)

      Bx = Ax + c * uABx
      By = Ay + c * uABy

  return Point([Bx, By])

And converted this 2 coordinates back to EPSG:4326. But closest coordinate is 2 meters far from origin

one distance: 2.6033912103159342 m.
anther distance: 19.215538365617018 m.

What I'm doing wrong?

Full code:

import math
import numpy as np
from pyproj import Transformer

from shapely.geometry import Point

transformer_4326 = Transformer.from_crs(3997, 4326, always_xy=True)
transformer = Transformer.from_crs(4326, 3997, always_xy=True)

def calculate_third_point(Ax, Ay, Cx, Cy, b, c, A, alt):
  uACx = (Cx - Ax) / b
  uACy = (Cy - Ay) / b

  if alt:
      uABx = uACx * math.cos(A) - uACy * math.sin(A)
      uABy = uACx * math.sin(A) + uACy * math.cos(A)

      Bx = Ax + c * uABx
      By = Ay + c * uABy
  else:
      uABx = uACx * math.cos(A) + uACy * math.sin(A)
      uABy = - uACx * math.sin(A) + uACy * math.cos(A)

      Bx = Ax + c * uABx
      By = Ay + c * uABy

  return Point([Bx, By])

def find_angle(rone, rtwo, rthree):
  one = np.array(rone)
  two = np.array(rtwo)
  three = np.array(rthree)
  ba = one - two
  bc = three - two
  cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
  angle = np.arccos(cosine_angle)
  return np.degrees(angle)

def convert(point):
  return Point(transformer.transform(point[0], point[1]))

first_coordinate = [55.266346, 25.053268]
second_coordinate = [55.266472, 25.053311]
third_coordinate = [55.266370, 25.0532]

first_point_in_meters = convert(first_coordinate)
second_point_in_meters = convert(second_coordinate)
third_point_in_meter = convert(third_coordinate)

angle = find_angle(third_point_in_meter, second_point_in_meters, first_point_in_meters)
distance = first_point_in_meters.distance(second_point_in_meters)
distance2 = first_point_in_meters.distance(third_point_in_meter)

one = calculate_third_point(
  first_point_in_meters.x,
  first_point_in_meters.y,
  second_point_in_meters.x,
  second_point_in_meters.y,
  distance,
  distance2,
  angle,
  True)

another = calculate_third_point(
  first_point_in_meters.x,
  first_point_in_meters.y,
  second_point_in_meters.x,
  second_point_in_meters.y,
  distance,
  distance2,
  angle,
  False)

print("one distance: {0} m.".format(one.distance(third_point_in_meter)))
print("anther distance: {0} m.".format(another.distance(third_point_in_meter)))
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
native1989
  • 95
  • 3
  • 10
  • 1
    Probably because `angle` is in degrees but `math.cos()` and other trig functions expect radians? – Pranav Hosangadi Nov 12 '20 at 17:58
  • @PranavHosangadi Thank you for pointing out my issue. It is really degree/radians issue and also need to swap second and third argument in find_angle function, because it's finding wrong angle – native1989 Nov 13 '20 at 00:05

0 Answers0