cesiumjs加载geojson+建筑物分层设色

需求:噪声管理项目,在项目的实际开发过程中,需要展示某一个地方的三维建筑物分布情况,并且一个建筑物要根据楼层不同高度设置不同的颜色,决定使用基于地图的三维框架cesium来实现。。在cesium官网上面,有通过加载geojson展示三维建筑物的实例,代码如下所示,(官网例子一个建筑物对应一个实体,一个建筑物只能有一种颜色,这跟建筑物分层设色的需求并不符合,还有待深究。。)

var neighborhoodsPromise = Cesium.GeoJsonDataSource.load('./SampleData/building.geojson', geojsonOptions);
    var neighborhoods;
    neighborhoodsPromise.then(function(dataSource) {
        // Add the new data as entities to the viewer
        var city = viewer.dataSources.add(dataSource);//向地图加载要素
        neighborhoods = dataSource.entities;  //获取要素中的实体,给实体一定的高度
        // Get the array of entities
        var neighborhoodEntities = dataSource.entities.values;
        for (var i = 0; i < neighborhoodEntities.length; i++) {
            var entity = neighborhoodEntities[i];
            if (Cesium.defined(entity.polygon)) {
                // 给每个实体不同的配色,每个实体对应一个建筑物,每个建筑物则对应一种颜色,根据建筑物高度不同,设置不同显示颜色
                entity.name = entity.properties.neighborhood;
                if(entity.properties.height>=0 && entity.properties.height<=4){
                    entity.polygon.material = Cesium.Color.AQUA;
                }else if(entity.properties.height>4 && entity.properties.height<=6){
                    entity.polygon.material = Cesium.Color.AQUAMARINE;
                }else if(entity.properties.height>6 && entity.properties.height<=8){
                    entity.polygon.material = Cesium.Color.BEIGE;
                }else if(entity.properties.height>8 && entity.properties.height<=10){
                    entity.polygon.material = Cesium.Color.CADETBLUE;
                }else if(entity.properties.height>10 && entity.properties.height<=12){
                    entity.polygon.material = Cesium.Color.CHARTREUSE;
                }else if(entity.properties.height>12 && entity.properties.height<=14){
                    entity.polygon.material = Cesium.Color.CORNFLOWERBLUE;
                }
                else{
                    console.log(222);
                    entity.polygon.material = Cesium.Color.DARKGOLDENROD;
                }
                entity.polygon.outline = false;
                // Tells the polygon to color the terrain. ClassificationType.CESIUM_3D_TILE will color the 3D tileset, and ClassificationType.BOTH will color both the 3d tiles and terrain (BOTH is the default)
                entity.polygon.classificationType = Cesium.ClassificationType.TERRAIN;//地形
                // Generate Polygon position
                var polyPositions = entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;//获取当前时间的一个位置消息
                var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center;//获得一个中心区域
                polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);
                entity.position = polyCenter;
                entity.polygon.extrudedHeight = entity.properties.height;
            }
        }
        //加载的建筑物的总数
        console.log(neighborhoodEntities.length);
    });

效果图如下所示:
cesiumjs加载geojson+建筑物分层设色_第1张图片
由于现在一个建筑物只能是一种颜色,所以跟需求并不符合,因此另外想出了一种方案,用for循环加载每一个建筑物的时候,在内部再通过一个for循环根据楼层设定不同的颜色,这样是不可行的,这样还是针对一个建筑物实体进行操作,会发生颜色覆盖,而且只会显示每个建筑物的最高层,贴近地面的是不可见的,因为发生了覆盖,在此陈述一下是不希望有人向我一样,可以少走点弯路;于是就接着想别的办法,期间也尝试了很多办法,有借鉴于别的框架的,比如supermap,等等,最后终于突破了,还是用cesiumjs这个框架,
在每个实体的加载过程中,相当于每个建筑物的每一层都是一个实体,在for 循环内部利用viewer.entities.add()记载进来,然后单独设置一定的颜色就可以了。。代码如下:

        for (var i = 0; i < neighborhoodEntities.length; i++) {
            var entity = neighborhoodEntities[i];
            if (Cesium.defined(entity.polygon)) {
                entity.polygon.outline = false;
                // Tells the polygon to color the terrain. ClassificationType.CESIUM_3D_TILE will color the 3D tileset, and ClassificationType.BOTH will color both the 3d tiles and terrain (BOTH is the default)
                entity.polygon.classificationType = Cesium.ClassificationType.TERRAIN;//地形
                // Generate Polygon position
                var polyPositions = entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;//获取当前时间的一个位置消息
                var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center;//获得一个中心区域
                // console.log(polyCenter);             //一个坐标点,中心点
                polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);
                // console.log(polyCenter);
                entity.position = polyCenter;
                // entity.polygon.extrudedHeight = entity.properties.height;
                var positionArr = [];   //定义实体坐标数组
                var features= getFeatures('./SampleData/building.geojson');
                for(var j=0;jif(i == j){
                        var choosefeature = features.features[j];
                        for(var m=0;m0].length;m++){
                            positionArr.push(choosefeature.geometry.coordinates[0][m][0]);
                            positionArr.push(choosefeature.geometry.coordinates[0][m][1]);
                        }
                    }
                }

                //下面这段代码表示每个建筑物2米一个颜色
                for(var currentHeight=0;currentHeight2){
                    //判断currentHeight+2是否大于entity.properties.height._value,在地图上新增实体类型
                        var Polygon = viewer.entities.add({
                            name : 'Blue polygon with holes',
                            polygon : {
                                hierarchy : {
                                    positions : Cesium.Cartesian3.fromDegreesArray(positionArr)
                                },
                                material : setColor(currentHeight),
                                height : currentHeight,
                                extrudedHeight: currentHeight+2,
                                outline : false // height is required for outline to display
                            }
                        });
                    }else{
                        var Polygon = viewer.entities.add({
                            name : 'Blue polygon with holes',
                            polygon : {
                                hierarchy : {
                                    positions : Cesium.Cartesian3.fromDegreesArray(positionArr)
                                },
                                material : setColor(currentHeight),
                                height : currentHeight,
                                extrudedHeight: entity.properties.height._value,
                                outline : false // height is required for outline to display
                            }
                        });
                    }
                }
            }
        }

效果图如下:
cesiumjs加载geojson+建筑物分层设色_第2张图片

你可能感兴趣的:(Cesium,cesiumjs,三维可视化,geojson,分层设色)