Filter SceneLayer with SceneFilter

This sample demonstrates how to apply a SceneFilter to a SceneLayer and persist this filter by saving it in a WebScene. In this sample, the 3D OpenStreetMap layers are used which contains data of 3D buildings and trees for the whole globe. Check out this blog post for more information about these layers.

A SceneFilter uses polygons to mask a scene layer. There are two different ways of masking, either removing parts of the scene layer ("disjoint") or only keeping certain parts ("contains"). The filter accepts two inputs: the type of masking (spatialRelationship) and a collection of polygons (geometries).

Use dark colors for code blocksCopy
1
2
3
4
5
// Set the filter on the scene layer
sceneLayer.filter = new SceneFilter({
  geometries: [polygon1, polygon2],
  spatialRelationship: "contains"
});

In this sample, the Sketch widget is used as an easy tool to add and modify the filter polygons. Different event listeners make sure we catch every modification of the polygons and trigger an update of the filter:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
// Listen to sketch widget's create event to update the filter
sketch.on("create", function (event) {
  // Only once the polygon is completed
  if (event.state === "complete") {
    updateFilter();
  }
});

Another persisted way of filtering out data is to exclude specific features. This can be done using excludeObjectIds:

Use dark colors for code blocksCopy
1
2
// Exclude some features
sceneLayer.excludeObjectIds = [132272992, 5277001]

When saving the scene to a WebScene, the filter is persisted as part of the WebScene. When you click 'Save', you will be asked to login with your ArcGIS credentials and then a new scene is saved to your content on ArcGIS Online. If you open the scene in the SceneViewer, you will see the same filters in place. See here on how to edit a filter in the SceneViewer.

Another way to filter a scene layer is using a FeatureFilter. This filter is set on the SceneLayerView instead of the SceneLayer. It has additional functionality like filtering by geometry with distance or by attributes. For more information, see this sample: Filter SceneLayer with FeatureFilter.

Other helpful resources

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.