-
Notifications
You must be signed in to change notification settings - Fork 531
New Sample: Show line of sight analysis on map #1787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e85d8df
New Sample: Show line of sight analysis on map
praveenaak 75a5ed6
Update src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysis…
praveenaak eaaf3b9
Update src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysis…
praveenaak cb13a01
Update src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysis…
praveenaak c2728d1
Update src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysis…
praveenaak bfbe346
Apply code-readability suggestions
praveenaak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions
42
...i.Samples/Samples/Analysis/ShowLineOfSightAnalysisOnMap/ShowLineOfSightAnalysisOnMap.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <ContentPage x:Class="ArcGIS.Samples.ShowLineOfSightAnalysisOnMap.ShowLineOfSightAnalysisOnMap" | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Maui;assembly=Esri.ArcGISRuntime.Maui"> | ||
| <Grid Style="{DynamicResource EsriSampleContainer}"> | ||
| <esriUI:MapView x:Name="MyMapView" Style="{DynamicResource EsriSampleGeoView}" /> | ||
| <Border Style="{DynamicResource EsriSampleControlPanel}" | ||
| VerticalOptions="Start" | ||
| MaximumWidthRequest="620"> | ||
| <Grid> | ||
| <Grid.RowDefinitions> | ||
| <RowDefinition Height="Auto" /> | ||
| <RowDefinition Height="Auto" /> | ||
| <RowDefinition Height="250" /> | ||
| </Grid.RowDefinitions> | ||
| <Grid Grid.Row="0" ColumnDefinitions="Auto,*"> | ||
| <CheckBox x:Name="VisibleOnlyCheckBox" | ||
| Grid.Column="0" | ||
| CheckedChanged="VisibleOnlyCheckBox_Changed" /> | ||
| <Label Grid.Column="1" | ||
| Text="Show line of sight where target (circle) is visible from observer (triangle)" | ||
| VerticalOptions="Center" | ||
| LineBreakMode="WordWrap" /> | ||
| </Grid> | ||
| <BoxView Grid.Row="1" HeightRequest="1" Color="Gray" Margin="0,4" /> | ||
| <ScrollView Grid.Row="2"> | ||
| <VerticalStackLayout x:Name="ResultsStackLayout" /> | ||
| </ScrollView> | ||
| </Grid> | ||
| </Border> | ||
| <Label Text="Raster data Copyright Scottish Government and SEPA (2014)" | ||
| Grid.RowSpan="2" | ||
| Grid.ColumnSpan="2" | ||
| HorizontalOptions="End" | ||
| VerticalOptions="End" | ||
| Margin="0,0,10,30" | ||
| FontAttributes="Italic" | ||
| FontSize="11" | ||
| TextColor="White" /> | ||
| </Grid> | ||
| </ContentPage> |
231 changes: 231 additions & 0 deletions
231
...amples/Samples/Analysis/ShowLineOfSightAnalysisOnMap/ShowLineOfSightAnalysisOnMap.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysisOnMap/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Show line of sight analysis in map | ||
|
|
||
| Perform a line of sight analysis in a map view between fixed observer and target positions. | ||
|
|
||
|  | ||
|
|
||
| ## Use case | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| ## How to use the sample | ||
|
|
||
| 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. | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. Create a `Map` and set it on a `MapView`. | ||
| 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. | ||
| 3. Create a `ContinuousField` from a raster file containing elevation data. | ||
| 4. Create a list of `LineOfSightPosition` from target and observer `MapPoint`s and a `HeightOrigin.Relative`. | ||
| 5. Configure `LineOfSightParameters` with a new `ObserverTargetPairs`, passing the list of observer and target line of sight positions. | ||
| 6. Create a `LineOfSightFunction` from the continuous field and line of sight parameters. | ||
| 7. Evaluate the function to get `LineOfSight` results. | ||
| 8. Create a `Graphic` from each result, using the geometry of the result's `VisibleLine` or `NotVisibleLine` result, and an appropriate symbol. | ||
| 9. Use `LineOfSight.TargetVisibility` to determine if the observer position has a direct line of sight to the target position. | ||
| 10. Check for `LineOfSight.Error`s and get the length of the visible line result with `GeometryEngine.LengthGeodetic` to report results. | ||
|
|
||
| ## Relevant API | ||
|
|
||
| * ContinuousField | ||
| * GeometryEngine | ||
| * GraphicsOverlay | ||
| * LineOfSight | ||
| * LineOfSightFunction | ||
| * LineOfSightParameters | ||
| * LineOfSightPosition | ||
| * ObserverTargetPairs | ||
|
|
||
| ## About the data | ||
|
|
||
| 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) | ||
| (Raster data Copyright Scottish Government and SEPA (2014)). | ||
|
|
||
| ## Tags | ||
|
|
||
| analysis, elevation, line of sight, map view, spatial analysis, terrain, visibility |
35 changes: 35 additions & 0 deletions
35
src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysisOnMap/readme.metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| { | ||
| "category": "Analysis", | ||
| "description": "Perform a line of sight analysis in a map view between fixed observer and target positions.", | ||
| "formal_name": "ShowLineOfSightAnalysisOnMap", | ||
| "ignore": false, | ||
| "images": [ | ||
| "showlineofsightanalysisonmap.jpg" | ||
| ], | ||
| "keywords": [ | ||
| "analysis", | ||
| "elevation", | ||
| "line of sight", | ||
| "map view", | ||
| "spatial analysis", | ||
| "terrain", | ||
| "visibility" | ||
| ], | ||
| "offline_data": [], | ||
| "redirect_from": [], | ||
| "relevant_apis": [ | ||
| "ContinuousField", | ||
| "GeometryEngine", | ||
| "GraphicsOverlay", | ||
| "LineOfSight", | ||
| "LineOfSightFunction", | ||
| "LineOfSightParameters", | ||
| "LineOfSightPosition", | ||
| "ObserverTargetPairs" | ||
| ], | ||
| "snippets": [ | ||
| "ShowLineOfSightAnalysisOnMap.xaml.cs", | ||
| "ShowLineOfSightAnalysisOnMap.xaml" | ||
| ], | ||
| "title": "Show line of sight analysis in map" | ||
| } |
Binary file added
BIN
+83 KB
.../Samples/Analysis/ShowLineOfSightAnalysisOnMap/showlineofsightanalysisonmap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+84.9 KB
.../Samples/Analysis/ShowLineOfSightAnalysisOnMap/ShowLineOfSightAnalysisOnMap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.