KapoorVashisht / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

PathOverlay.addGreatCircle - bug if segment < 100km #528

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Build a PathOverlay using addGreatCircle, with a small segment (< 100km): 

PathOverlay geodesicLine = new PathOverlay(Color.GREEN, this);
geodesicLine.addGreatCircle(
  new GeoPoint(48.0, -1.5), 
  new GeoPoint(48.0, -1.0));
map.getOverlays().add(geodesicLine);

Expected result: a straight line between the 2 points. 

Result: 
1) Nothing displayed. 
2) The PathOverlay contains 1 point, located at (0, 0). Which is very bad. 

Root cause: 
In addGreatCircle(startPoint, endPoint), with this small segment, 
numberOfPoints is equal to 0. 

Then it calls addGreatCircle(startPoint, endPoint, numberOfPoints). 
But this method doesn't support numberOfPoints=0. 
At this line:
  final double f = 1.0 / numberOfPoints * i; 
=> division by 0 (surprisingly, not raising an exception). 

Solutions: 

1) Simple one:

if (numberOfPoints>0){
  addGreatCircle(startPoint, endPoint, numberOfPoints);
} else {
  addPoint(startPoint);
  addPoint(endPoint);
}

2) Or deeply revise addGreatCircle(startPoint, endPoint, numberOfPoints)... 

Original issue reported on code.google.com by mathieu....@gmail.com on 16 Feb 2014 at 9:30

GoogleCodeExporter commented 8 years ago
Well, in fact, Solution 2 (deep revision) is really simple: 

Just replace: 
for (int i = 0, j = numberOfPoints + 1; i < j; i++){
  final double f = 1.0 / numberOfPoints * i;

By: 
for (int i = 1; i < numberOfPoints; i++){
  final double f = 1.0 * i / (numberOfPoints+1);

Now, addGreatCircle adds exactly "numberOfPoints" intermediate points. 
And it doesn't add startPoint and endPoint - which is more flexible, for 
instance when we want to add successive great circle segments. 

I can send the updated method, including also minor improvements (using 
MathConstants.DEG2RAD). 

Original comment by mathieu....@gmail.com on 17 Feb 2014 at 4:57

GoogleCodeExporter commented 8 years ago
Solution is here: 
http://code.google.com/p/osmbonuspack/source/browse/trunk/OSMBonusPack/src/org/o
smdroid/bonuspack/overlays/Polyline.java

Original comment by mathieu....@gmail.com on 19 Mar 2014 at 11:56