-
Notifications
You must be signed in to change notification settings - Fork 531
Expand file tree
/
Copy pathShowLineOfSightAnalysisOnMap.xaml.cs
More file actions
231 lines (197 loc) · 11.2 KB
/
ShowLineOfSightAnalysisOnMap.xaml.cs
File metadata and controls
231 lines (197 loc) · 11.2 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2026 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 ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Analysis;
using Esri.ArcGISRuntime.Analysis.Visibility;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Color = System.Drawing.Color;
using Grid = Microsoft.Maui.Controls.Grid;
namespace ArcGIS.Samples.ShowLineOfSightAnalysisOnMap
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Show line of sight analysis in map",
category: "Analysis",
description: "Perform a line of sight analysis in a map view between fixed observer and target positions.",
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.",
tags: new[] { "analysis", "elevation", "line of sight", "map view", "spatial analysis", "terrain", "visibility" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData("aa97788593e34a32bcaae33947fdc271")]
[ArcGIS.Samples.Shared.Attributes.EmbeddedResource(@"PictureMarkerSymbols\beacon.png")]
public partial class ShowLineOfSightAnalysisOnMap : ContentPage
{
private const double RelativeHeightMeters = 5.0;
// Target position (radio mast/receiver location).
private readonly MapPoint _targetPosition = new MapPoint(
-577955.365, 7484288.220, RelativeHeightMeters,
SpatialReferences.WebMercator);
// Observer positions with associated colors.
private static readonly (Color Color, double X, double Y)[] ObserverSeeds =
{
(Color.Green, -580893.546, 7489102.890),
(Color.Cyan, -583446.004, 7483567.462),
(Color.Orange, -577665.236, 7490792.908),
(Color.Yellow, -576452.981, 7487071.388),
(Color.FromArgb(255, 228, 168, 239), -576650.067, 7481479.772), // light purple
(Color.Blue, -571683.896, 7492017.864),
};
// Graphics overlay for the analysis results.
private GraphicsOverlay _resultsGraphicsOverlay;
// Symbols for visible and not-visible line segments.
private readonly SimpleLineSymbol _visibleLineSymbol = new SimpleLineSymbol(
SimpleLineSymbolStyle.Solid, Color.Green, 4);
private readonly SimpleLineSymbol _notVisibleLineSymbol = new SimpleLineSymbol(
SimpleLineSymbolStyle.LongDash, Color.Gray, 2);
public ShowLineOfSightAnalysisOnMap()
{
InitializeComponent();
_ = Initialize();
}
// Set up the map, graphics overlays, and perform the line of sight analysis.
private async Task Initialize()
{
// Create a map with a dark hillshade basemap and set the initial viewpoint to the Isle of Arran, Scotland.
MyMapView.Map = new Map(BasemapStyle.ArcGISHillshadeDark)
{
InitialViewpoint = new Viewpoint(55.632572, -5.185883, DeviceInfo.Idiom == DeviceIdiom.Phone ? 200000 : 90000)
};
// Create graphics overlays for results and position markers.
_resultsGraphicsOverlay = new GraphicsOverlay();
var positionsGraphicsOverlay = new GraphicsOverlay();
MyMapView.GraphicsOverlays.Add(_resultsGraphicsOverlay);
MyMapView.GraphicsOverlays.Add(positionsGraphicsOverlay);
try
{
// Create a beacon symbol from an embedded resource image for the target point graphic.
string resourceStreamName = GetType().Assembly.GetManifestResourceNames().Single(str => str.EndsWith("beacon.png"));
PictureMarkerSymbol beaconSymbol;
using (Stream resourceStream = GetType().Assembly.GetManifestResourceStream(resourceStreamName))
{
beaconSymbol = await PictureMarkerSymbol.CreateAsync(resourceStream);
beaconSymbol.Width = 24;
beaconSymbol.Height = 24;
}
positionsGraphicsOverlay.Graphics.Add(new Graphic(_targetPosition, beaconSymbol));
// Add observer position graphics using triangle marker symbols.
foreach (var seed in ObserverSeeds)
{
var observerPoint = new MapPoint(seed.X, seed.Y, RelativeHeightMeters, SpatialReferences.WebMercator);
var observerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Triangle, seed.Color, 15);
positionsGraphicsOverlay.Graphics.Add(new Graphic(observerPoint, observerSymbol));
}
// Get the path to the locally stored elevation raster file.
string rasterPath = DataManager.GetDataFolder("aa97788593e34a32bcaae33947fdc271", "arran.tif");
// Create a continuous field from the elevation raster file.
var continuousField = await ContinuousField.CreateAsync(new[] { rasterPath }, 0);
// Create a line of sight position for the target.
var targetPosition = new LineOfSightPosition(_targetPosition, HeightOrigin.Relative);
// Create line of sight positions for each observer.
var observerPositions = ObserverSeeds.Select(seed =>
new LineOfSightPosition(
new MapPoint(seed.X, seed.Y, RelativeHeightMeters, SpatialReferences.WebMercator),
HeightOrigin.Relative)).ToList();
// Create line of sight parameters with many-to-many observer-target pairs.
var parameters = new LineOfSightParameters
{
ObserverTargetPairs = new ObserverTargetPairs(
observerPositions, new[] { targetPosition })
};
// Create line of sight function with the continuous field and parameters.
var lineOfSightFunction = new LineOfSightFunction(continuousField, parameters);
// Evaluate the line of sight function.
var results = await lineOfSightFunction.EvaluateAsync();
// Add result line graphics to the results graphics overlay.
foreach (var result in results)
{
var targetVisibility = result.TargetVisibility;
if (result.VisibleLine != null)
{
var graphic = new Graphic(result.VisibleLine, _visibleLineSymbol);
graphic.Attributes["TargetVisibility"] = targetVisibility;
_resultsGraphicsOverlay.Graphics.Add(graphic);
}
if (result.NotVisibleLine != null)
{
var graphic = new Graphic(result.NotVisibleLine, _notVisibleLineSymbol);
graphic.Attributes["TargetVisibility"] = targetVisibility;
_resultsGraphicsOverlay.Graphics.Add(graphic);
}
}
// Build observer summaries for the info panel.
BuildResultsUI(results);
}
catch (Exception ex)
{
await Application.Current.Windows[0].Page.DisplayAlertAsync("Error", ex.Message, "OK");
}
}
// Build the results UI by programmatically adding rows to the VerticalStackLayout for each observer.
private void BuildResultsUI(IReadOnlyList<LineOfSight> results)
{
for (int i = 0; i < results.Count; i++)
{
var result = results[i];
// Get the length of the visible line if it exists.
var visibleLength = result.VisibleLine == null ? 0 :
GeometryEngine.LengthGeodetic(result.VisibleLine, LinearUnits.Meters, GeodeticCurveType.Geodesic);
// Set the info text based on the results of the analysis.
string infoText;
if (result.Error != null)
infoText = result.Error.Message;
else if (result.NotVisibleLine == null)
infoText = $"Target visible from observer over {visibleLength:F1} metres.";
else
infoText = $"Target not visible from observer. Obstructed after {visibleLength:F1} metres.";
// Update the UI.
var color = ObserverSeeds[i].Color;
var row = new Grid
{
Margin = new Thickness(0, 2),
ColumnDefinitions =
{
new ColumnDefinition(new GridLength(30)),
new ColumnDefinition(GridLength.Star)
}
};
var rect = new BoxView
{
WidthRequest = 16,
HeightRequest = 16,
Color = Microsoft.Maui.Graphics.Color.FromRgba(color.R, color.G, color.B, color.A),
HorizontalOptions = LayoutOptions.Start,
Margin = new Thickness(4, 0, 0, 0)
};
Grid.SetColumn(rect, 0);
var label = new Label
{
Text = infoText,
LineBreakMode = LineBreakMode.WordWrap,
Margin = new Thickness(4, 2)
};
Grid.SetColumn(label, 1);
row.Children.Add(rect);
row.Children.Add(label);
ResultsStackLayout.Children.Add(row);
}
}
// Filter the result line graphics based on the checkbox state to show only visible lines.
private void VisibleOnlyCheckBox_Changed(object sender, CheckedChangedEventArgs e)
{
if (_resultsGraphicsOverlay == null) return;
bool showVisibleOnly = VisibleOnlyCheckBox.IsChecked;
foreach (var graphic in _resultsGraphicsOverlay.Graphics)
{
// TargetVisibility value of 1.0 indicates the target is visible from the observer.
bool isVisible = Convert.ToDouble(graphic.Attributes["TargetVisibility"]) == 1.0;
graphic.IsVisible = !showVisibleOnly || isVisible;
}
}
}
}