Skip to content

Commit 6045a1d

Browse files
New Sample: Show line of sight analysis on map (#1787)
Co-authored-by: Ian Malcolm <imalcolm@esri.com>
1 parent ebd9cfb commit 6045a1d

23 files changed

Lines changed: 1079 additions & 0 deletions

File tree

src/MAUI/Maui.Samples/ArcGIS.Samples.Maui.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<MauiImage Include="Resources\Icons\*.svg" />
106106
</ItemGroup>
107107
<ItemGroup>
108+
<EmbeddedResource Include="Resources\PictureMarkerSymbols\beacon.png" />
108109
<EmbeddedResource Include="Resources\PictureMarkerSymbols\pin_blue.png" />
109110
<EmbeddedResource Include="Resources\PictureMarkerSymbols\pin_star_blue.png" />
110111
</ItemGroup>
43.8 KB
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage x:Class="ArcGIS.Samples.ShowLineOfSightAnalysisOnMap.ShowLineOfSightAnalysisOnMap"
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Maui;assembly=Esri.ArcGISRuntime.Maui">
6+
<Grid Style="{DynamicResource EsriSampleContainer}">
7+
<esriUI:MapView x:Name="MyMapView" Style="{DynamicResource EsriSampleGeoView}" />
8+
<Border Style="{DynamicResource EsriSampleControlPanel}"
9+
VerticalOptions="Start"
10+
MaximumWidthRequest="620">
11+
<Grid>
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="Auto" />
14+
<RowDefinition Height="Auto" />
15+
<RowDefinition Height="250" />
16+
</Grid.RowDefinitions>
17+
<Grid Grid.Row="0" ColumnDefinitions="Auto,*">
18+
<CheckBox x:Name="VisibleOnlyCheckBox"
19+
Grid.Column="0"
20+
CheckedChanged="VisibleOnlyCheckBox_Changed" />
21+
<Label Grid.Column="1"
22+
Text="Show line of sight where target (circle) is visible from observer (triangle)"
23+
VerticalOptions="Center"
24+
LineBreakMode="WordWrap" />
25+
</Grid>
26+
<BoxView Grid.Row="1" HeightRequest="1" Color="Gray" Margin="0,4" />
27+
<ScrollView Grid.Row="2">
28+
<VerticalStackLayout x:Name="ResultsStackLayout" />
29+
</ScrollView>
30+
</Grid>
31+
</Border>
32+
<Label Text="Raster data Copyright Scottish Government and SEPA (2014)"
33+
Grid.RowSpan="2"
34+
Grid.ColumnSpan="2"
35+
HorizontalOptions="End"
36+
VerticalOptions="End"
37+
Margin="0,0,10,30"
38+
FontAttributes="Italic"
39+
FontSize="11"
40+
TextColor="White" />
41+
</Grid>
42+
</ContentPage>
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// Copyright 2026 Esri.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
5+
//
6+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
7+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
8+
// language governing permissions and limitations under the License.
9+
10+
using ArcGIS.Samples.Managers;
11+
using Esri.ArcGISRuntime.Analysis;
12+
using Esri.ArcGISRuntime.Analysis.Visibility;
13+
using Esri.ArcGISRuntime.Geometry;
14+
using Esri.ArcGISRuntime.Mapping;
15+
using Esri.ArcGISRuntime.Symbology;
16+
using Esri.ArcGISRuntime.UI;
17+
using Color = System.Drawing.Color;
18+
using Grid = Microsoft.Maui.Controls.Grid;
19+
20+
namespace ArcGIS.Samples.ShowLineOfSightAnalysisOnMap
21+
{
22+
[ArcGIS.Samples.Shared.Attributes.Sample(
23+
name: "Show line of sight analysis in map",
24+
category: "Analysis",
25+
description: "Perform a line of sight analysis in a map view between fixed observer and target positions.",
26+
instructions: "The sample loads with a map centered on the Isle of Arran, Scotland, and runs a line of sight analysis from multiple observer points (triangles) to a fixed target point (beacon icon) located at the highest point of the island. Solid green line segments represent visible portions of each line of sight result, and dashed gray segments represent not visible portions. For each observer, the information panel reports whether the target is visible and over what distance the line remains unobstructed. Use the checkbox in the panel to show only results where the target is visible from the observer.",
27+
tags: new[] { "analysis", "elevation", "line of sight", "map view", "spatial analysis", "terrain", "visibility" })]
28+
[ArcGIS.Samples.Shared.Attributes.OfflineData("aa97788593e34a32bcaae33947fdc271")]
29+
[ArcGIS.Samples.Shared.Attributes.EmbeddedResource(@"PictureMarkerSymbols\beacon.png")]
30+
public partial class ShowLineOfSightAnalysisOnMap : ContentPage
31+
{
32+
private const double RelativeHeightMeters = 5.0;
33+
34+
// Target position (radio mast/receiver location).
35+
private readonly MapPoint _targetPosition = new MapPoint(
36+
-577955.365, 7484288.220, RelativeHeightMeters,
37+
SpatialReferences.WebMercator);
38+
39+
// Observer positions with associated colors.
40+
private static readonly (Color Color, double X, double Y)[] ObserverSeeds =
41+
{
42+
(Color.Green, -580893.546, 7489102.890),
43+
(Color.Cyan, -583446.004, 7483567.462),
44+
(Color.Orange, -577665.236, 7490792.908),
45+
(Color.Yellow, -576452.981, 7487071.388),
46+
(Color.FromArgb(255, 228, 168, 239), -576650.067, 7481479.772), // light purple
47+
(Color.Blue, -571683.896, 7492017.864),
48+
};
49+
50+
// Graphics overlay for the analysis results.
51+
private GraphicsOverlay _resultsGraphicsOverlay;
52+
53+
// Symbols for visible and not-visible line segments.
54+
private readonly SimpleLineSymbol _visibleLineSymbol = new SimpleLineSymbol(
55+
SimpleLineSymbolStyle.Solid, Color.Green, 4);
56+
57+
private readonly SimpleLineSymbol _notVisibleLineSymbol = new SimpleLineSymbol(
58+
SimpleLineSymbolStyle.LongDash, Color.Gray, 2);
59+
60+
public ShowLineOfSightAnalysisOnMap()
61+
{
62+
InitializeComponent();
63+
_ = Initialize();
64+
}
65+
66+
// Set up the map, graphics overlays, and perform the line of sight analysis.
67+
private async Task Initialize()
68+
{
69+
// Create a map with a dark hillshade basemap and set the initial viewpoint to the Isle of Arran, Scotland.
70+
MyMapView.Map = new Map(BasemapStyle.ArcGISHillshadeDark)
71+
{
72+
InitialViewpoint = new Viewpoint(55.632572, -5.185883, DeviceInfo.Idiom == DeviceIdiom.Phone ? 200000 : 90000)
73+
};
74+
75+
// Create graphics overlays for results and position markers.
76+
_resultsGraphicsOverlay = new GraphicsOverlay();
77+
var positionsGraphicsOverlay = new GraphicsOverlay();
78+
MyMapView.GraphicsOverlays.Add(_resultsGraphicsOverlay);
79+
MyMapView.GraphicsOverlays.Add(positionsGraphicsOverlay);
80+
81+
try
82+
{
83+
// Create a beacon symbol from an embedded resource image for the target point graphic.
84+
string resourceStreamName = GetType().Assembly.GetManifestResourceNames().Single(str => str.EndsWith("beacon.png"));
85+
PictureMarkerSymbol beaconSymbol;
86+
using (Stream resourceStream = GetType().Assembly.GetManifestResourceStream(resourceStreamName))
87+
{
88+
beaconSymbol = await PictureMarkerSymbol.CreateAsync(resourceStream);
89+
beaconSymbol.Width = 24;
90+
beaconSymbol.Height = 24;
91+
}
92+
positionsGraphicsOverlay.Graphics.Add(new Graphic(_targetPosition, beaconSymbol));
93+
94+
// Add observer position graphics using triangle marker symbols.
95+
foreach (var seed in ObserverSeeds)
96+
{
97+
var observerPoint = new MapPoint(seed.X, seed.Y, RelativeHeightMeters, SpatialReferences.WebMercator);
98+
var observerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Triangle, seed.Color, 15);
99+
positionsGraphicsOverlay.Graphics.Add(new Graphic(observerPoint, observerSymbol));
100+
}
101+
102+
// Get the path to the locally stored elevation raster file.
103+
string rasterPath = DataManager.GetDataFolder("aa97788593e34a32bcaae33947fdc271", "arran.tif");
104+
105+
// Create a continuous field from the elevation raster file.
106+
var continuousField = await ContinuousField.CreateAsync(new[] { rasterPath }, 0);
107+
108+
// Create a line of sight position for the target.
109+
var targetPosition = new LineOfSightPosition(_targetPosition, HeightOrigin.Relative);
110+
111+
// Create line of sight positions for each observer.
112+
var observerPositions = ObserverSeeds.Select(seed =>
113+
new LineOfSightPosition(
114+
new MapPoint(seed.X, seed.Y, RelativeHeightMeters, SpatialReferences.WebMercator),
115+
HeightOrigin.Relative)).ToList();
116+
117+
// Create line of sight parameters with many-to-many observer-target pairs.
118+
var parameters = new LineOfSightParameters
119+
{
120+
ObserverTargetPairs = new ObserverTargetPairs(
121+
observerPositions, new[] { targetPosition })
122+
};
123+
124+
// Create line of sight function with the continuous field and parameters.
125+
var lineOfSightFunction = new LineOfSightFunction(continuousField, parameters);
126+
127+
// Evaluate the line of sight function.
128+
var results = await lineOfSightFunction.EvaluateAsync();
129+
130+
// Add result line graphics to the results graphics overlay.
131+
foreach (var result in results)
132+
{
133+
var targetVisibility = result.TargetVisibility;
134+
135+
if (result.VisibleLine != null)
136+
{
137+
var graphic = new Graphic(result.VisibleLine, _visibleLineSymbol);
138+
graphic.Attributes["TargetVisibility"] = targetVisibility;
139+
_resultsGraphicsOverlay.Graphics.Add(graphic);
140+
}
141+
142+
if (result.NotVisibleLine != null)
143+
{
144+
var graphic = new Graphic(result.NotVisibleLine, _notVisibleLineSymbol);
145+
graphic.Attributes["TargetVisibility"] = targetVisibility;
146+
_resultsGraphicsOverlay.Graphics.Add(graphic);
147+
}
148+
}
149+
150+
// Build observer summaries for the info panel.
151+
BuildResultsUI(results);
152+
}
153+
catch (Exception ex)
154+
{
155+
await Application.Current.Windows[0].Page.DisplayAlertAsync("Error", ex.Message, "OK");
156+
}
157+
}
158+
159+
// Build the results UI by programmatically adding rows to the VerticalStackLayout for each observer.
160+
private void BuildResultsUI(IReadOnlyList<LineOfSight> results)
161+
{
162+
for (int i = 0; i < results.Count; i++)
163+
{
164+
var result = results[i];
165+
166+
// Get the length of the visible line if it exists.
167+
var visibleLength = result.VisibleLine == null ? 0 :
168+
GeometryEngine.LengthGeodetic(result.VisibleLine, LinearUnits.Meters, GeodeticCurveType.Geodesic);
169+
170+
// Set the info text based on the results of the analysis.
171+
string infoText;
172+
if (result.Error != null)
173+
infoText = result.Error.Message;
174+
else if (result.NotVisibleLine == null)
175+
infoText = $"Target visible from observer over {visibleLength:F1} metres.";
176+
else
177+
infoText = $"Target not visible from observer. Obstructed after {visibleLength:F1} metres.";
178+
179+
// Update the UI.
180+
var color = ObserverSeeds[i].Color;
181+
182+
var row = new Grid
183+
{
184+
Margin = new Thickness(0, 2),
185+
ColumnDefinitions =
186+
{
187+
new ColumnDefinition(new GridLength(30)),
188+
new ColumnDefinition(GridLength.Star)
189+
}
190+
};
191+
192+
var rect = new BoxView
193+
{
194+
WidthRequest = 16,
195+
HeightRequest = 16,
196+
Color = Microsoft.Maui.Graphics.Color.FromRgba(color.R, color.G, color.B, color.A),
197+
HorizontalOptions = LayoutOptions.Start,
198+
Margin = new Thickness(4, 0, 0, 0)
199+
};
200+
Grid.SetColumn(rect, 0);
201+
202+
var label = new Label
203+
{
204+
Text = infoText,
205+
LineBreakMode = LineBreakMode.WordWrap,
206+
Margin = new Thickness(4, 2)
207+
};
208+
Grid.SetColumn(label, 1);
209+
210+
row.Children.Add(rect);
211+
row.Children.Add(label);
212+
ResultsStackLayout.Children.Add(row);
213+
}
214+
}
215+
216+
// Filter the result line graphics based on the checkbox state to show only visible lines.
217+
private void VisibleOnlyCheckBox_Changed(object sender, CheckedChangedEventArgs e)
218+
{
219+
if (_resultsGraphicsOverlay == null) return;
220+
221+
bool showVisibleOnly = VisibleOnlyCheckBox.IsChecked;
222+
223+
foreach (var graphic in _resultsGraphicsOverlay.Graphics)
224+
{
225+
// TargetVisibility value of 1.0 indicates the target is visible from the observer.
226+
bool isVisible = Convert.ToDouble(graphic.Attributes["TargetVisibility"]) == 1.0;
227+
graphic.IsVisible = !showVisibleOnly || isVisible;
228+
}
229+
}
230+
}
231+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Show line of sight analysis in map
2+
3+
Perform a line of sight analysis in a map view between fixed observer and target positions.
4+
5+
![Show line of sight analysis in map sample](showlineofsightanalysisonmap.jpg)
6+
7+
## Use case
8+
9+
Line of sight analysis determines whether a target can be seen from one or more observer locations based on elevation data. This can support planning workflows such as siting communication equipment, assessing observation coverage, or evaluating potential obstructions between known locations. In this sample, several predefined observer points are evaluated against a single fixed target to compare visibility outcomes side by side.
10+
11+
Note: This analysis is a form of "data-driven analysis", which means the analysis is calculated at the resolution of the data rather than the resolution of the display.
12+
13+
## How to use the sample
14+
15+
The sample loads with a map centered on the Isle of Arran, Scotland, and runs a line of sight analysis from multiple observer points (triangles) to a fixed target point (beacon icon) located at the highest point of the island. Solid green line segments represent visible portions of each line of sight result, and dashed gray segments represent not visible portions. For each observer, the information panel reports whether the target is visible and over what distance the line remains unobstructed. Use the checkbox in the panel to show only results where the target is visible from the observer.
16+
17+
## How it works
18+
19+
1. Create a `Map` and set it on a `MapView`.
20+
2. Create a `GraphicsOverlay` and add target and observer points to it, along with an appropriate symbol. Create another `GraphicsOverlay` that will display the line of sight result graphics.
21+
3. Create a `ContinuousField` from a raster file containing elevation data.
22+
4. Create a list of `LineOfSightPosition` from target and observer `MapPoint`s and a `HeightOrigin.Relative`.
23+
5. Configure `LineOfSightParameters` with a new `ObserverTargetPairs`, passing the list of observer and target line of sight positions.
24+
6. Create a `LineOfSightFunction` from the continuous field and line of sight parameters.
25+
7. Evaluate the function to get `LineOfSight` results.
26+
8. Create a `Graphic` from each result, using the geometry of the result's `VisibleLine` or `NotVisibleLine` result, and an appropriate symbol.
27+
9. Use `LineOfSight.TargetVisibility` to determine if the observer position has a direct line of sight to the target position.
28+
10. Check for `LineOfSight.Error`s and get the length of the visible line result with `GeometryEngine.LengthGeodetic` to report results.
29+
30+
## Relevant API
31+
32+
* ContinuousField
33+
* GeometryEngine
34+
* GraphicsOverlay
35+
* LineOfSight
36+
* LineOfSightFunction
37+
* LineOfSightParameters
38+
* LineOfSightPosition
39+
* ObserverTargetPairs
40+
41+
## About the data
42+
43+
The sample uses a [10m resolution digital terrain elevation raster of the Isle of Arran, Scotland](https://www.arcgis.com/home/item.html?id=aa97788593e34a32bcaae33947fdc271)
44+
(Raster data Copyright Scottish Government and SEPA (2014)).
45+
46+
## Tags
47+
48+
analysis, elevation, line of sight, map view, spatial analysis, terrain, visibility
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"category": "Analysis",
3+
"description": "Perform a line of sight analysis in a map view between fixed observer and target positions.",
4+
"formal_name": "ShowLineOfSightAnalysisOnMap",
5+
"ignore": false,
6+
"images": [
7+
"showlineofsightanalysisonmap.jpg"
8+
],
9+
"keywords": [
10+
"analysis",
11+
"elevation",
12+
"line of sight",
13+
"map view",
14+
"spatial analysis",
15+
"terrain",
16+
"visibility"
17+
],
18+
"offline_data": [],
19+
"redirect_from": [],
20+
"relevant_apis": [
21+
"ContinuousField",
22+
"GeometryEngine",
23+
"GraphicsOverlay",
24+
"LineOfSight",
25+
"LineOfSightFunction",
26+
"LineOfSightParameters",
27+
"LineOfSightPosition",
28+
"ObserverTargetPairs"
29+
],
30+
"snippets": [
31+
"ShowLineOfSightAnalysisOnMap.xaml.cs",
32+
"ShowLineOfSightAnalysisOnMap.xaml"
33+
],
34+
"title": "Show line of sight analysis in map"
35+
}
83 KB
Loading

src/MAUI/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [Show exploratory viewshed from geoelement in scene](Maui.Samples/Samples/Analysis/ShowExploratoryViewshedFromGeoelementInScene) - Analyze the exploratory viewshed for an object (GeoElement) in a scene.
1212
* [Show exploratory viewshed from point in scene](Maui.Samples/Samples/Analysis/ShowExploratoryViewshedFromPointInScene) - Perform an exploratory viewshed analysis from a defined vantage point.
1313
* [Show interactive viewshed with analysis overlay](Maui.Samples/Samples/Analysis/ShowInteractiveViewshedInAnalysisOverlay) - Perform an interactive viewshed analysis to determine visible and non-visible areas from a given observer position.
14+
* [Show line of sight analysis in map](Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysisOnMap) - Perform a line of sight analysis in a map view between fixed observer and target positions.
1415

1516
## Data
1617

src/Samples.Shared/Resources/FeaturedSamples.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<Sample>ApplyMapAlgebra</Sample>
66
<Sample>DistanceMeasurement</Sample>
77
<Sample>ShowInteractiveViewshedInAnalysisOverlay</Sample>
8+
<Sample>ShowLineOfSightAnalysisOnMap</Sample>
89
</Category>
910
<Category name="Data">
1011
<Sample>CreateKmlMultiTrack</Sample>
43.8 KB
Loading

0 commit comments

Comments
 (0)