Style point with distance composite scene symbol

View on GitHub

Change a graphic's symbol based on the camera's proximity to it.

Image of style point with distance composite scene symbol

Use case

When showing dense datasets, it is beneficial to reduce the detail of individual points when zooming out to avoid visual clutter and to avoid data points overlapping and obscuring each other.

How to use the sample

The sample starts looking at a plane. Zoom out from the plane to see it turn into a cone. Keeping zooming out and it will turn into a point.

How it works

  1. Create a GraphicsOverlay object and add it to a SceneView.
  2. Create a DistanceCompositeSceneSymbol object.
  3. Create DistanceSymbolRange objects specifying a Symbol and the min and max distance within which the symbol should be visible.
  4. Add the ranges to the range collection of the distance composite scene symbol.
  5. Create a Graphic object with the distance composite scene symbol at a location and add it to the graphics overlay.

Relevant API

  • DistanceCompositeSceneSymbol
  • DistanceSymbolRange
  • OrbitGeoElementCameraController

Tags

3D, data, graphic

Sample Code

StylePointWithDistanceCompositeSceneSymbolView.swift
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
// Copyright 2024 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
//
//   https://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.

import ArcGIS
import SwiftUI

struct StylePointWithDistanceCompositeSceneSymbolView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// The distance from the target object to the camera in meters.
    @State private var cameraDistance = Measurement(value: 0, unit: UnitLength.meters)

    var body: some View {
        SceneView(
            scene: model.scene,
            cameraController: model.cameraController,
            graphicsOverlays: [model.graphicsOverlay]
        )
        .overlay(alignment: .top) {
            VStack {
                Text("Zoom in and out to see the symbol change.")
                    .frame(maxWidth: .infinity)
                    .padding(8)
                    .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)

                HStack {
                    Spacer()
                    HStack {
                        Text("Distance:")
                        Text(cameraDistance, format: .measurement(
                            width: .narrow,
                            usage: .asProvided,
                            numberFormatStyle: .number.precision(.fractionLength(0))
                        ))
                    }
                    .padding()
                    .background(.ultraThinMaterial)
                    .clipShape(.rect(cornerRadius: 10))
                    .shadow(radius: 3)
                }
                .padding(.trailing, 8)
            }
        }
        .task {
            for await newDistance in model.cameraController.$cameraDistance {
                cameraDistance.value = newDistance
            }
        }
    }
}

private extension StylePointWithDistanceCompositeSceneSymbolView {
    /// The view model for the sample.
    class Model: ObservableObject {
        /// A scene with an imagery basemap and world elevation surface.
        let scene: ArcGIS.Scene = {
            let scene = Scene(basemapStyle: .arcGISImagery)
            let elevationSource = ArcGISTiledElevationSource(url: .worldElevationService)
            scene.baseSurface.addElevationSource(elevationSource)
            return scene
        }()

        /// The camera controller focused on the plane graphic.
        private(set) var cameraController: OrbitGeoElementCameraController!

        /// The graphics overlay for the plane graphic.
        let graphicsOverlay: GraphicsOverlay = {
            let graphicsOverlay = GraphicsOverlay()
            graphicsOverlay.sceneProperties.surfacePlacement = .relative
            return graphicsOverlay
        }()

        /// The plane graphic created from a distance composite symbol.
        private let planeGraphic: Graphic = {
            // Create the different symbols.
            let planeSymbol = ModelSceneSymbol(url: .bristol, scale: 100)
            let coneSymbol = SimpleMarkerSceneSymbol.cone(
                color: .red,
                diameter: 200,
                height: 600,
                anchorPosition: .center
            )
            coneSymbol.pitch = -90.0
            let circleSymbol = SimpleMarkerSymbol(style: .circle, color: .red, size: 10)

            // Create a distance composite symbol using the symbols.
            let distanceCompositeSymbol = DistanceCompositeSceneSymbol()
            distanceCompositeSymbol.addRange(
                DistanceSymbolRange(symbol: planeSymbol, maxDistance: 10000)
            )
            distanceCompositeSymbol.addRange(
                DistanceSymbolRange(symbol: coneSymbol, minDistance: 10001, maxDistance: 30000)
            )
            distanceCompositeSymbol.addRange(
                DistanceSymbolRange(symbol: circleSymbol, minDistance: 30001)
            )

            // Create a graphic using the distance composite symbol.
            let planePosition = Point(x: -2.708, y: 56.096, z: 5000, spatialReference: .wgs84)
            let planeGraphic = Graphic(geometry: planePosition, symbol: distanceCompositeSymbol)

            return planeGraphic
        }()

        init() {
            graphicsOverlay.addGraphic(planeGraphic)

            // Create the camera controller targeted on the plane graphic.
            cameraController = {
                let cameraController = OrbitGeoElementCameraController(
                    target: planeGraphic,
                    distance: 4000
                )
                cameraController.cameraPitchOffset = 80
                cameraController.cameraHeadingOffset = -30
                return cameraController
            }()
        }
    }
}

private extension URL {
    /// A URL to the local Bristol 3D model file.
    static var bristol: URL {
        Bundle.main.url(forResource: "Bristol", withExtension: "dae", subdirectory: "Bristol")!
    }

    /// A world elevation service from the Terrain3D ArcGIS REST service.
    static var worldElevationService: URL {
        URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
    }
}

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