Cesium缓冲区分析

Cesium缓冲区分析

前言

在Cesium中做缓冲区分析其实与在二维地图中做缓存区分析是一样的道理,缓冲后需要将二维坐标转到三维坐标,然后在场景中显示结果。

效果图

在这里插入图片描述

关键代码

//初始化点缓冲
    initPointBuffer() {
        let point = [106.422638966289, 29.5698367125623];
        this.addPoint(point);

        let pointF = turf.point(point);
        let buffered = turf.buffer(pointF, 60, { units: 'meters' });
        let coordinates = buffered.geometry.coordinates;
        let points = coordinates[0];
        let degreesArray = this.pointsToDegreesArray(points);
        this.addBufferPolyogn(Cesium.Cartesian3.fromDegreesArray(degreesArray));
    },

    //添加点
    addPoint(point) {
        this.viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(point[0], point[1], 0),
            point: {
                pixelSize: 10,
                color: Cesium.Color.YELLOW,
                outlineWidth: 3,
                outlineColor: Cesium.Color.YELLOW.withAlpha(0.4),
            }
        });
    },

    //初始化线缓冲
    initPolylineBuffer() {
        let points = [
            [106.425203158107, 29.5694914480581],
            [106.428808047023, 29.569230166027],
            [106.431661917416, 29.5692674920729],
            [106.434708906857, 29.5693048181049]
        ];
        let degreesArray = this.pointsToDegreesArray(points);
        this.addPolyline(Cesium.Cartesian3.fromDegreesArray(degreesArray));

        let polylineF = turf.lineString(points);
        let buffered = turf.buffer(polylineF, 30, { units: 'meters' });
        let coordinates = buffered.geometry.coordinates;
        points = coordinates[0];
        degreesArray = this.pointsToDegreesArray(points);
        this.addBufferPolyogn(Cesium.Cartesian3.fromDegreesArray(degreesArray));
    },

    //添加线
    addPolyline(positions) {
        this.viewer.entities.add({
            polyline: {
                positions: positions,
                width: 2,
                material: Cesium.Color.YELLOW,
            }
        })
    },

    //初始化面缓冲
    initPolygonBuffer() {
        let points = [
            [106.438549830166, 29.5701073244566],
            [106.440695597377, 29.5701073244566],
            [106.440738512722, 29.5688755679036],
            [106.438700033871, 29.5687262630581],
            [106.438034846035, 29.5690248725284],
            [106.438549830166, 29.5701073244566]
        ];

        let degreesArray = this.pointsToDegreesArray(points);
        this.addPolygon(Cesium.Cartesian3.fromDegreesArray(degreesArray));

        let polygonF = turf.polygon([points]);
        let buffered = turf.buffer(polygonF, 60, { units: 'meters' });
        let coordinates = buffered.geometry.coordinates;
        points = coordinates[0];
        degreesArray = this.pointsToDegreesArray(points);
        this.addBufferPolyogn(Cesium.Cartesian3.fromDegreesArray(degreesArray));
    },

    //添加面
    addPolygon(positions) {
        this.viewer.entities.add({
            polygon: {
                hierarchy: new Cesium.PolygonHierarchy(positions),
                material: Cesium.Color.YELLOW.withAlpha(0.6),
                classificationType: Cesium.ClassificationType.BOTH
            },
            polyline: {
                positions: positions,
                width: 2,
                material: Cesium.Color.YELLOW.withAlpha(0.4),
            }
        });
    },

    //添加缓冲面
    addBufferPolyogn(positions) {
        this.viewer.entities.add({
            polygon: {
                hierarchy: new Cesium.PolygonHierarchy(positions),
                material: Cesium.Color.RED.withAlpha(0.6),
                classificationType: Cesium.ClassificationType.BOTH
            },
        });
    },

    //格式转换
    pointsToDegreesArray(points) {
        let degreesArray = [];
        points.map(item => {
            degreesArray.push(item[0]);
            degreesArray.push(item[1]);
        });
        return degreesArray;
    },

在前端我们可以借助turf插件进行缓冲区的生成,这样不用依赖后的GIS服务器就能做分析。

详情参见 Cesium实战专栏

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xt3d

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值