You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case anyone was looking for a solution to this issue. Gemini just spit out this class that seems to work, although it also seems to create a large amount of points:
import org.maplibre.geojson.LineString;
import org.maplibre.geojson.Point;
import java.util.ArrayList;
import java.util.List;
public class GreatCircleUtils {
/**
* Generates a Great Circle LineString between two points.
*
* @param start The starting Point.
* @param end The ending Point.
* @param numPoints The number of intermediate points to generate (higher = smoother curve).
* @return A LineString representing the Great Circle path.
*/
public static LineString createGreatCircle(Point start, Point end, int numPoints) {
List<Point> points = new ArrayList<>();
// Convert coordinates to radians
double lat1 = Math.toRadians(start.latitude());
double lon1 = Math.toRadians(start.longitude());
double lat2 = Math.toRadians(end.latitude());
double lon2 = Math.toRadians(end.longitude());
// Calculate the angular distance between the two points (Haversine formula)
double d = 2 * Math.asin(Math.sqrt(
Math.pow(Math.sin((lat1 - lat2) / 2), 2) +
Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin((lon1 - lon2) / 2), 2)
));
// Handle edge case where points are identical
if (d == 0) {
points.add(start);
points.add(end);
return LineString.fromLngLats(points);
}
// Generate the intermediate points
for (int i = 0; i <= numPoints; i++) {
double f = (double) i / numPoints;
double a = Math.sin((1 - f) * d) / Math.sin(d);
double b = Math.sin(f * d) / Math.sin(d);
double x = a * Math.cos(lat1) * Math.cos(lon1) + b * Math.cos(lat2) * Math.cos(lon2);
double y = a * Math.cos(lat1) * Math.sin(lon1) + b * Math.cos(lat2) * Math.sin(lon2);
double z = a * Math.sin(lat1) + b * Math.sin(lat2);
double lat = Math.atan2(z, Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)));
double lon = Math.atan2(y, x);
points.add(Point.fromLngLat(Math.toDegrees(lon), Math.toDegrees(lat)));
}
return LineString.fromLngLats(points);
}
}
To use it:
Point start = Point.fromLngLat(-74.0060, 40.7128); // New York
Point end = Point.fromLngLat(-0.1276, 51.5072); // London
// Generate a curve with 100 intermediate points for smoothness
LineString greatCircleLine = GreatCircleUtils.createGreatCircle(start, end, 100);
// Assuming you have a GeoJsonSource setup
GeoJsonSource source = style.getSourceAs("your-line-source-id");
if (source != null) {
source.setGeoJson(greatCircleLine);
}
Activity
zugaldia commentedon Apr 15, 2019
@tobrun Could you explain why do we need to support this method and, if possible, include a sense or urgency for this feature? Thanks!
tobrun commentedon Apr 15, 2019
Use-case in ticket linking back to this ticket, no urgency. I can imagine other users wanting this functionality at some point as well.
stale commentedon Oct 12, 2019
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.
stale commentedon Apr 9, 2020
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.
konradbr commentedon Nov 9, 2022
I would very much welcome this feature as well! It seems like such an essential feature: it really is just a straight line, properly projected.
Elyahu41 commentedon Jul 3, 2026
In case anyone was looking for a solution to this issue. Gemini just spit out this class that seems to work, although it also seems to create a large amount of points:
To use it: