Show line of sight between points

View on GitHub

Perform a line of sight analysis between two points in real time.

Image of show line of sight between points

Use case

A line of sight analysis can be used to assess whether a view is obstructed between an observer and a target. Obstructing features could either be natural, like topography, or man-made, like buildings. Consider an events planning company wanting to commemorate a national event by lighting sequential beacons across hill summits or roof tops. To guarantee a successful event, ensuring an unobstructed line of sight between neighboring beacons would allow each beacon to be activated as intended.

How to use the sample

Tap on the map to set the observer location. Tap and hold to set the line of sight target. A red segment on the line means the view between observer and target is obstructed, whereas green means the view is unobstructed.

How it works

  1. Create an AnalysisOverlay and add it to the scene view.
  2. Create a LocationLineOfSight with initial observer and target locations and add it to the analysis overlay.
  3. Track the screen taps using the onSingleTapGesture and onLongPressGesture.
  4. Use the scene point to update the target location with lineOfSight.targetLocation and the observer location with lineOfSight.observerLocation.

Relevant API

  • AnalysisOverlay
  • LocationLineOfSight
  • SceneView

Tags

3D, line of sight, visibility, visibility analysis

Sample Code

ShowLineOfSightBetweenPointsView.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
// 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
//
//   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 SwiftUI
import ArcGIS

struct ShowLineOfSightBetweenPointsView: View {
    /// A scene with an imagery basemap and centered on mountains in Chile.
    @State private var scene: ArcGIS.Scene = {
        // Creates a scene.
        let scene = Scene(basemapStyle: .arcGISImagery)

        // Add elevation source to the base surface of the scene with the service URL.
        let elevationSource = ArcGISTiledElevationSource(url: .worldElevationService)
        scene.baseSurface.addElevationSource(elevationSource)

        // Set scene to the viewpoint specified by the camera position.
        let point = Point(x: -73.0870, y: -49.3460, z: 5046, spatialReference: .wgs84)
        let camera = Camera(location: point, heading: 11, pitch: 62, roll: 0)
        scene.initialViewpoint = Viewpoint(boundingGeometry: point, camera: camera)

        return scene
    }()

    /// The analysis overlay for the line of sight analysis.
    @State private var analysisOverlay = AnalysisOverlay()

    /// The line of sight analysis.
    @State private var lineOfSight: LocationLineOfSight?

    /// A Boolean value indicating whether to show the target instructions.
    @State private var shouldShowTargetInstruction = false

    var body: some View {
        SceneView(scene: scene, analysisOverlays: [analysisOverlay])
            .onSingleTapGesture { _, scenePoint in
                // User tapped to place line of sight observer.
                guard let scenePoint else { return }
                if lineOfSight == nil {
                    // Create and set initial line of sight analysis with tapped scene point.
                    lineOfSight = LocationLineOfSight(observerLocation: scenePoint, targetLocation: scenePoint)
                    analysisOverlay.addAnalysis(lineOfSight!)
                    shouldShowTargetInstruction = true
                } else {
                    // Update the observer location.
                    lineOfSight?.observerLocation = scenePoint
                }
            }
            .onLongPressGesture {  _, scenePoint in
                // Update the target location on long press.
                guard let scenePoint else { return }
                lineOfSight?.targetLocation = scenePoint
            }
            .overlay(alignment: .top) {
                // Instruction texts.
                VStack {
                    Text("Tap on the map to set the observer location.")
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(8)
                        .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)

                    Text("Tap and hold to set the line of sight target.")
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(8)
                        .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)
                        .padding(.top, -8)
                        .opacity(shouldShowTargetInstruction ? 1 : 0)
                }
            }
    }
}

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

#Preview {
    ShowLineOfSightBetweenPointsView()
}

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