| | |
| | | /** |
| | | * 将普通经纬度数组转换为标准的 MULTIPOLYGON WKT 格式 |
| | | * @param {Array<Array<number>>} coordinates - 经纬度数组,每个元素是一个 [经度, 纬度] 的点 |
| | | * 将多个多边形的经纬度数组转换为标准的 MULTIPOLYGON WKT 格式 |
| | | * @param {Array<Array<Array<number>>>} multiPolygonCoordinates - 多个多边形的坐标数组 |
| | | * @returns {string} 标准化的 WKT 格式字符串 |
| | | */ |
| | | export function convertToWKT(coordinates) { |
| | | export function convertToWKT(multiPolygonCoordinates) { |
| | | // 检查输入是否为非空数组 |
| | | if (!Array.isArray(coordinates) || coordinates.length === 0) { |
| | | throw new Error("Invalid input: 'coordinates' must be a non-empty array of [longitude, latitude] points."); |
| | | if (!Array.isArray(multiPolygonCoordinates) || multiPolygonCoordinates.length === 0) { |
| | | throw new Error("Invalid input: 'multiPolygonCoordinates' must be a non-empty array of polygons."); |
| | | } |
| | | |
| | | // 检查每个点是否是有效的 [经度, 纬度] 数组 |
| | | for (const point of coordinates) { |
| | | if (!Array.isArray(point) || point.length !== 2 || typeof point[0] !== "number" || typeof point[1] !== "number") { |
| | | throw new Error("Invalid input: Each coordinate must be an array of two numbers [longitude, latitude]."); |
| | | // 转换每个多边形的坐标 |
| | | const polygons = multiPolygonCoordinates.map(polygon => { |
| | | if (!Array.isArray(polygon) || polygon.length === 0) { |
| | | throw new Error("Invalid input: Each polygon must be a non-empty array of [longitude, latitude] points."); |
| | | } |
| | | } |
| | | |
| | | // 将经纬度数组转换为 WKT 格式的坐标字符串 |
| | | const wktCoordinates = coordinates |
| | | .map(([longitude, latitude]) => `${longitude} ${latitude}`) |
| | | .join(","); |
| | | // 检查每个点是否是有效的 [经度, 纬度] 数组 |
| | | for (const point of polygon) { |
| | | if (!Array.isArray(point) || point.length !== 2 || typeof point[0] !== "number" || typeof point[1] !== "number") { |
| | | throw new Error("Invalid input: Each coordinate must be an array of two numbers [longitude, latitude]."); |
| | | } |
| | | } |
| | | |
| | | // 将每个多边形的坐标转换为 WKT 格式的字符串 |
| | | return `((${polygon.map(([lng, lat]) => `${lng} ${lat}`).join(",")}))`; |
| | | }); |
| | | |
| | | // 构建标准的 MULTIPOLYGON WKT 格式 |
| | | return `MULTIPOLYGON(((${wktCoordinates})))`; |
| | | return `MULTIPOLYGON(${polygons.join(",")})`; |
| | | } |