I have just written this code that takes an initial coordinate and returns a new vector depending on a user-supplied range and bearing.
The calculation of the new position is done using Complex Numbers. I am sure there are easier ways to do this, but I wanted to play around with complex numbers. Any thoughts on these would be great.
The calculator is in a file called GenGeo, as this is where I keep lots of little modules like this that may be called again in other programs.
I have used **kwargs
for a_e
and a_n
as these may not be required by the user. However, I have never really used *args
and **kwargs
before, so presume that I may have messed something up here.
This is the calculator module in GenGeo:
class CmO:
"""
This class will contain specific code that is required to run a C-O calculation
This includes range and bearing calc
"""
def __init__(self, *args, **kwargs):
"""
:param a_e: initial easting
:param a_n: initial northing
"""
self.a_e = kwargs.get('a_e', None)
self.a_n = kwargs.get('a_n', None)
def complex_randb(self, r, b):
"""
An equation that using imaginary numbers to calculate the coordinates of a new
point from a range and bearing of an old point
:param r: range from original coordinate to new coordinate
:param b: bearing from original coordinate to new coordinate
"""
# -b is required as geodetic bearings are opposite to mathematical bearings
t = complex(cos(radians(-b)), sin(radians(-b))) * complex(0, r)
delta_easting = t.real
delta_northing = t.imag
if self.a_e and self.a_n is not False:
new_easting = self.a_e + delta_easting
new_northing = self.a_n + delta_northing
return new_easting, new_northing
else:
return delta_easting, delta_northing
and this is the program that I have used to call it:
from GenGeo import CmO
initial_E = 100
initial_N = 100
input_range = 3
input_bearing = 310
a = CmO(a_e=initial_E, a_n=initial_N)
nE, nN = a.complex_randb(input_range, input_bearing)
print(f"The delta Easting and Northing are {round(nE, 3)}mE and {round(nN, 3)}mN")
Next project is writing something to calculate the range and bearing between two points
Any thoughts or improvements would be greatly appreciated!