guonan
7 天以前 a57caa72a54efe9de3fe26a6c36d3e8038267377
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
27
28
29
30
31
/**
 * 将多个多边形的经纬度数组转换为标准的 MULTIPOLYGON WKT 格式
 * @param {Array<Array<Array<number>>>} multiPolygonCoordinates - 多个多边形的坐标数组
 * @returns {string} 标准化的 WKT 格式字符串
 */
export function convertToWKT(multiPolygonCoordinates) {
    // 检查输入是否为非空数组
    if (!Array.isArray(multiPolygonCoordinates) || multiPolygonCoordinates.length === 0) {
        throw new Error("Invalid input: 'multiPolygonCoordinates' must be a non-empty array of polygons.");
    }
 
    // 转换每个多边形的坐标
    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.");
        }
 
        // 检查每个点是否是有效的 [经度, 纬度] 数组
        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(${polygons.join(",")})`;
}