wangjuncheng
2025-04-16 bd13b522f95d4f9429eb0e8bc7df2e1ce3d58554
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * 将普通经纬度数组转换为标准的 MULTIPOLYGON WKT 格式
 * @param {Array<Array<number>>} coordinates - 经纬度数组,每个元素是一个 [经度, 纬度] 的点
 * @returns {string} 标准化的 WKT 格式字符串
 */
export function convertToWKT(coordinates) {
    // 检查输入是否为非空数组
    if (!Array.isArray(coordinates) || coordinates.length === 0) {
        throw new Error("Invalid input: 'coordinates' must be a non-empty array of [longitude, latitude] points.");
    }
 
    // 检查每个点是否是有效的 [经度, 纬度] 数组
    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].");
        }
    }
 
    // 将经纬度数组转换为 WKT 格式的坐标字符串
    const wktCoordinates = coordinates
        .map(([longitude, latitude]) => `${longitude} ${latitude}`)
        .join(",");
 
    // 构建标准的 MULTIPOLYGON WKT 格式
    return `MULTIPOLYGON(((${wktCoordinates})))`;
}