Bearing between two points

I use a geophysical data package that does an excellent job, however some of the results that I get are inconsistent or have a relatively large offset, I suspect the problem is with my carrier calculation:

def gb(x,y,center_x,center_y): dx=x-center_x dy=y-center_y if ((dy>=0)and((dx>0)or(dx<0))): return math.degrees(math.atan2(dy,dx)) elif (dy<=0)and((dx>0)or (dx<0)): return (math.degrees(math.atan2(dy,dx))+360) else: return (math.degrees(math.atan2(dy,dx))+360)%360 

I need to calculate the bearing, st center_x and center_y are the rod. I subsequently use geophysics to reverse engineer the gps coordinates:

 latlon = VincentyDistance(miles=dist).destination(Point(lat1, lon1), bearing) 

Can someone tell me what I can do wrong?

+4
source share
2 answers

Can someone tell me what I can do wrong?

  • Do not show an example of your "dissenting or come with a relatively large shift" results and expected results; therefore, defendants must rely on conjecture.

  • Without specifying in what units your input is measured (x, y, etc.), and how you get the dist used in calculating destination . I assume (when calculating bearing2 below) that positive x is east in miles, and positive y is north in miles. This would help a lot if you edited your question to fix (1) and (2).

  • A coding style that doesn't really make people want to read it ... check out this .

  • In school trigonometry, angles are measured counterclockwise from the X axis (east). In navigation, bearings are measured clockwise from the Y axis (north). See code below. For the example of the bearing used, follow this link , scroll down to the item "End point with a given distance and bearing from the starting point", note that the example says about bearings about 96 or 97 degrees, then click "see map" and you will notice that heading slightly south of the east (east is 90 degrees).

Code:

 from math import degrees, atan2 def gb(x, y, center_x, center_y): angle = degrees(atan2(y - center_y, x - center_x)) bearing1 = (angle + 360) % 360 bearing2 = (90 - angle) % 360 print "gb: x=%2d y=%2d angle=%6.1f bearing1=%5.1f bearing2=%5.1f" % (x, y, angle, bearing1, bearing2) for pt in ((0, 1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1, 0),(-1,1)): gb(pt[0], pt[1], 0, 0) 

Output:

 gb: x= 0 y= 1 angle= 90.0 bearing1= 90.0 bearing2= 0.0 gb: x= 1 y= 1 angle= 45.0 bearing1= 45.0 bearing2= 45.0 gb: x= 1 y= 0 angle= 0.0 bearing1= 0.0 bearing2= 90.0 gb: x= 1 y=-1 angle= -45.0 bearing1=315.0 bearing2=135.0 gb: x= 0 y=-1 angle= -90.0 bearing1=270.0 bearing2=180.0 gb: x=-1 y=-1 angle=-135.0 bearing1=225.0 bearing2=225.0 gb: x=-1 y= 0 angle= 180.0 bearing1=180.0 bearing2=270.0 gb: x=-1 y= 1 angle= 135.0 bearing1=135.0 bearing2=315.0 
+7
source

I'm not quite sure what you are trying to do in your code, but I see some oddities that may need to be cleared.

  • You test dy<=0 after testing for dy>=0 in a conditional clause. What should your code do if dy==0 and dx==0 .
  • Your test ((dy>=0)and((dx>0)or(dx<0))) equivalent to (dy> = 0 and dx! = 0), is that what you intended?
  • Basically you do the same in all your conventions. Can return math.degrees(math.atan2(dy,dx))+360)%360 work in every scenario? In this case, you will not need to use your if statements.
+2
source

Source: https://habr.com/ru/post/1340533/


All Articles