forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadGeoPackage.xaml.cs
222 lines (178 loc) · 11.4 KB
/
ReadGeoPackage.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
// Copyright 2018 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 System;
using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Rasters;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Xamarin.Forms;
namespace ArcGISRuntime.Samples.ReadGeoPackage
{
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
"Read a GeoPackage",
"Data",
"This sample demonstrates how to open a GeoPackage file from the local file system and list the available GeoPackageRasters and GeoPackageFeatureTables from the GeoPackage. Users can add and remove the selected datasets as RasterLayers or FeatureLayers to the map.",
"Select a layer name in the 'Layers Not in the Map' ListBox and then click the 'Add Layer to Map' button to add it to the map. Conversely to remove a layer from the map select a layer name in the 'Layers in the Map' ListBox and click the 'Remove Layer from Map' button. NOTE: The GeoPackage will be downloaded from an ArcGIS Online portal automatically.")]
[ArcGISRuntime.Samples.Shared.Attributes.OfflineData("68ec42517cdd439e81b036210483e8e7")]
public partial class ReadGeoPackage : ContentPage
{
public ReadGeoPackage()
{
InitializeComponent();
// Set up the GUI of the sample
Initialize();
}
// Member HybridDictionary to hold the multiple key/object pairs that represent:
// human-readable string name of a layer - key
// the layer itself (RasterLayer or FeatureLayer) - object
// NOTE: According to MSDN, a HybridDictionary is useful for cases where the number
// of elements in a dictionary is unknown
private HybridDictionary _myHybridDictionary_Layers = new HybridDictionary();
// Member ObservableCollection to hold the human-readable string name of the
// layers - used as the ListView_LayersNotInTheMap.ItemsSource
private ObservableCollection<string> _myObservableCollection_LayerNamesNotInTheMap = new ObservableCollection<string>();
// Member ObservableCollection to hold the human-readable string name of the
// layers - used as the ListView_LayersInTheMap.ItemsSource
private ObservableCollection<string> _myObservableCollection_LayerNamesInTheMap = new ObservableCollection<string>();
private async void Initialize()
{
// Create a new map centered on Aurora Colorado
MyMapView.Map = new Map(BasemapType.Streets, 39.7294, -104.8319, 11);
// Get the full path to the GeoPackage on the device
string myGeoPackagePath = GetGeoPackagePath();
try
{
// Open the GeoPackage
GeoPackage myGeoPackage = await GeoPackage.OpenAsync(myGeoPackagePath);
// Get the read only list of GeoPackageRasters from the GeoPackage
IReadOnlyList<GeoPackageRaster> myReadOnlyListOfGeoPackageRasters = myGeoPackage.GeoPackageRasters;
// Loop through each GeoPackageRaster
foreach (GeoPackageRaster oneGeoPackageRaster in myReadOnlyListOfGeoPackageRasters)
{
// Create a RasterLayer from the GeoPackageRaster
RasterLayer myRasterLayer = new RasterLayer(oneGeoPackageRaster)
{
// Set the opacity on the RasterLayer to partially visible
Opacity = 0.55
};
// Load the RasterLayer - that way we can get to it's properties
await myRasterLayer.LoadAsync();
// Create a string variable to hold the human-readable name of the RasterLayer for display
// in the ListBox and the HybridDictonary - it will initially be an empty string
string myRasterLayerName = "";
if (myRasterLayer.Name != "")
{
// We have a good human-readable name for the RasterLayer that came from
// the RasterLayer.Name property
myRasterLayerName = myRasterLayer.Name;
}
else if (oneGeoPackageRaster.Path.Split('/').Last() != "")
{
// We did not get a good human-readable name from the RasterLayer from the .Name
// property, get the good human-readable name from the GeoPackageRaster.Path instead
myRasterLayerName = oneGeoPackageRaster.Path.Split('/').Last();
}
// Append the 'type of layer' to the myRasterLayerName string to display in the
// ListBox and as the key for the HybridDictonary
myRasterLayerName = myRasterLayerName + " - RasterLayer";
// Add the name of the RasterLayer and the RasterLayer itself into the HybridDictionary
_myHybridDictionary_Layers.Add(myRasterLayerName, myRasterLayer);
// Add the name of the RasterLayer to _myObservableCollection_LayerNamesNotInTheMap
// which displays the human-readable layer names in the ListView_LayersNotInTheMap
_myObservableCollection_LayerNamesNotInTheMap.Add(myRasterLayerName);
}
// Get the read only list of GeoPackageFeatureTabless from the GeoPackage
IReadOnlyList<GeoPackageFeatureTable> myReadOnlyListOfGeoPackageFeatureTables = myGeoPackage.GeoPackageFeatureTables;
// Loop through each GeoPackageFeatureTable
foreach (GeoPackageFeatureTable oneGeoPackageFeatureTable in myReadOnlyListOfGeoPackageFeatureTables)
{
// Create a FeatureLayer from the GeoPackageFeatureLayer
FeatureLayer myFeatureLayer = new FeatureLayer(oneGeoPackageFeatureTable);
// Load the FeatureLayer - that way we can get to it's properties
await myFeatureLayer.LoadAsync();
// Create a string variable to hold the human-readable name of the FeatureLayer for
// display in the ListBox and the HybridDictonary
string myFeatureLayerName = myFeatureLayer.Name;
// Append the 'type of layer' to the myFeatureLayerName string to display in the
// ListBox and as the key for the HybridDictonary
myFeatureLayerName = myFeatureLayerName + " - FeatureLayer";
// Add the name of the FeatureLayer and the FeatureLayer itself into the HybridDictionary
_myHybridDictionary_Layers.Add(myFeatureLayerName, myFeatureLayer);
// Add the name of the RasterLayer to _myObservableCollection_LayerNamesNotInTheMap
// which displays the human-readable layer names in the ListView_LayersNotInTheMap
_myObservableCollection_LayerNamesNotInTheMap.Add(myFeatureLayerName);
}
// Set the _myObservableCollection_LayerNamesNotInTheMap as the ListView_LayersNotInTheMap.ItemSource
ListView_LayersNotInTheMap.ItemsSource = _myObservableCollection_LayerNamesNotInTheMap;
// Set the _myObservableCollection_LayerNamesInTheMap as the ListView_LayersInTheMap.ItemSource
ListView_LayersInTheMap.ItemsSource = _myObservableCollection_LayerNamesInTheMap;
}
catch (Exception e)
{
await ((Page)Parent).DisplayAlert("Error", e.ToString(), "OK");
}
}
private void Button_AddLayerToMap_Clicked(object sender, System.EventArgs e)
{
// Get the user selected item from the ListView_LayersNotInTheMap
object myLayerSelection = ListView_LayersNotInTheMap.SelectedItem;
// Ensure we have a valid selection
if (myLayerSelection != null)
{
// Get the human-readable name of the layer
string myLayerName = myLayerSelection.ToString();
// Get the layer from the HybridDictionary (it could be either a RasterLayer
// or a FeatureLayer - both inherit from the abstract/base Layer class)
Layer myLayer = (Layer)_myHybridDictionary_Layers[myLayerName];
// Add the layer to the map
MyMapView.Map.OperationalLayers.Add(myLayer);
// Remove the human-readable layer name from the ObservableCollection _myLayerNamesNotInTheMap
// This will automatically update the ListView_LayersNotInTheMap
_myObservableCollection_LayerNamesNotInTheMap.Remove(myLayerName);
// Add the human-readable layer name from the ObservableCollection _myLayerNamesInTheMap
// This will automatically update the ListView_LayersInTheMap
_myObservableCollection_LayerNamesInTheMap.Add(myLayerName);
}
// Clear out an existing selected items in the ListView_LayersNotInTheMap
ListView_LayersNotInTheMap.SelectedItem = null;
}
private void Button_RemoveLayerFromMap_Clicked(object sender, System.EventArgs e)
{
// Get the user selected item from the ListView_LayersInTheMap
object myLayerSelection = ListView_LayersInTheMap.SelectedItem;
// Ensure we have a valid selection
if (myLayerSelection != null)
{
// Get the human-readable name of the layer
string myLayerName = myLayerSelection.ToString();
// Get the layer from the HybridDictionary (it could be either a RasterLayer
// or a FeatureLayer - both inherit from the abstract/base Layer class)
Layer myLayer = (Layer)_myHybridDictionary_Layers[myLayerName];
// Remove the layer from the map
MyMapView.Map.OperationalLayers.Remove(myLayer);
// Remove the human-readable layer name from the _myObservableCollection_LayerNamesInTheMap
// This will automatically update the ListView_LayersInTheMap
_myObservableCollection_LayerNamesInTheMap.Remove(myLayerName);
// Add the human-readable layer name from the _myObservableCollection_LayerNamesNotInTheMap
// This will automatically update the ListView_LayersNotInTheMap
_myObservableCollection_LayerNamesNotInTheMap.Add(myLayerName);
}
// Clear out an existing selected items in the ListView_LayersInTheMap
ListView_LayersInTheMap.SelectedItem = null;
}
private static string GetGeoPackagePath()
{
return DataManager.GetDataFolder("68ec42517cdd439e81b036210483e8e7", "AuroraCO.gpkg");
}
}
}