Filter features in scene

View inMAUIWPFWinUIView on GitHub

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

filterfeaturesinscene

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 the 3D buildings OpenStreetMap layer. Tap the "Load detailed buildings" button to load an additional scene layer that contains more detailed buildings. Notice how the two scene layers overlap and clip into each other. Tap the "Filter OSM buildings in extent" 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. Tap the "Reset scene" button to hide the detailed buildings scene layer and clear the OSM buildings filter.

How it works

  1. Add BaseLayers to a Basemap for the scene using a topographic ArcGISVectorTileLayer and the OpenStreetMap 3D Buildings ArcGISSceneLayer as baselayers.
  2. Create a Surface for the scene and set the World Elevation 3D as an elevation source.
  3. Add the 3D San Francisco Buildings ArcGISSceneLayer to the scene's operational layers.
  4. Construct a SceneLayerPolygonFilter with the extent of the San Francisco Buildings Scene Layer and the SceneLayerPolygonFilterSpatialRelationship.Disjoint enum to hide all features within the extent.
  5. 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 the SceneLayerPolygonFilterSpatialRelationship.Contains enum to only show features within the extent of the geometry.

You can also show or hide features in a scene layer using ArcGISSceneLayer.SetFeatureVisible() or SetFeaturesVisible() 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

FilterFeaturesInScene.xaml.csFilterFeaturesInScene.xaml.csFilterFeaturesInScene.xaml
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
155
156
157
158
159
160
161
// Copyright 2023 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.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;

