Skip to content

Add support for great circle #1002

Open

Activity

  1. zugaldia commented on Apr 15, 2019

    @zugaldia
    Member

    @tobrun Could you explain why do we need to support this method and, if possible, include a sense or urgency for this feature? Thanks!

  2. tobrun commented on Apr 15, 2019

    @tobrun
    MemberAuthor

    Use-case in ticket linking back to this ticket, no urgency. I can imagine other users wanting this functionality at some point as well.

  3. added
    Archived
    on Oct 12, 2019
  4. stale commented on Oct 12, 2019

    @stale

    This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

  5. removed
    Archived
    on Oct 12, 2019
  6. added
    Archived
    on Apr 9, 2020
  7. stale commented on Apr 9, 2020

    @stale

    This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

  8. removed
    Archived
    on Apr 9, 2020
  9. konradbr commented on Nov 9, 2022

    @konradbr

    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.

  10. Elyahu41 commented on Jul 3, 2026

    @Elyahu41

    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);
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions