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
| Name | Required | Description | Default |
|---|---|---|---|
| coordType | No | 输入坐标类型 | WGS84 |
| coordinates | Yes | 多边形坐标点数组,格式:[[lon1,lat1], [lon2,lat2],...] | |
| unit | No | 面积单位:square_meters(平方米)、square_kilometers(平方公里)或hectares(公顷) | square_meters |
Implementation Reference
- dist/server.js:291-337 (handler)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 }; } } );
- dist/server.js:285-290 (schema)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投影进行平面面积计算。多边形无需手动闭合。适用于中小尺度的面积计算。",
- dist/server.js:45-66 (helper)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; }
- dist/server.js:154-159 (helper)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]; }