-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathDownloadPreplannedMap.xaml.cs
282 lines (237 loc) · 11.1 KB
/
DownloadPreplannedMap.xaml.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2019 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.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.Tasks.Offline;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace ArcGIS.WinUI.Samples.DownloadPreplannedMap
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Download preplanned map area",
category: "Map",
description: "Take a map offline using a preplanned map area.",
instructions: "Select a map area from the Preplanned Map Areas list. Click the button to download the selected area. The download progress will be shown in the Downloads list. When a download is complete, select it to display the offline map in the map view.",
tags: new[] { "map area", "offline", "pre-planned", "preplanned" })]
public partial class DownloadPreplannedMap
{
// ID of a web map with preplanned map areas.
private const string PortalItemId = "acc027394bc84c2fb04d1ed317aac674";
// Folder to store the downloaded mobile map packages.
private string _offlineDataFolder;
// Task for taking map areas offline.
private OfflineMapTask _offlineMapTask;
// Hold onto the original map.
private Map _originalMap;
// Most recently opened map package.
private MobileMapPackage _mobileMapPackage;
public DownloadPreplannedMap()
{
InitializeComponent();
_ = Initialize();
}
private async Task Initialize()
{
try
{
// Close the current mobile package when the sample closes.
Unloaded += (s, e) => { _mobileMapPackage?.Close(); };
// The data manager provides a method to get a suitable offline data folder.
_offlineDataFolder = Path.Combine(DataManager.GetDataFolder(), "SampleData", "DownloadPreplannedMapAreas");
// If temporary data folder doesn't exists, create it.
if (!Directory.Exists(_offlineDataFolder))
{
Directory.CreateDirectory(_offlineDataFolder);
}
// Create a portal to enable access to the portal item.
ArcGISPortal portal = await ArcGISPortal.CreateAsync();
// Create the portal item from the portal item ID.
PortalItem webMapItem = await PortalItem.CreateAsync(portal, PortalItemId);
// Show the map.
_originalMap = new Map(webMapItem);
MyMapView.Map = _originalMap;
// Create an offline map task for the web map item.
_offlineMapTask = await OfflineMapTask.CreateAsync(webMapItem);
// Find the available preplanned map areas.
IReadOnlyList<PreplannedMapArea> preplannedAreas = await _offlineMapTask.GetPreplannedMapAreasAsync();
// Load each item, then add it to the UI.
foreach (PreplannedMapArea area in preplannedAreas)
{
await area.LoadAsync();
AreasList.Items.Add(area);
}
// Hide the loading indicator.
BusyIndicator.Visibility = Visibility.Collapsed;
}
catch (Exception ex)
{
// Something unexpected happened, show the error message.
Debug.WriteLine(ex);
await new MessageDialog2(ex.Message, "There was an error.").ShowAsync();
}
}
private void ShowOnlineButton_Click(object sender, RoutedEventArgs e)
{
// Show the online map.
MyMapView.Map = _originalMap;
// Disable the button.
ShowOnlineButton.IsEnabled = false;
}
private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
{
// Close the current mobile package.
_mobileMapPackage?.Close();
// Set up UI for downloading.
ProgressBar.IsIndeterminate = false;
ProgressBar.Value = 0;
BusyText.Text = "Downloading map area...";
BusyIndicator.Visibility = Visibility.Visible;
// Create folder path where the map package will be downloaded.
string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);
// If the area is already downloaded, open it.
if (Directory.Exists(path))
{
try
{
// Open the offline map package.
_mobileMapPackage = await MobileMapPackage.OpenAsync(path);
// Display the first map.
MyMapView.Map = _mobileMapPackage.Maps.First();
// Update the UI.
BusyText.Text = string.Empty;
BusyIndicator.Visibility = Visibility.Collapsed;
MessageLabel.Text = "Opened offline area.";
return;
}
catch (Exception e)
{
Debug.WriteLine(e);
await new MessageDialog2(e.Message, "Couldn't open offline area. Proceeding to take area offline.").ShowAsync();
}
}
// Create download parameters.
DownloadPreplannedOfflineMapParameters parameters = await _offlineMapTask.CreateDefaultDownloadPreplannedOfflineMapParametersAsync(mapArea);
// Set the update mode to not receive updates.
parameters.UpdateMode = PreplannedUpdateMode.NoUpdates;
// Create the job.
DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(parameters, path);
// Set up event to update the progress bar while the job is in progress.
job.ProgressChanged += OnJobProgressChanged;
try
{
// Download the area.
DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();
// Set the current mobile map package.
_mobileMapPackage = results.MobileMapPackage;
// Handle possible errors and show them to the user.
if (results.HasErrors)
{
// Accumulate all layer and table errors into a single message.
string errors = "";
foreach (KeyValuePair<Layer, Exception> layerError in results.LayerErrors)
{
errors = $"{errors}\n{layerError.Key.Name} {layerError.Value.Message}";
}
foreach (KeyValuePair<FeatureTable, Exception> tableError in results.TableErrors)
{
errors = $"{errors}\n{tableError.Key.TableName} {tableError.Value.Message}";
}
// Show the message.
await new MessageDialog2(errors, "Warning!").ShowAsync();
}
// Show the downloaded map.
MyMapView.Map = results.OfflineMap;
// Update the UI.
ShowOnlineButton.IsEnabled = true;
DownloadButton.Content = "View downloaded area";
MessageLabel.Text = "Downloaded preplanned area.";
}
catch (Exception ex)
{
// Report any errors.
Debug.WriteLine(ex);
await new MessageDialog2(ex.Message, "Downloading map area failed.").ShowAsync();
}
finally
{
BusyText.Text = string.Empty;
BusyIndicator.Visibility = Visibility.Collapsed;
}
}
private void OnJobProgressChanged(object sender, EventArgs e)
{
// Because the event is raised on a background thread, the dispatcher must be used to
// ensure that UI updates happen on the UI thread.
DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
// Update the UI with the progress.
DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;
ProgressBar.Value = downloadJob.Progress;
BusyPercentage.Text = $"{downloadJob.Progress}%";
});
}
private void OnDownloadMapAreaClicked(object sender, RoutedEventArgs e)
{
if (AreasList.SelectedItem != null)
{
PreplannedMapArea selectedMapArea = AreasList.SelectedItem as PreplannedMapArea;
_ = DownloadMapAreaAsync(selectedMapArea);
}
}
private async void OnDeleteAllMapAreasClicked(object sender, RoutedEventArgs e)
{
try
{
// Set up UI for downloading.
ProgressBar.IsIndeterminate = true;
BusyText.Text = "Deleting downloaded map area...";
BusyIndicator.Visibility = Visibility.Visible;
// Reset the map.
MyMapView.Map = _originalMap;
// Close the current mobile package.
_mobileMapPackage?.Close();
// Delete all data from the temporary data folder.
Directory.Delete(_offlineDataFolder, true);
Directory.CreateDirectory(_offlineDataFolder);
// Update the UI.
MessageLabel.Text = "Deleted downloaded areas.";
DownloadButton.Content = "Download preplanned area";
ShowOnlineButton.IsEnabled = false;
}
catch (Exception ex)
{
// Report the error.
await new MessageDialog2(ex.Message, "Deleting map areas failed.").ShowAsync();
}
finally
{
BusyIndicator.Visibility = Visibility.Collapsed;
}
}
private void AreasList_SelectionChanged(object sender, Microsoft.UI.Xaml.Controls.SelectionChangedEventArgs e)
{
// Update the download button to reflect if the map area has already been downloaded.
if (Directory.Exists(Path.Combine(_offlineDataFolder, (AreasList.SelectedItem as PreplannedMapArea).PortalItem.Title)))
{
DownloadButton.Content = "View downloaded area";
}
else
{
DownloadButton.Content = "Download preplanned area";
}
}
}
}