Solved: what is wrong with this LatLong->Distance code? A Trigonometry "feature".
|
10-12-2016, 06:20 PM
(This post was last modified: 10-17-2016 10:21 PM by StephenG1CMZ.)
Post: #1
|
|||
|
|||
Solved: what is wrong with this LatLong->Distance code? A Trigonometry "feature".
Don't be put off by the code - it includes three different implementations all exhibiting the same bug, so you only need to check one...Each one is just a dozen or so lines of trigonometry.
I have coded up three routines for converting a pair of LatLong points to a distance. HAVERS implements a Haversine spherical Earth calculation VINCENTYS implements a simplification of Vincenty's formula for a spherical Earth. These two return practically identical results (trivial rounding in micrometres). Z-VINCENTY is the normal ellipsoidal formula, perhaps a few km away from the other results. All 3 exhibit the same bug: Reporting a distance of 1km for an expected distance of 54 km, for example. (The expected result here is from Geoscience Australia). Since all three implementations are wrong, I assume their is some common error. I have tried changing my atan2 algorithm and YY XX parameter sequence. I have tried calculating the longitude difference as abs(LON2-LON1) whereas other sources just subtract. I am aware that VINCENTY has a few superfluous brackets compared to other online sources, but that would not cause all 3 to fail. I have stared at the code long enough that I need a break before having another look... Can anyone else see something I have misunderstood in all 3 implementations? Or recommend a worked example with intermediate values that I can check my code against? Or is it simply that this application demands more precision than the Prime has? Code:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-12-2016, 08:14 PM
Post: #2
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code?
It's rather difficult for me to decipher your code entirely. Therefore I've just taken the Haversine part and adjusted it a little bit. That is, I've done the HAV-function and the D2R in place. Further I replaced RR directly with the value of 6378.137 Km, I think that is correct because Haversine refers to the sphere and not to the ellipsoid.
Code: EXPORT Haversine(LAT1,LON1,LAT2,LON2) This works for me with known values. But I must admit, for rather long distances. If it works for you also then you should investigate the method of achieving "RR" with your "ESS{}". There might be the glitch. HTH Günter |
|||
10-12-2016, 09:46 PM
(This post was last modified: 10-12-2016 11:05 PM by StephenG1CMZ.)
Post: #3
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code?
Thank you for taking the time to look at this.
When I first saw your code I was thinking the lower-case Sin was exact and avoiding a rounding error - but it doesn't fix this example, which is still 1 km rather than 54 km. For spherical calculations I usually use an average of polar and equatorial radius, (actually. (2a+b)/3), or repeat the calculations for both - but that detail isn't significant. Example data: http://www.ga.gov.au/geodesy/datums/vinc...nverse.jsp Update: a missing atan2 call is added - the reported distance is still 1 km. Actually, that is probably just equivalent to the previous line. Code:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-13-2016, 10:12 AM
(This post was last modified: 10-13-2016 10:25 AM by Guenter Schink.)
Post: #4
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code?
(10-12-2016 09:46 PM)StephenG1CMZ Wrote: Thank you for taking the time to look at this.Sorry the lower case "sin" was not intentionally. But now I think we're coming closer. Don't know what really happens (a bug?). I added commands to print DLON and DLAT. And much to my surprise they showed up in DMS format. Consequently I removed the transformation to radians et voilá the result comes pretty close to what's expected. Code: EXPORT Haversine(LAT1,LON1,LAT2,LON2) It will happen on the command line too. Just enter "12°30' * PI / 180" the expression will stay in DMS mode. HTH, Günter ** That's obviously the reason why I didn't encounter the problem, as I entered the data in degrees format. Günter |
|||
10-13-2016, 04:52 PM
(This post was last modified: 10-13-2016 05:04 PM by StephenG1CMZ.)
Post: #5
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code?
Thank you so much for that insight Guenter - I have only briefly read your answer and need to re-read it more thoroughly.
But if I understand you, these two SIN's give different answers, despite - one would have thought - being the same calculation (my home setting is Radians default). Code:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-13-2016, 05:31 PM
(This post was last modified: 10-13-2016 05:33 PM by Tim Wessman.)
Post: #6
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code? Trigonometry bug?
If you are specifying an input in DMS then your value being input is degrees - essentially you have tagged it with a unit. What would be the expected behavior for these cases?
SIN(1.5) SIN(1.5_grad) SIN(1.5_rad) SIN(1°30') Seems to me the first would be dependent on your setting and all the rest are essentially units that can be directly handled no matter your system setting to give the correct result. Since DMS is an inherent part of the number, you don't have to do conversions between steps to calculate using DMS. Thus something like 5°15' * 3 is a perfectly valid calculation and returns a DMS result. Anyway, I think you have your answer about the bug. If you want to be completely independent of your setting, run HMS-> (doesn't matter if you are are in HMS or not, just sets the flag on the real number input) then D->R. I'm wondering now though if HMS-> and ->HMS should also handle angular unit objects directly so something like ->HMS(1.5_rad) would return 85°56′37.20937″ ... thoughts anyone? TW Although I work for HP, the views and opinions I post here are my own. |
|||
10-13-2016, 08:58 PM
Post: #7
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code? Trigonometry bug?
I guess where I was wrong in thinking it should "just work", is I wasn't thinking of the DMS format as including a unit. After all, it was degrees, not underscore degrees to be consistent with other units - and the same syntax could also have been hours.
I was thinking of it as more of just a different presentation of a decimal number, and assuming the calculation would be unchanged. Like 1.5 = 1,5.. Like decimal 5 = hex 5. Seeing your other examples with units, I see it's not as simple as keeping the presentation and calculation separate. As it stands, this issue seems likely to catch people out unless they are aware of it and code around it. But I am not sure what could or should be changed to make things clearer. Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-14-2016, 09:53 AM
Post: #8
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code? Trigonometry bug?
(10-13-2016 05:31 PM)Tim Wessman Wrote: . Contrary to my previous statement, with a question mark though, I would no longer consider this to be a bug. In my opinion it is a feature, perhaps needs to be stated clearer somewhere! But where? And a, yet to be implemented, D->R command should work regardless of DMS or HR format, avoiding these ambiguities. And I think your proposal for ->HMS () would be valuable enhancement. Günter Don't forget to implement newRPL |
|||
10-17-2016, 10:17 PM
Post: #9
|
|||
|
|||
RE: Can you see what is wrong with this LatLong->Distance code? A Trigonometry &quo...
I have now uploaded corrected spherical calculations to my Geodesy program in the software library. The ellipsoidal Vincenty implementation needs more work, however - it's values are currently less accurate than the spherical calculations.
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-23-2016, 06:41 PM
Post: #10
|
|||
|
|||
RE: Solved: what is wrong with this LatLong->Distance code? A Trigonometry "fe...
Version 0.3 of my Geodesy program now implements Lat Long to distance calculations, including the Vincenty formula. If you have V0.1 or V0.2 please upgrade.
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)