Skip to content

Commit bfbe346

Browse files
committed
Apply code-readability suggestions
1 parent c2728d1 commit bfbe346

6 files changed

Lines changed: 95 additions & 138 deletions

File tree

src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysisOnMap/ShowLineOfSightAnalysisOnMap.xaml.cs

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
using Esri.ArcGISRuntime.Mapping;
1515
using Esri.ArcGISRuntime.Symbology;
1616
using Esri.ArcGISRuntime.UI;
17-
using System;
18-
using System.Collections.Generic;
19-
using System.IO;
20-
using System.Linq;
21-
using System.Threading.Tasks;
2217
using Color = System.Drawing.Color;
2318
using Grid = Microsoft.Maui.Controls.Grid;
2419

@@ -111,10 +106,10 @@ private async Task Initialize()
111106
var continuousField = await ContinuousField.CreateAsync(new[] { rasterPath }, 0);
112107

113108
// Create a line of sight position for the target.
114-
var targetLosPosition = new LineOfSightPosition(_targetPosition, HeightOrigin.Relative);
109+
var targetPosition = new LineOfSightPosition(_targetPosition, HeightOrigin.Relative);
115110

116111
// Create line of sight positions for each observer.
117-
var observerLosPositions = ObserverSeeds.Select(seed =>
112+
var observerPositions = ObserverSeeds.Select(seed =>
118113
new LineOfSightPosition(
119114
new MapPoint(seed.X, seed.Y, RelativeHeightMeters, SpatialReferences.WebMercator),
120115
HeightOrigin.Relative)).ToList();
@@ -123,7 +118,7 @@ private async Task Initialize()
123118
var parameters = new LineOfSightParameters
124119
{
125120
ObserverTargetPairs = new ObserverTargetPairs(
126-
observerLosPositions, new[] { targetLosPosition })
121+
observerPositions, new[] { targetPosition })
127122
};
128123

129124
// Create line of sight function with the continuous field and parameters.
@@ -132,9 +127,6 @@ private async Task Initialize()
132127
// Evaluate the line of sight function.
133128
var results = await lineOfSightFunction.EvaluateAsync();
134129

135-
// Build observer summaries for the info panel.
136-
BuildResultsUI(results);
137-
138130
// Add result line graphics to the results graphics overlay.
139131
foreach (var result in results)
140132
{
@@ -154,28 +146,16 @@ private async Task Initialize()
154146
_resultsGraphicsOverlay.Graphics.Add(graphic);
155147
}
156148
}
149+
150+
// Build observer summaries for the info panel.
151+
BuildResultsUI(results);
157152
}
158153
catch (Exception ex)
159154
{
160155
await Application.Current.Windows[0].Page.DisplayAlertAsync("Error", ex.Message, "OK");
161156
}
162157
}
163158

164-
// Filter the result line graphics based on the checkbox state to show only visible lines.
165-
private void VisibleOnlyCheckBox_Changed(object sender, CheckedChangedEventArgs e)
166-
{
167-
if (_resultsGraphicsOverlay == null) return;
168-
169-
bool showVisibleOnly = VisibleOnlyCheckBox.IsChecked;
170-
171-
foreach (var graphic in _resultsGraphicsOverlay.Graphics)
172-
{
173-
// TargetVisibility value of 1.0 indicates the target is visible from the observer.
174-
bool isVisible = Convert.ToDouble(graphic.Attributes["TargetVisibility"]) == 1.0;
175-
graphic.IsVisible = !showVisibleOnly || isVisible;
176-
}
177-
}
178-
179159
// Build the results UI by programmatically adding rows to the VerticalStackLayout for each observer.
180160
private void BuildResultsUI(IReadOnlyList<LineOfSight> results)
181161
{
@@ -186,17 +166,18 @@ private void BuildResultsUI(IReadOnlyList<LineOfSight> results)
186166
// Get the length of the visible line if it exists.
187167
var visibleLength = result.VisibleLine == null ? 0 :
188168
GeometryEngine.LengthGeodetic(result.VisibleLine, LinearUnits.Meters, GeodeticCurveType.Geodesic);
189-
169+
190170
// Set the info text based on the results of the analysis.
191-
string infoText;
192-
if (result.Error != null)
193-
infoText = result.Error.Message;
194-
else if (result.NotVisibleLine == null)
195-
infoText = $"Target visible from observer over {visibleLength:F1} metres.";
196-
else
197-
infoText = $"Target not visible from observer. Obstructed after {visibleLength:F1} metres.";
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.";
198178

199-
// Update the UI.
179+
// Update the UI.
180+
var color = ObserverSeeds[i].Color;
200181

201182
var row = new Grid
202183
{
@@ -232,20 +213,19 @@ private void BuildResultsUI(IReadOnlyList<LineOfSight> results)
232213
}
233214
}
234215

