geometry 与 wkt 互转

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 定义变量
        const jsonPoint = [39.69, 120.39];

        const jsonLine = [
            [39.69, 120.39],
            [39.69, 120.39],
            [39.69, 120.39],
            [39.69, 120.39]
        ];

        const jsonMultiLine = [
            [
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39]
            ],
            [
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39]
            ]
        ];

        const jsonPolygon = [
            [
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39]
            ],
            [
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39],
                [39.69, 120.39]
            ]
        ];

        const jsonMultiPolygon = [
            [
                [
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39]
                ],
                [
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39]
                ],
            ],
            [
                [
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39]
                ],
                [
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39],
                    [39.69, 120.39]
                ]
            ]
        ];

        const wktPoint = "point(120.39 39.69)";

        const wktLine = "linestring(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69)";

        const wktMultiLine =
            "multiLinestring((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69))";

        const wktPolygon =
            "polygon((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69))";

        const wktMultiPolygon =
            "multiPolygon(((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69)),((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69)))";

        /**
         * wkt转经纬度坐标
         *  @param  wkt wkt数据
         */
        function wktToGeometry(wkt) {
            if (!wkt) {
                return;
            }

            let type = wkt.match(/[a-zA-Z]+/g);
            if (!type) {
                return null;
            }

            let coordinates = wkt.replace(type, '');
            coordinates = coordinates.replaceAll('(', '[');
            coordinates = coordinates.replaceAll(')', ']');
            coordinates = coordinates.replaceAll(' ', ',');

            function coordinatesFormat(coordinates) {
                const arr = [];
                if (!isArr) {
                    return;
                }
                for (let i = 0; i < coordinates.length; i++) {
                    if (isArr(coordinates[i])) {
                        coordinates[i] = coordinatesFormat(coordinates[i]);
                    } else {
                        if (i % 2 === 0) {
                            arr.push([coordinates[i + 1], coordinates[i]]);
                        }
                        if (i === coordinates.length - 1) {
                            coordinates = arr;
                        }
                    }
                }
                return coordinates;
            }

            function getType(type) {
                let newType = type.toLowerCase();

                if (newType.includes('point')) {
                    return 'point';
                }

                if (newType.includes('line')) {
                    return 'line';
                }

                if (newType.includes('polygon')) {
                    return 'polygon';
                }

                return newType;
            }

            function isArr(arr) {
                return Array.isArray(arr);
            }

            let lngLats = coordinatesFormat(JSON.parse(coordinates));

            const newType = getType(type[0]);

            // 点特殊处理
            if (newType === 'point') {
                lngLats = lngLats[0];
            }

            return {
                type: newType,
                lngLats
            };
        }


        /**
         * 经纬度坐标转wkt
         * @param  geometry 要素空间信息
         */
        function geometryToWkt(geometry) {
            let {
                type,
                coordinates
            } = geometry;

            let coordinateWkt = '';

            let typeMap = {
                point: getPointWkt,
                linestring: getLineWkt,
                multiLinestring: getMultiLineWkt,
                polygon: getPolygonWkt,
                multiPolygon: getMultiPolygonWkt
            };

            // 根据类型调取方法
            if (typeMap[type]) {
                // 点特殊处理
                if (type === 'point') {
                    coordinateWkt = `(${typeMap[type](coordinates)})`;
                } else {
                    coordinateWkt = typeMap[type](coordinates);
                }

            };

            // 点
            function getPointWkt(coordinates) {
                if (isArr(coordinates)) {
                    return `${coordinates[1]} ${coordinates[0]}`;
                }
            }

            // 线
            function getLineWkt(coordinates) {
                let str = '';
                if (isArr(coordinates)) {
                    coordinates.forEach(coordinate => {
                        const symbol = str ? ',' : '';
                        str += `${symbol}${getPointWkt(coordinate)}`;
                    });
                }
                return `(${str})`;
            }

            // 多线
            function getMultiLineWkt(coordinates) {
                let str = '';
                if (isArr(coordinates)) {
                    coordinates.forEach(coordinate => {
                        const symbol = str ? ',' : '';
                        str += `${symbol}${getLineWkt(coordinate)}`;
                    });
                }
                return `(${str})`;
            }

            // 面(处理方式同多线)
            function getPolygonWkt(coordinates) {
                return getMultiLineWkt(coordinates);
            }

            // 多面
            function getMultiPolygonWkt(coordinates) {
                let str = '';
                if (isArr(coordinates)) {
                    coordinates.forEach(coordinate => {
                        const symbol = str ? ',' : '';
                        str += `${symbol}${getPolygonWkt(coordinate)}`;
                    });
                }
                return `(${str})`;
            }

            // 判断是否为数组
            function isArr(arr) {
                return Array.isArray(arr);
            }

            return `${type}${coordinateWkt}`;
        }



        console.log(geometryToWkt({
            type: 'point',
            coordinates: jsonPoint
        }));

        console.log(geometryToWkt({
            type: 'linestring',
            coordinates: jsonLine
        }));

        console.log(geometryToWkt({
            type: 'multiLinestring',
            coordinates: jsonMultiLine
        }));

        console.log(geometryToWkt({
            type: 'polygon',
            coordinates: jsonPolygon
        }));

        console.log(geometryToWkt({
            type: 'multiPolygon',
            coordinates: jsonMultiPolygon
        }));

        console.log('---------------');

        console.log(wktToGeometry(wktPoint));

        console.log(wktToGeometry(wktLine));

        console.log(wktToGeometry(wktMultiLine));

        console.log(wktToGeometry(wktPolygon));
        console.log(wktToGeometry(wktMultiPolygon));
    </script>
</body>

</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值