|
| 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 | +} |
0 commit comments