# Viewer and scene problem

**URL:** <https://community.cesium.com/t/viewer-and-scene-problem/6936>\
**Category:** CesiumJS\
**Created:** [May 18, 2018, 8:38am UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936 "2018-05-18T08:38:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dogus\_Guler](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dogus\_Guler](https://community.cesium.com/u/Dogus_Guler)\
**Post date:** [May 18, 2018, 8:38am UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936/1 "2018-05-18T08:38:51Z")

</div>

**Hello, I create an app contains glTF model and geojson data sources. When I change the data source with a function using HTML button, the code gives an error.**

**I tried to “viewer.scene.primitives.removeAll();” code block but I did not take a result. How can I solve this problem?**

**Thank you.**

**Dogus**

---

<div class="post-metadata">

**Author:** ![Scott\_Reynolds](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/scott_reynolds/32/2014_2.png) [@Scott\_Reynolds](https://community.cesium.com/u/Scott_Reynolds)\
**Post date:** [May 19, 2018, 3:07pm UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936/2 "2018-05-19T15:07:36Z")

</div>

Hello Dogus,

What is the error you are seeing?

Scott

---

<div class="post-metadata">

**Author:** ![Dogus\_Guler](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dogus\_Guler](https://community.cesium.com/u/Dogus_Guler)\
**Post date:** [May 20, 2018, 10:08am UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936/3 "2018-05-20T10:08:24Z")

</div>

Hi, you can see the error in the link below.

[http://web.itu.edu.tr/gulerdo/Cesium/Apps/HelloWorld4.html](http://web.itu.edu.tr/gulerdo/Cesium/Apps/HelloWorld4.html)

Thanks

19 Mayıs 2018 Cumartesi 18:07:36 UTC+3 tarihinde Scott Reynolds yazdı:

---

<div class="post-metadata">

**Author:** ![Gabby\_Getz](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/gabby_getz/32/7838_2.png) [@Gabby\_Getz](https://community.cesium.com/u/Gabby_Getz)\
**Post date:** [May 22, 2018, 2:37pm UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936/4 "2018-05-22T14:37:40Z")

</div>

Could you help us track down the error by creating a [Sandcastle](https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/index.html?src=Polyline.html) example that duplicates this?

Thanks,  
Gabby

---

<div class="post-metadata">

**Author:** ![Scott\_Reynolds](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/scott_reynolds/32/2014_2.png) [@Scott\_Reynolds](https://community.cesium.com/u/Scott_Reynolds)\
**Post date:** [May 23, 2018, 1:02am UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936/5 "2018-05-23T01:02:11Z")

</div>

It looks like the call to primitives.removeAll() destroys the “primitives” that are associated with the GeoJsonDataSource and thus the call dataSources.removeAll() attempts to destroy primitives that have already been destroyed. Maybe Gabby knows for sure.

Here’s a Sandcastle example that works.

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var modelPrimitive = null;

function hideByHeight1() {

```
viewer.dataSources.removeAll();

if (modelPrimitive) {

    viewer.scene.primitives.remove(modelPrimitive);

    modelPrimitive = null;

}

var promise = Cesium.GeoJsonDataSource.load('../../SampleData/Dogus/Dogus_ist.geojson');

promise.then(function (dataSource) {

    viewer.dataSources.add(dataSource);

    var entities = dataSource.entities.values;

    for (var i = 0; i < entities.length; i++) {

        //Remove the outlines.

        entities[i].polygon.outline = false;

        //Extrude the polygon based on any attribute you desire         

        entities[i].polygon.extrudedHeight = entities[i].properties.Shape_Area * 100000;

        if (entities[i].polygon.extrudedHeight > 5000) {

            entities[i].polygon.material = Cesium.Color.FUCHSIA;

        } else {

            entities[i].polygon.material = Cesium.Color.GOLD;

        }

    }

    viewer.zoomTo(promise);

});

```

}

function hideByHeight() {

```
viewer.dataSources.removeAll();

if (modelPrimitive) {

    viewer.scene.primitives.remove(modelPrimitive);

    modelPrimitive = null;

}

var promise = Cesium.GeoJsonDataSource.load('../../SampleData/Dogus/Dogus_ist.geojson');

promise.then(function (dataSource) {

    viewer.dataSources.add(dataSource);

    var entities = dataSource.entities.values;

    for (var i = 0; i < entities.length; i++) {

        //Remove the outlines.

        entities[i].polygon.outline = false;

        //Extrude the polygon based on any attribute you desire         

        entities[i].polygon.extrudedHeight = entities[i].properties.Shape_Area * 100000;

        if (entities[i].polygon.extrudedHeight > 1000) {

            entities[i].polygon.material = Cesium.Color.DARKCYAN;

        } else {

            entities[i].polygon.material = Cesium.Color.GOLD;

        }

    }

    viewer.zoomTo(entities);

```

});

}

function addModel() {

```
viewer.dataSources.removeAll();

if (modelPrimitive) {

    viewer.scene.primitives.remove(modelPrimitive);

    modelPrimitive = null;

}

var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(

    Cesium.Cartesian3.fromDegrees(29, 41.0, 0.0));

modelPrimitive = viewer.scene.primitives.add(Cesium.Model.fromGltf({

    url: '../../SampleData/Dogus/Dogus_deger2.gltf',

    modelMatrix: modelMatrix,

    scale: 2000

}));

```

}

Sandcastle.addToolbarMenu([{

```
text: 'Options'

```

}, {

```
text: '> 1000',

onselect: function() {

    hideByHeight();

}

```

}, {

```
text: '> 5000',

onselect: function() {

    hideByHeight1();

}

```

}, {

```
text: 'Model',

onselect: function() {

    addModel();

}

```

}]);

``

---

<div class="post-metadata">

**Author:** ![Dogus\_Guler](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dogus\_Guler](https://community.cesium.com/u/Dogus_Guler)\
**Post date:** [May 23, 2018, 5:23am UTC](https://community.cesium.com/t/viewer-and-scene-problem/6936/6 "2018-05-23T05:23:05Z")

</div>

It works, thank you so much.

Bests,
