Skip to main content
Glama
NodeGIS

GeoSpatial MCP Server

by NodeGIS

mcp_geo_calculate_area

Calculate polygon area using coordinates in WGS84, GCJ02, or BD09 systems. Supports units like square meters, square kilometers, or hectares. Automatically handles polygon closure and performs calculations via Web Mercator projection. Ideal for small to medium-scale spatial analysis.

Instructions

计算多边形面积。支持多种坐标系统输入,内部会先转换为WGS84坐标,再通过Web Mercator投影进行平面面积计算。多边形无需手动闭合。适用于中小尺度的面积计算。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
coordTypeNo输入坐标类型WGS84
coordinatesYes多边形坐标点数组,格式:[[lon1,lat1], [lon2,lat2],...]
unitNo面积单位:square_meters(平方米)、square_kilometers(平方公里)或hectares(公顷)square_meters

Implementation Reference

  • Handler function that processes the tool input, converts coordinates to WGS84, calculates area using calculatePolygonArea and Web Mercator projection, converts units, rounds to 4 decimals, and returns JSON-formatted result or error.
      async ({ coordinates, unit, coordType }) => {
        try {
          // 如果不是WGS84坐标,先转换为WGS84
          let wgs84Coordinates = coordinates;
          if (coordType === "GCJ02") {
            wgs84Coordinates = coordinates.map(([lon, lat]) => this.GCJ02toWGS84(lon, lat));
          } else if (coordType === "BD09") {
            wgs84Coordinates = coordinates.map(([lon, lat]) => this.BD09toWGS84(lon, lat));
          }
    
          let area = this.calculatePolygonArea(wgs84Coordinates);
          
          // 单位转换
          switch (unit) {
            case "square_kilometers":
              area = area / 1000000; // 转换为平方公里
              break;
            case "hectares":
              area = area / 10000; // 转换为公顷
              break;
          }
    
          // 保留4位小数
          area = Math.round(area * 10000) / 10000;
    
          return {
            content: [{
              type: "text",
              text: JSON.stringify({
                area: area,
                unit: unit,
                coordinates: coordinates,
                coordType: coordType
              }, null, 2)
            }]
          };
        } catch (err) {
          return {
            content: [{
              type: "text",
              text: `错误: ${err.message}`
            }],
            isError: true
          };
        }
      }
    );
  • Zod input schema defining parameters: coordinates (array of [lon,lat] pairs, min 3), unit (square_meters|square_kilometers|hectares, default square_meters), coordType (WGS84|GCJ02|BD09, default WGS84).
    {
      coordinates: z.array(z.array(z.number())).min(3).describe("多边形坐标点数组,格式:[[lon1,lat1], [lon2,lat2],...]"),
      unit: z.enum(["square_meters", "square_kilometers", "hectares"]).default("square_meters")
        .describe("面积单位:square_meters(平方米)、square_kilometers(平方公里)或hectares(公顷)"),
      coordType: z.enum(["WGS84", "GCJ02", "BD09"]).default("WGS84").describe("输入坐标类型")
    },
  • dist/server.js:282-284 (registration)
    Tool registration call using this.tool(name, description, schema, handler) inside GeoServer.initializeTools() method.
    this.tool(
      "mcp_geo_calculate_area",
      "计算多边形面积。支持多种坐标系统输入,内部会先转换为WGS84坐标,再通过Web Mercator投影进行平面面积计算。多边形无需手动闭合。适用于中小尺度的面积计算。",
  • Core helper method that closes the polygon if needed, projects coordinates to Web Mercator, computes shoelace formula area in square meters.
    // 计算多边形面积(平方米)
    calculatePolygonArea(coordinates) {
      // 确保多边形闭合
      if (coordinates[0][0] !== coordinates[coordinates.length - 1][0] ||
          coordinates[0][1] !== coordinates[coordinates.length - 1][1]) {
        coordinates = [...coordinates, coordinates[0]];
      }
    
      // 转换为Web Mercator坐标
      const mercatorCoords = coordinates.map(([lon, lat]) => this.lngLatToWebMercator(lon, lat));
    
      // 使用平面坐标系下的多边形面积计算公式
      let area = 0;
      for (let i = 0; i < mercatorCoords.length - 1; i++) {
        const [x1, y1] = mercatorCoords[i];
        const [x2, y2] = mercatorCoords[i + 1];
        area += (x1 * y2 - x2 * y1);
      }
    
      // 取绝对值并除以2
      return Math.abs(area) / 2;
    }
  • Helper for projecting longitude/latitude to Web Mercator coordinates, used in area calculation.
    lngLatToWebMercator(lng, lat) {
      const x = lng * ORIGIN_SHIFT / 180.0;
      const y = Math.log(Math.tan((90 + lat) * Math.PI / 360.0)) / (Math.PI / 180.0);
      const y2 = y * ORIGIN_SHIFT / 180.0;
      return [x, y2];
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key behaviors: coordinate system conversion (WGS84 conversion), projection method (Web Mercator), polygon handling (no manual closure needed), and scale limitations (small to medium scale). This provides good operational context beyond basic functionality.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is efficiently structured in three sentences that each add value: core functionality, technical implementation details, and usage context. There's no wasted text, and key information is front-loaded about what the tool does.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a calculation tool with no annotations and no output schema, the description provides good contextual completeness. It covers the calculation method, coordinate handling, polygon requirements, and scale limitations. The main gap is lack of information about return values or error conditions.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 100% schema description coverage, the schema already documents all three parameters thoroughly. The description adds some context about coordinate system conversion and scale applicability, but doesn't provide additional parameter-specific semantics beyond what's in the schema descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('计算多边形面积' - calculate polygon area) and resource (polygons), distinguishing it from sibling tools like distance calculation and coordinate conversion. It provides additional context about supported coordinate systems and the calculation method.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context about when to use this tool ('适用于中小尺度的面积计算' - suitable for small to medium scale area calculations) and mentions that polygons don't need manual closure. However, it doesn't explicitly state when NOT to use it or provide direct alternatives to sibling tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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/NodeGIS/geo-mcp-server'

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