Cesium中对geojson的处理

最近关注到cesium对json处理,有点小收获,记录下来,有不对的请指正。

Cesium读取geojson

cesium支持topojson,GEOjson和普通的json格式,方法可以共用

一.topojson


    

二.geojson


    

三.普通json


  

给geojson设置颜色与拉伸高度

//Seed the random number generator for repeatable results.
    Cesium.Math.setRandomNumberSeed(0);

    var promise = Cesium.GeoJsonDataSource.load('../../SampleData/test.json');
    promise.then(function(dataSource) {
        viewer.dataSources.add(dataSource);

        //Get the array of entities
        var entities = dataSource.entities.values;
        
        var colorHash = {};
        for (var i = 0; i < entities.length; i++) {
            //For each entity, create a random color based on the state name.
            //Some states have multiple entities, so we store the color in a
            //hash so that we use the same color for the entire state.
            var entity = entities[i];
            var name = entity.name;
            var color = colorHash[name];
            if (!color) {
                color = Cesium.Color.fromRandom({
                    alpha : 1.0
                });
                colorHash[name] = color;
            }
            
            //Set the polygon material to our random color.
            entity.polygon.material = color;
            //Remove the outlines.
            entity.polygon.outline = false;

            //Extrude the polygon based on the state's population.  Each entity
            //stores the properties for the GeoJSON feature it was created from
            //Since the population is a huge number, we divide by 50.
            entity.polygon.extrudedHeight = entity.properties.Shape_Area / 100000.0;
          
            }
        }
        viewer.zoomTo(promise);

geojson按时间显示

Cesium.GeoJsonDataSource.load('data/earthquake.geojson').then(function(dataSource) {
        viewer.dataSources.add(dataSource);

        var entities = dataSource.entities.values; 

        for (var i = 0; i < entities.length; i++) {
            var entity = entities[i];
            entity.billboard = undefined;
            entity.point = new Cesium.PointGraphics({
                color: Cesium.Color.RED,
                pixelSize: 10
            });
            entity.availability = new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
                start: Cesium.JulianDate.fromIso8601(entity.properties.date),
                stop: addDay(Cesium.JulianDate.fromIso8601(entity.properties.date))
            })]);
        }
    });
});

        可以看到只是多加了 entity.availability = ... 一项,这样就能够按照时间显示,主要是其中的 start 和 stop 属性,控制显示的时间范围。date 是 GeoJSON 中数据的一个字段,格式为 '2008-01-01',当然你也可以使用其他格式,在此处进行自定义处理即可,addDay 用于控制显示一天,此处不用多考虑。

 

Cesium移除geojson数据

//移除全部 
var len = viewer.dataSources.length;
 if(len > 0){
     console.log(len)
     for(var n = 0; n < len; n++){
        //每删除一条viewer.dataSources,len就会减少1
         viewer.dataSources.remove(viewer.dataSources.get(0));
     }
 }

//移除某一条
var dataSources = viewer.dataSources;
for (var i = 0; i < viewer.dataSources.length; i++) {
    if (viewer.dataSources.get(i).name.indexOf("dz")!= -1) {
        viewer.dataSources.remove(viewer.dataSources.get(i));
        i--;
    }
}



//看看viewer.dataSources.get(i)获取到数据源没有

 

你可能感兴趣的:(Cesium)