namespace ArcGIS.Samples.FilterFeaturesInScene
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Filter features in scene",
        category: "Scene",
        description: "Filter 3D scene features out of a given geometry with a polygon filter.",
        instructions: "The sample initializes showing the 3D buildings OpenStreetMap layer. Tap the \"Load detailed buildings\" button to load an additional scene layer that contains more detailed buildings. Notice how the two scene layers overlap and clip into each other. Tap the \"Filter OSM buildings in extent\" 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. Tap the \"Reset scene\" button to hide the detailed buildings scene layer and clear the OSM buildings filter.",
        tags: new[] { "3D", "OSM", "buildings", "disjoint", "exclude", "extent", "filter", "hide", "polygon" })]
    [ArcGIS.Samples.Shared.Attributes.OfflineData()]
    public partial class FilterFeaturesInScene
    {
        // ArcGIS Online services.
        private const string OsmTopographic = "https://www.arcgis.com/home/item.html?id=1e7d1784d1ef4b79ba6764d0bd6c3150";
        private const string OsmBuildings = "https://www.arcgis.com/home/item.html?id=ca0470dbbddb4db28bad74ed39949e25";
        private const string ElevationSource = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer";
        private const string DetailedBuildings = "https://tiles.arcgis.com/tiles/z2tnIkrLQ2BRzr6P/arcgis/rest/services/SanFrancisco_Bldgs/SceneServer";

        private ArcGISSceneLayer _detailedBuildingsSceneLayer;
        private ArcGISSceneLayer _osmBuildingSceneLayer;

        private SceneLayerPolygonFilter _sceneLayerPolygonFilter;
        private Graphic _cityExtentGraphic;
        private Geometry _sceneLayerExtentPolygon;

        // String to keep track of which step user is on.
        private static string _step;

        public FilterFeaturesInScene()
        {
            InitializeComponent();
            _ = Initialize();
        }

        private async Task Initialize()
        {
            MySceneView.Scene = new Scene();

            // Add base layers to the scene's basemap using OSM layers from AGOL.
            MySceneView.Scene.Basemap.BaseLayers.Add(new ArcGISVectorTiledLayer(new Uri(OsmTopographic)));
            MySceneView.Scene.Basemap.BaseLayers.Add(_osmBuildingSceneLayer = new ArcGISSceneLayer(new Uri(OsmBuildings)));

            // Create a surface with an elevation source for the scene.
            var surface = new Surface();
            surface.ElevationSources.Add(new ArcGISTiledElevationSource(new Uri(ElevationSource)));
            MySceneView.Scene.BaseSurface = surface;

            // Create a scene layer for the detailed buildings.
            _detailedBuildingsSceneLayer = new ArcGISSceneLayer(new Uri(DetailedBuildings));
            await _detailedBuildingsSceneLayer.LoadAsync();

            // Construct a red polygon that shows the extent of the detailed buildings scene layer.
            Envelope cityExtent = _detailedBuildingsSceneLayer.FullExtent;
            var builder = new PolygonBuilder(MySceneView.SpatialReference);
            builder.AddPoint(cityExtent.XMin, cityExtent.YMin);
            builder.AddPoint(cityExtent.XMax, cityExtent.YMin);
            builder.AddPoint(cityExtent.XMax, cityExtent.YMax);
            builder.AddPoint(cityExtent.XMin, cityExtent.YMax);

            _sceneLayerExtentPolygon = builder.ToGeometry();

            // Create the SceneLayerPolygonFilter to later apply to the OSM buildings layer.
            _sceneLayerPolygonFilter = new SceneLayerPolygonFilter(new List<Polygon>() { builder.ToGeometry() }, SceneLayerPolygonFilterSpatialRelationship.Disjoint);

            // Create the extent graphic so we can add it later with the detailed buildings scene layer.
            var simpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Red, 5.0f);
            var simpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.Transparent, simpleLineSymbol);
            _cityExtentGraphic = new Graphic(_sceneLayerExtentPolygon, simpleFillSymbol);

            MySceneView.GraphicsOverlays.Add(new GraphicsOverlay());

            LoadScene();

            await MySceneView.SetViewpointCameraAsync(new Camera(new MapPoint(-122.421, 37.7041, 207), 60, 70, 0));
        }

        // Determine which step of the sample user is on.
        private void MyButton_Click(object sender, EventArgs e)
        {
            switch (_step)
            {
                case "LoadScene":
                    LoadScene();
                    break;

                case "FilterScene":
                    FilterScene();
                    break;

                case "ResetScene":
                    ResetScene();
                    break;
            }
        }

        // Show the detailed buildings scene layer and extent graphic.
        private void LoadScene()
        {
            _step = "FilterScene";

            // Update the label to reflect next step.
            MyButton.Text = "Filter OSM buildings in extent";

            // Show the detailed buildings scene layer and the city extent graphic.
            MySceneView.Scene.OperationalLayers.Add(_detailedBuildingsSceneLayer);
            MySceneView.GraphicsOverlays.FirstOrDefault().Graphics.Add(_cityExtentGraphic);
        }

        // Hide buildings within the detailed building extent so they don't clip.
        private void FilterScene()
        {
            _step = "ResetScene";

            // Update label to reflect next step.
            MyButton.Text = "Reset scene";

            // Initially, the building layer does not have a polygon filter, set it.
            if (_osmBuildingSceneLayer.PolygonFilter == null)
            {
                _osmBuildingSceneLayer.PolygonFilter = _sceneLayerPolygonFilter;
            }
            // After the scene is reset, the layer will have a polygon filter, but that filter will not have polygons set.
            // Add the polygon back to the polygon filter.
            else
            {
                _sceneLayerPolygonFilter.Polygons.Add((Polygon)_sceneLayerExtentPolygon);
            }
        }

        // Reset the scene to its original state.
        private void ResetScene()
        {
            _step = "LoadScene";

            // Update label to reflect next step.
            MyButton.Text = "Load detailed buildings";

            // Remove the detailed buildings layer from the scene.
            MySceneView.Scene.OperationalLayers.Clear();

            // Clear the OSM buildings polygon filter polygons list.
            _osmBuildingSceneLayer.PolygonFilter.Polygons.Clear();

            // Clear the graphics list in the graphics overlay to remove the red extent boundary graphic.
            MySceneView.GraphicsOverlays.FirstOrDefault().Graphics.Clear();
        }
    }
}

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