Filter features in scene

View on GitHubSample viewer app

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

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 the 3D buildings OpenStreetMap layer. Click 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. Click 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. Click the "Reset scene" button to hide the detailed buildings scene layer and clear the OSM buildings filter.

How it works

  1. Construct a Basemap for the scene using a topographic ArcGISVectorTileLayer and the OpenStreetMap 3D BuildingsArcGISSceneLayer 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 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 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.cppFilterFeaturesInScene.cppFilterFeaturesInScene.hFilterFeaturesInScene.qml
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
162
163
164
165
166
167
168
169
170
171
172
// [WriteFile Name=FilterFeaturesInScene, Category=Scenes]
// [Legal]
// 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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

// sample headers
#include "FilterFeaturesInScene.h"

// ArcGIS Maps SDK headers
#include "ArcGISSceneLayer.h"
#include "ArcGISTiledElevationSource.h"
#include "ArcGISVectorTiledLayer.h"
#include "Basemap.h"
#include "Camera.h"
#include "ElevationSourceListModel.h"
#include "Envelope.h"
#include "Error.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "LayerListModel.h"
#include "MapTypes.h"
#include "Point.h"
#include "Polygon.h"
#include "PolygonBuilder.h"
#include "PortalItem.h"
#include "Scene.h"
#include "SceneLayerPolygonFilter.h"
#include "SceneQuickView.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SpatialReference.h"
#include "Surface.h"
#include "SymbolTypes.h"

// Qt headers
#include <QFuture>

using namespace Esri::ArcGISRuntime;

FilterFeaturesInScene::FilterFeaturesInScene(QObject* parent /* = nullptr */):
  QObject(parent)
{
  // Construct and set the basemap
  ArcGISVectorTiledLayer* vectorTileLayer = new ArcGISVectorTiledLayer(new PortalItem("1e7d1784d1ef4b79ba6764d0bd6c3150", this), this);
  m_osmBuildings = new ArcGISSceneLayer(new PortalItem("ca0470dbbddb4db28bad74ed39949e25", this), this);
  Basemap* basemap = new Basemap(this);
  basemap->baseLayers()->append(vectorTileLayer);
  basemap->baseLayers()->append(m_osmBuildings);
  m_scene = new Scene(basemap, this);

  // Construct and set the scene topography
  Surface* surface = new Surface(this);
  surface->elevationSources()->append(new ArcGISTiledElevationSource(QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this));
  m_scene->setBaseSurface(surface);

  // Construct the detailed buildings scene layer
  m_detailedBuildingsSceneLayer = new ArcGISSceneLayer(QUrl("https://tiles.arcgis.com/tiles/z2tnIkrLQ2BRzr6P/arcgis/rest/services/SanFrancisco_Bldgs/SceneServer"), this);

  connect(m_detailedBuildingsSceneLayer, &ArcGISSceneLayer::doneLoading, this, [this](const Error& error)
  {
    if (!error.isEmpty())
    {
      qWarning() << "San Francisco Buildings Layer failed to load." << error.message() << error.additionalMessage();
      return;
    }

    // Construct a red polygon that shows the extent of the detailed buildings scene layer
    const Envelope sfExtent = m_detailedBuildingsSceneLayer->fullExtent();

    PolygonBuilder* builder = new PolygonBuilder(m_sceneView->spatialReference(), this);
    builder->addPoint(sfExtent.xMin(), sfExtent.yMin());
    builder->addPoint(sfExtent.xMax(), sfExtent.yMin());
    builder->addPoint(sfExtent.xMax(), sfExtent.yMax());
    builder->addPoint(sfExtent.xMin(), sfExtent.yMax());

    m_sceneLayerExtentPolygon = builder->toPolygon();

    // Create the SceneLayerPolygonFilter to later apply to the OSM buildings layer
    m_sceneLayerPolygonFilter = new SceneLayerPolygonFilter(
          QList<Polygon>{m_sceneLayerExtentPolygon}, // polygons to filter by
          SceneLayerPolygonFilterSpatialRelationship::Disjoint, // hide all features within the polygons
          this);

    // Create the extent graphic so we can add it later with the detailed buildings scene layer
    SimpleFillSymbol* sfs = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor(Qt::transparent), new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(Qt::red), 5.0f, this), this);
    m_sanFranciscoExtentGraphic = new Graphic(m_sceneLayerExtentPolygon, sfs, this);
  });

  m_detailedBuildingsSceneLayer->load();
}

FilterFeaturesInScene::~FilterFeaturesInScene() = default;

void FilterFeaturesInScene::init()
{
  // Register the scene view for QML
  qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView");
  qmlRegisterType<FilterFeaturesInScene>("Esri.Samples", 1, 0, "FilterFeaturesInSceneSample");
}

void FilterFeaturesInScene::loadScene()
{
  // Show the detailed buildings scene layer and the extent graphic
  m_scene->operationalLayers()->append(m_detailedBuildingsSceneLayer);
  m_sceneView->graphicsOverlays()->first()->graphics()->append(m_sanFranciscoExtentGraphic);
}

void FilterFeaturesInScene::filterScene()
{
  // Hide buildings within the detailed building extent so they don't clip

  // Initially, the building layer does not have a polygon filter, set it
  if (!m_osmBuildings->polygonFilter())
    m_osmBuildings->setPolygonFilter(m_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
    m_sceneLayerPolygonFilter->setPolygons({m_sceneLayerExtentPolygon});
}

void FilterFeaturesInScene::reset()
{
  // Remove the detailed buildings layer from the scene
  m_scene->operationalLayers()->clear();

  // Set the OSM buildings polygon filter to an empty list of polygons to clear the filter
  m_osmBuildings->polygonFilter()->setPolygons(QList<Polygon>{});

  // Clear the graphics list in the graphics overlay to remove the red extent boundary graphic
  m_sceneView->graphicsOverlays()->first()->graphics()->clear();
}

SceneQuickView* FilterFeaturesInScene::sceneView() const
{
  return m_sceneView;
}

// Set the view (created in QML)
void FilterFeaturesInScene::setSceneView(SceneQuickView* sceneView)
{
  if (!sceneView || sceneView == m_sceneView)
    return;

  m_sceneView = sceneView;
  m_sceneView->setArcGISScene(m_scene);

  // Set the viewpoint to San Francisco
  m_sceneView->setViewpointCameraAsync(Camera(Point(-122.421, 37.7041, 207), 60, 70, 0));

  // Add a graphics overlay to display the extent graphic
  m_sceneView->graphicsOverlays()->append(new GraphicsOverlay(this));

  emit sceneViewChanged();
}

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