Filter features in scene

View on GitHubSample viewer app

Filter 3D scene features out of a given geometry with a polygon filter.

Filter features in scene screenshot

Use case

You can directly control what users see within a specific scene view to give a more focused or cleaner user experience by using a SceneLayerPolygonFilter to selectively show or hide scene features within a given area.

How to use the sample

The sample initializes showing overlapping datasets of 3D buildings from the OpenStreetMap layer and an additional detailed scene layer of buildings in San Francisco. Notice how the two scene layers overlap and clip into each other. Click the "Filter OSM buildings" button, to set a SceneLayerPolygonFilter and filter out the OpenStreetMap buildings within the extent of the detailed buildings scene. Notice how the OSM buildings within and intersecting the extent of the detailed buildings layer are hidden.

How it works

  1. Construct an ArcGISScene and add a Surface elevation source set to the World Elevation 3D as an elevation source.
  2. Add the two ArcGISSceneLayers building scene layers to the ArcGISScenes operational layers.
  3. Construct a SceneLayerPolygonFilter with the extent of the San Francisco Buildings Scene Layer and the SceneLayerPolygonFilterSpatialRelationship.Disjoint object to hide all features within the extent.
  4. Set the SceneLayerPolygonFilter on the OSM Buildings layer to hide all OSM buildings within the extent of the San Francisco Buildings layer.

Relevant API

  • ArcGISSceneLayer
  • SceneLayerPolygonFilter
  • SceneLayerPolygonFilterSpatialRelationship

About the data

This sample uses the OpenStreetMap 3D Buildings which provides generic 3D outlines of buildings throughout the world. It is based on the OSM Daylight map distribution and is hosted by Esri. It uses the San Francisco 3D Buildings scene layer which provides detailed 3D models of buildings in San Francisco, California, USA.

Additional information

This sample uses SceneLayerPolygonFilterSpatialRelationship.Disjoint to hide all features within the extent of the given geometry. You can alternatively use SceneLayerPolygonFilterSpatialRelationship.Contains to only show features within the extent of the geometry.

You can also show or hide features in a scene layer using ArcGISSceneLayer.setFeatureVisible(...) and pass in a feature or list of features and a boolean value to set their visibility.

Tags

3D, buildings, disjoint, exclude, extent, filter, hide, OSM, polygon

Sample Code

FilterFeaturesInSceneViewModel.ktFilterFeaturesInSceneViewModel.ktMainActivity.ktFilterFeaturesInSceneScreen.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Copyright 2025 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgismaps.sample.filterfeaturesinscene.components

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.Color
import com.arcgismaps.geometry.Polygon
import com.arcgismaps.geometry.PolygonBuilder
import com.arcgismaps.mapping.ArcGISScene
import com.arcgismaps.mapping.ArcGISTiledElevationSource
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.layers.ArcGISSceneLayer
import com.arcgismaps.mapping.layers.SceneLayerPolygonFilter
import com.arcgismaps.mapping.layers.SceneLayerPolygonFilterSpatialRelationship
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleLineSymbol
import com.arcgismaps.mapping.symbology.SimpleLineSymbolStyle
import com.arcgismaps.mapping.view.Camera
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.arcgismaps.portal.Portal
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch

class FilterFeaturesInSceneViewModel(application: Application) : AndroidViewModel(application) {

    // create a ViewModel to handle dialog interactions
    val messageDialogVM: MessageDialogViewModel = MessageDialogViewModel()

    // Create an OSM Buildings ArcGISSceneLayer from a portal item
    private val osmBuildingsSceneLayer = ArcGISSceneLayer(
        PortalItem(
            portal = Portal("https://www.arcgis.com"), itemId = "ca0470dbbddb4db28bad74ed39949e25"
        )
    )

    // Create a San Francisco Buildings ArcGISSceneLayer from a url
    private val sanFranciscoBuildingsSceneLayer =
        ArcGISSceneLayer(uri = "https://tiles.arcgis.com/tiles/z2tnIkrLQ2BRzr6P/arcgis/rest/services/SanFrancisco_Bldgs/SceneServer")

    // Create a new ArcGISScene and set a basemap from a portal item with a vector tile layer
    val arcGISScene: ArcGISScene by mutableStateOf(ArcGISScene(BasemapStyle.ArcGISTopographic).apply {
        // Add an elevation source to the scene's base surface.
        val tiledElevationSource =
            ArcGISTiledElevationSource(uri = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")
        baseSurface.elevationSources.add(tiledElevationSource)
        // OSM building layer
        operationalLayers.add(osmBuildingsSceneLayer)
        // Add the buildings scene layer to the operational layers
        operationalLayers.add(sanFranciscoBuildingsSceneLayer)
        // Set the initial viewpoint of the scene to San Francisco
        initialViewpoint = Viewpoint(
            latitude = 37.7041,
            longitude = -122.421,
            1000.0,
            Camera(
                latitude = 37.7041,
                longitude = -122.421,
                altitude = 207.0,
                heading = 60.0,
                pitch = 70.0,
                roll = 0.0
            )
        )
    })

    // Define a red boundary graphic
    private val boundaryGraphic = Graphic(
        symbol = SimpleFillSymbol(
            style = SimpleFillSymbolStyle.Solid,
            color = Color.transparent,
            outline = SimpleLineSymbol(
                style = SimpleLineSymbolStyle.Solid,
                color = Color.red,
                width = 5.0f
            )
        )
    )

    // Set up graphic overlay and graphic for the San Francisco buildings layer extent.
    val graphicsOverlay = GraphicsOverlay(listOf(boundaryGraphic))

    init {
        loadBuildingsLayer()
    }

    /**
     * Load the San Francisco buildings layer and create a polygon boundary using the layer extent.
     */
    private fun loadBuildingsLayer() {
        viewModelScope.launch {
            // Load the San Francisco buildings layer
            sanFranciscoBuildingsSceneLayer.load().onFailure {
                messageDialogVM.showMessageDialog(it.message.toString(), it.cause.toString())
            }
            // Create a polygon boundary using the San Francisco buildings layer extent
            sanFranciscoBuildingsSceneLayer.fullExtent?.let {
                boundaryGraphic.geometry = PolygonBuilder().apply {
                    addPoint(it.xMin, it.yMin)
                    addPoint(it.xMax, it.yMin)
                    addPoint(it.xMax, it.yMax)
                    addPoint(it.xMin, it.yMax)
                }.toGeometry()
            }
        }
    }

    /**
     * Filter the OSM buildings layer to only show buildings that are disjoint from the San Francisco buildings layer
     * extent.
     */
    fun filterScene() {
        // Check that the San Francisco buildings layer extent is available
        (boundaryGraphic.geometry as? Polygon)?.let { boundary ->
            // Create a polygon filter with the San Francisco buildings layer boundary polygon and set it to be disjoint
            val sceneLayerPolygonFilter =
                SceneLayerPolygonFilter(
                    polygons = listOf(boundary),
                    spatialRelationship = SceneLayerPolygonFilterSpatialRelationship.Disjoint
                )
            // Set the polygon filter to the OSM buildings layer
            osmBuildingsSceneLayer.polygonFilter = sceneLayerPolygonFilter
        }
    }

    /**
     * Reset the OSM buildings layer filter to show all buildings.
     */
    fun resetFilter() {
        // Clear all polygon filters
        osmBuildingsSceneLayer.polygonFilter?.polygons?.clear()
    }
}

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