235-
// Return a description of the line of sight result based on the visible and not-visible line lengths.
236-
private static string GetVisibleDistanceInfoText(double visibleLength, double notVisibleLength)
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)
237218
{
238-
if (notVisibleLength <= 0)
239-
return $"Target visible from observer over {visibleLength:F1} metres.";
219+
if (_resultsGraphicsOverlay == null) return;
240220

241-
return $"Target not visible from observer. Obstructed after {visibleLength:F1} metres.";
242-
}
221+
bool showVisibleOnly = VisibleOnlyCheckBox.IsChecked;
243222

244-
// Calculate the geodetic length of a polyline in meters.
245-
private static double PolylineLengthMeters(Polyline line)
246-
{
247-
if (line == null) return 0;
248-
return GeometryEngine.LengthGeodetic(line, LinearUnits.Meters, GeodeticCurveType.Geodesic);
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+
}
249229
}
250230
}
251231
}

src/MAUI/Maui.Samples/Samples/Analysis/ShowLineOfSightAnalysisOnMap/readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ The sample loads with a map centered on the Isle of Arran, Scotland, and runs a
2323
5. Configure `LineOfSightParameters` with a new `ObserverTargetPairs`, passing the list of observer and target line of sight positions.
2424
6. Create a `LineOfSightFunction` from the continuous field and line of sight parameters.
2525
7. Evaluate the function to get `LineOfSight` results.
26-
8. Check if the results contain any `LineOfSight.Error`s.
27-
9. Create a `Graphic` from each result, using the geometry of the result's `VisibleLine` or `NotVisibleLine` result, and an appropriate symbol.
28-
10. Use `LineOfSight.TargetVisibility` to determine if the observer position has a direct line of sight to the target position.
29-
11. Get the length of the visible line result with `GeometryEngine.LengthGeodetic` to report 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.
3029

3130
## Relevant API
3231

src/WPF/WPF.Viewer/Samples/Analysis/ShowLineOfSightAnalysisOnMap/ShowLineOfSightAnalysisOnMap.xaml.cs

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private static readonly (Color Color, double X, double Y)[] ObserverSeeds =
5353
(Color.Blue, -571683.896, 7492017.864),
5454
};
5555

56+
// Graphics overlay for the analysis results.
5657
private GraphicsOverlay _resultsGraphicsOverlay;
5758

5859
// Symbols for visible and not-visible line segments.
@@ -110,11 +111,11 @@ private async Task Initialize()
110111
// Create a continuous field from the elevation raster file.
111112
var continuousField = await ContinuousField.CreateAsync(new[] { rasterPath }, 0);
112113

113-
// Create line of sight positions for the target.
114-
var targetLosPosition = new LineOfSightPosition(_targetPosition, HeightOrigin.Relative);
114+
// Create a line of sight position for the target.
115+
var targetPosition = new LineOfSightPosition(_targetPosition, HeightOrigin.Relative);
115116

116117
// Create line of sight positions for each observer.
117-
var observerLosPositions = ObserverSeeds.Select(seed =>
118+
var observerPositions = ObserverSeeds.Select(seed =>
118119
new LineOfSightPosition(
119120
new MapPoint(seed.X, seed.Y, RelativeHeightMeters, SpatialReferences.WebMercator),
120121
HeightOrigin.Relative)).ToList();
@@ -123,7 +124,7 @@ private async Task Initialize()
123124
var parameters = new LineOfSightParameters
124125
{
125126
ObserverTargetPairs = new ObserverTargetPairs(
126-
observerLosPositions, new[] { targetLosPosition })
127+
observerPositions, new[] { targetPosition })
127128
};
128129

129130
// Create line of sight function with the continuous field and parameters.
@@ -132,10 +133,6 @@ private async Task Initialize()
132133
// Evaluate the line of sight function.
133134
var results = await lineOfSightFunction.EvaluateAsync();
134135

135-
// Build observer summaries for the info panel.
136-
var summaries = BuildObserverSummaries(results);
137-
ResultsItemsControl.ItemsSource = summaries;
138-
139136
// Add result line graphics to the results graphics overlay.
140137
foreach (var result in results)
141138
{
@@ -155,42 +152,36 @@ private async Task Initialize()
155152
_resultsGraphicsOverlay.Graphics.Add(graphic);
156153
}
157154
}
155+
156+
// Build observer summaries for the info panel.
157+
ResultsItemsControl.ItemsSource = BuildObserverSummaries(results);
158158
}
159159
catch (Exception ex)
160160
{
161161
MessageBox.Show(ex.Message, "Error");
162162
}
163163
}
164164

