Skip to main content
Glama

calculate_distance

Calculate great circle distance between two geographic coordinates for flight planning and aviation route analysis.

Instructions

Calculate great circle distance between two points.

Args: lat1: Latitude of first point in degrees lon1: Longitude of first point in degrees lat2: Latitude of second point in degrees lon2: Longitude of second point in degrees

Returns: JSON string with distance information

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
lat1Yes
lon1Yes
lat2Yes
lon2Yes

Implementation Reference

  • The handler function for the 'calculate_distance' tool. It takes two pairs of latitude/longitude coordinates and computes the great circle distance, bearings, and returns a formatted JSON string using the great_circle_points helper.
    def calculate_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> str:
        """Calculate great circle distance between two points.
    
        Args:
            lat1: Latitude of first point in degrees
            lon1: Longitude of first point in degrees
            lat2: Latitude of second point in degrees
            lon2: Longitude of second point in degrees
    
        Returns:
            JSON string with distance information
        """
        try:
            # Calculate great circle route
            route = great_circle_points(
                lat1, lon1, lat2, lon2, step_km=1000000
            )  # Single segment
    
            return json.dumps(
                {
                    "distance_km": route["distance_km"],
                    "distance_nm": route["distance_nm"],
                    "initial_bearing_deg": route["initial_bearing_deg"],
                    "final_bearing_deg": route["final_bearing_deg"],
                    "coordinates": {
                        "start": {"lat": lat1, "lon": lon1},
                        "end": {"lat": lat2, "lon": lon2},
                    },
                },
                indent=2,
            )
    
        except Exception as e:
            return f"Distance calculation error: {str(e)}"
  • Registration of the calculate_distance tool in the FastMCP server using mcp.tool().
    mcp.tool(calculate_distance)
  • The great_circle_points helper function that performs the core geodesic calculation using geographiclib, generating points along the great circle and total distance. Used by the calculate_distance handler.
    def great_circle_points(
        lat1: float, lon1: float, lat2: float, lon2: float, step_km: float
    ) -> tuple[list[tuple[float, float]], float]:
        g = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
        dist_m = g["s12"]
        line = Geodesic.WGS84.Line(lat1, lon1, g["azi1"])
        n = max(1, int(math.ceil((dist_m / 1000.0) / step_km)))
        pts = []
        for i in range(n + 1):
            s = min(dist_m, (dist_m * i) / n)
            p = line.Position(s)
            pts.append((p["lat2"], p["lon2"]))
        return pts, dist_m / 1000.0

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cheesejaguar/aerospace-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server