165-
// Filter the result line graphics based on the checkbox state to show only visible lines.
166-
private void VisibleOnlyCheckBox_Changed(object sender, RoutedEventArgs e)
167-
{
168-
if (_resultsGraphicsOverlay == null) return;
169-
170-
bool showVisibleOnly = VisibleOnlyCheckBox.IsChecked == true;
171-
172-
foreach (var graphic in _resultsGraphicsOverlay.Graphics)
173-
{
174-
// TargetVisibility value of 1.0 indicates the target is visible from the observer.
175-
bool isVisible = Convert.ToDouble(graphic.Attributes["TargetVisibility"]) == 1.0;
176-
graphic.IsVisible = !showVisibleOnly || isVisible;
177-
}
178-
}
179-
180165
// Build a list of observer result summaries from the line of sight results for display in the info panel.
181166
private List<ObserverResultSummary> BuildObserverSummaries(IReadOnlyList<LineOfSight> results)
182167
{
183168
var summaries = new List<ObserverResultSummary>();
184169
for (int i = 0; i < results.Count; i++)
185170
{
186171
var result = results[i];
187-
var error = result.Error;
188-
var visibleLength = PolylineLengthMeters(result.VisibleLine);
189-
var notVisibleLength = PolylineLengthMeters(result.NotVisibleLine);
190172

191-
string infoText = error != null
192-
? error.Message
193-
: GetVisibleDistanceInfoText(visibleLength, notVisibleLength);
173+
// Get the length of the visible line if it exists.
174+
var visibleLength = result.VisibleLine == null ? 0 :
175+
GeometryEngine.LengthGeodetic(result.VisibleLine, LinearUnits.Meters, GeodeticCurveType.Geodesic);
176+
177+
// Set the info text based on the results of the analysis.
178+
string infoText;
179+
if (result.Error != null)
180+
infoText = result.Error.Message;
181+
else if (result.NotVisibleLine == null)
182+
infoText = $"Target visible from observer over {visibleLength:F1} metres.";
183+
else
184+
infoText = $"Target not visible from observer. Obstructed after {visibleLength:F1} metres.";
194185

195186
var color = ObserverSeeds[i].Color;
196187
var brush = new SolidColorBrush(
@@ -201,20 +192,19 @@ private List<ObserverResultSummary> BuildObserverSummaries(IReadOnlyList<LineOfS
201192
return summaries;
202193
}
203194

204-
// Return a description of the line of sight result based on the visible and not-visible line lengths.
205-
private static string GetVisibleDistanceInfoText(double visibleLength, double notVisibleLength)
195+
// Filter the result line graphics based on the checkbox state to show only visible lines.
196+
private void VisibleOnlyCheckBox_Changed(object sender, RoutedEventArgs e)
206197
{
207-
if (notVisibleLength <= 0)
208-
return $"Target visible from observer over {visibleLength:F1} metres.";
198+
if (_resultsGraphicsOverlay == null) return;
209199

210-
return $"Target not visible from observer. Obstructed after {visibleLength:F1} metres.";
211-
}
200+
bool showVisibleOnly = VisibleOnlyCheckBox.IsChecked == true;
212201

213-
// Calculate the geodetic length of a polyline in meters.
214-
private static double PolylineLengthMeters(Polyline line)
215-
{
216-
if (line == null) return 0;
217-
return GeometryEngine.LengthGeodetic(line, LinearUnits.Meters, GeodeticCurveType.Geodesic);
202+
foreach (var graphic in _resultsGraphicsOverlay.Graphics)
203+
{
204+
// TargetVisibility value of 1.0 indicates the target is visible from the observer.
205+
bool isVisible = Convert.ToDouble(graphic.Attributes["TargetVisibility"]) == 1.0;
206+
graphic.IsVisible = !showVisibleOnly || isVisible;
207+
}
218208
}
219209
}
220210

src/WPF/WPF.Viewer/Samples/Analysis/ShowLineOfSightAnalysisOnMap/readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ The sample loads with a map centered on the Isle of Arran, Scotland, and runs a
2323
5. Configure `LineOfSightParameters` with a new `ObserverTargetPairs`, passing the list of observer and target line of sight positions.
2424
6. Create a `LineOfSightFunction` from the continuous field and line of sight parameters.
2525
7. Evaluate the function to get `LineOfSight` results.
26-
8. Check if the results contain any `LineOfSight.Error`s.
27-
9. Create a `Graphic` from each result, using the geometry of the result's `VisibleLine` or `NotVisibleLine` result, and an appropriate symbol.
28-
10. Use `LineOfSight.TargetVisibility` to determine if the observer position has a direct line of sight to the target position.
29-
11. Get the length of the visible line result with `GeometryEngine.LengthGeodetic` to report 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.
3029

3130
## Relevant API
3231

0 commit comments

Comments
 (0)