// provide a callback to execute when the GeodatabaseSyncTask completes (successfully or with an exception) private async void GdbCompleteCallback(Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseStatusInfo statusInfo, Exception ex) { // if unsuccessful, report the exception and return if (ex != null) { this.Dispatcher.Invoke(() => this.SyncStatusTextBlock.Text = "An exception occured: " + ex.Message); return; } // if successful, read the generated geodatabase from the server var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusInfo.ResultUri, null); var geodatabasePath = System.IO.Path.Combine(@"C:\Temp\Cache", "PoiLocal.geodatabase"); // create a local path for the geodatabase, if it doesn't already exist if (!System.IO.Directory.Exists(@"C:\Temp\Cache")) { System.IO.Directory.CreateDirectory(@"C:\Temp\Cache"); } await Task.Factory.StartNew(async delegate { using (var stream = System.IO.File.Create(geodatabasePath)) { await gdbStream.Result.Content.CopyToAsync(stream); } this.Dispatcher.Invoke(() => this.LocalDataPathTextBlock.Text = geodatabasePath); this.Dispatcher.Invoke(() => this.SyncProgressBar.Visibility = System.Windows.Visibility.Hidden); this.Dispatcher.Invoke(() => this.SyncStatusPanel.Visibility = System.Windows.Visibility.Collapsed); this.Dispatcher.Invoke(() => this.UseLocalDataOption.IsEnabled = true); }); }
/// <summary> /// Downloads and saved geodatabase from given Uri to the device. /// </summary> /// <param name="task">Task used.</param> /// <param name="uriToGeodatabase">Uri to geodabase that is downloaded.</param> /// <param name="locationForGeodatabase">Full file path, where geodatabase is downloaded.</param> /// <param name="geodatabaseName">Name for the geodatabase. This is the name that is used when it is saved to the device.</param> /// <returns>Returns after download is fully completed.</returns> /// <remarks>If target folder doesn't exists, it is created.</remarks> public static async Task DownloadGeodatabaseAsync( this GeodatabaseSyncTask task, Uri uriToGeodatabase, string locationForGeodatabase, string geodatabaseName) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(uriToGeodatabase, null); var geodatabasePath = Path.Combine(locationForGeodatabase, geodatabaseName); if (!Directory.Exists(locationForGeodatabase)) { Directory.CreateDirectory(locationForGeodatabase); } await Task.Factory.StartNew(async () => { using (var stream = File.Create(geodatabasePath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); }
// provide a callback to execute when the GeodatabaseSyncTask completes (successfully or with an exception) private async void GdbCompleteCallback(Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseStatusInfo statusInfo, Exception ex) { // if unsuccessful, report the exception and return if (ex != null) { Console.WriteLine("An exception occured: " + ex.Message); //this.ReportStatus("An exception occured: " + ex.Message); return; } // if successful, read the generated geodatabase from the server var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusInfo.ResultUri, null); var geodatabasePath = System.IO.Path.Combine(@"C:\Temp\10_2_7Demo", "WildlifeLocal.geodatabase"); // create a local path for the geodatabase, if it doesn't already exist if (!System.IO.Directory.Exists(@"C:\Temp\10_2_7Demo")) { System.IO.Directory.CreateDirectory(@"C:\Temp\10_2_7Demo"); } // write geodatabase to local location await Task.Factory.StartNew(async delegate { using (var stream = System.IO.File.Create(geodatabasePath)) { await gdbStream.Result.Content.CopyToAsync(stream); } MessageBox.Show("Offline database created at " + geodatabasePath); }); }
// Batch Geocode private async void Button_Click(object sender, RoutedEventArgs e) { try { progress.Visibility = Visibility.Visible; _graphicsOverlay.Graphics.Clear(); MyMapView.Overlays.Items.Clear(); string records = string.Join(",", SourceAddresses.Where(s => !string.IsNullOrWhiteSpace(s.Address)) .Select((s, idx) => string.Format("{{ \"attributes\": {{ \"OBJECTID\": {0}, \"SingleLine\": \"{1}\" }} }}", idx, s.Address)) .ToArray()); string addresses = string.Format("{{ \"records\": [ {0} ] }}", records); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters["f"] = "json"; parameters["outSR"] = MyMapView.SpatialReference.Wkid.ToString(); parameters["addresses"] = addresses; ArcGISHttpClient httpClient = new ArcGISHttpClient(); var response = await httpClient.GetOrPostAsync(GEOCODE_SERVICE_URL, parameters); var jsonResults = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonResults)); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeocodeResults)); var results = serializer.ReadObject(mStream) as GeocodeResults; foreach (var candidate in results.locations) { var location = candidate.location; MapPoint point = new MapPoint(Convert.ToDouble(location.x), Convert.ToDouble(location.y), MyMapView.SpatialReference); _graphicsOverlay.Graphics.Add(new Graphic(point)); // Create a new templated overlay for the geocoded address var overlay = new ContentControl() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top }; overlay.Template = layoutGrid.Resources["MapTipTemplate"] as ControlTemplate; overlay.DataContext = candidate.attributes; MapView.SetViewOverlayAnchor(overlay, point); MyMapView.Overlays.Items.Add(overlay); } await MyMapView.SetViewAsync(GeometryEngine.Union(_graphicsOverlay.Graphics.Select(g => g.Geometry)).Extent.Expand(1.5)); } catch (Exception ex) { var _x = new MessageDialog(ex.Message, "Sample Error").ShowAsync(); } finally { progress.Visibility = Visibility.Collapsed; AddressFlyout.Hide(); } }
// Batch Geocode private async void Button_Click(object sender, RoutedEventArgs e) { try { progress.Visibility = Visibility.Visible; graphicsLayer.Graphics.Clear(); mapView.Overlays.Clear(); JavaScriptSerializer serializer = new JavaScriptSerializer(); string records = string.Join(",", SourceAddresses.Where(s => !string.IsNullOrWhiteSpace(s.Address)) .Select((s, idx) => string.Format("{{ \"attributes\": {{ \"OBJECTID\": {0}, \"SingleLine\": \"{1}\" }} }}", idx, s.Address)) .ToArray()); string addresses = string.Format("{{ \"records\": [ {0} ] }}", records); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters["f"] = "json"; parameters["outSR"] = mapView.SpatialReference.Wkid.ToString(); parameters["addresses"] = addresses; ArcGISHttpClient httpClient = new ArcGISHttpClient(); var response = await httpClient.GetOrPostAsync(GEOCODE_SERVICE_URL, parameters); var jsonResults = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var results = serializer.Deserialize <Dictionary <string, object> >(jsonResults); var candidates = results["locations"] as ArrayList; foreach (var candidate in candidates.OfType <Dictionary <string, object> >()) { var location = candidate["location"] as Dictionary <string, object>; MapPoint point = new MapPoint(Convert.ToDouble(location["x"]), Convert.ToDouble(location["y"]), mapView.SpatialReference); graphicsLayer.Graphics.Add(new Graphic(point)); // Create a new templated overlay for the geocoded address var overlay = new ContentControl() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top }; overlay.Template = layoutGrid.Resources["MapTipTemplate"] as ControlTemplate; overlay.DataContext = candidate["attributes"] as Dictionary <string, object>; MapView.SetMapOverlayAnchor(overlay, point); mapView.Overlays.Add(overlay); } await mapView.SetViewAsync(GeometryEngine.Union(graphicsLayer.Graphics.Select(g => g.Geometry)).Extent.Expand(1.5)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Sample Error"); } finally { progress.Visibility = Visibility.Collapsed; } }
// Batch Geocode private async void Button_Click(object sender, RoutedEventArgs e) { try { progress.Visibility = Visibility.Visible; _graphicsOverlay.Graphics.Clear(); MyMapView.Overlays.Items.Clear(); string records = string.Join(",", SourceAddresses.Where(s => !string.IsNullOrWhiteSpace(s.Address)) .Select((s, idx) => string.Format("{{ \"attributes\": {{ \"OBJECTID\": {0}, \"SingleLine\": \"{1}\" }} }}", idx, s.Address)) .ToArray()); string addresses = string.Format("{{ \"records\": [ {0} ] }}", records); Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters["f"] = "json"; parameters["outSR"] = MyMapView.SpatialReference.Wkid.ToString(); parameters["addresses"] = addresses; ArcGISHttpClient httpClient = new ArcGISHttpClient(); var response = await httpClient.GetOrPostAsync(GEOCODE_SERVICE_URL, parameters); var jsonResults = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonResults)); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeocodeResults)); var results = serializer.ReadObject(mStream) as GeocodeResults; foreach (var candidate in results.locations) { var location = candidate.location; MapPoint point = new MapPoint(Convert.ToDouble(location.x), Convert.ToDouble(location.y), MyMapView.SpatialReference); _graphicsOverlay.Graphics.Add(new Graphic(point)); // Create a new templated overlay for the geocoded address var overlay = new ContentControl() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top }; overlay.Template = layoutGrid.Resources["MapTipTemplate"] as ControlTemplate; overlay.DataContext = candidate.attributes; MapView.SetViewOverlayAnchor(overlay, point); MyMapView.Overlays.Items.Add(overlay); } await MyMapView.SetViewAsync(GeometryEngine.Union(_graphicsOverlay.Graphics.Select(g => g.Geometry)).Extent.Expand(1.5)); } catch (Exception ex) { var _x = new MessageDialog(ex.Message, "Sample Error").ShowAsync(); } finally { progress.Visibility = Visibility.Collapsed; AddressFlyout.Hide(); } }
// Batch Geocode private async void Button_Click(object sender, RoutedEventArgs e) { try { progress.Visibility = Visibility.Visible; graphicsLayer.Graphics.Clear(); mapView.Overlays.Clear(); JavaScriptSerializer serializer = new JavaScriptSerializer(); string records = string.Join(",", SourceAddresses.Where(s => !string.IsNullOrWhiteSpace(s.Address)) .Select((s,idx) => string.Format("{{ \"attributes\": {{ \"OBJECTID\": {0}, \"SingleLine\": \"{1}\" }} }}", idx, s.Address)) .ToArray()); string addresses = string.Format("{{ \"records\": [ {0} ] }}", records); Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters["f"] = "json"; parameters["outSR"] = mapView.SpatialReference.Wkid.ToString(); parameters["addresses"] = addresses; ArcGISHttpClient httpClient = new ArcGISHttpClient(); var response = await httpClient.GetOrPostAsync(GEOCODE_SERVICE_URL, parameters); var jsonResults = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var results = serializer.Deserialize<Dictionary<string, object>>(jsonResults); var candidates = results["locations"] as ArrayList; foreach (var candidate in candidates.OfType<Dictionary<string, object>>()) { var location = candidate["location"] as Dictionary<string, object>; MapPoint point = new MapPoint(Convert.ToDouble(location["x"]), Convert.ToDouble(location["y"]), mapView.SpatialReference); graphicsLayer.Graphics.Add(new Graphic(point)); // Create a new templated overlay for the geocoded address var overlay = new ContentControl() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top }; overlay.Template = layoutGrid.Resources["MapTipTemplate"] as ControlTemplate; overlay.DataContext = candidate["attributes"] as Dictionary<string, object>; MapView.SetMapOverlayAnchor(overlay, point); mapView.Overlays.Add(overlay); } await mapView.SetViewAsync(GeometryEngine.Union(graphicsLayer.Graphics.Select(g => g.Geometry)).Extent.Expand(1.5)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Sample Error"); } finally { progress.Visibility = Visibility.Collapsed; } }
// Download a generated geodatabase file private async Task <StorageFile> DownloadGeodatabaseAsync(GeodatabaseStatusInfo statusResult) { var file = await GetGeodatabaseFileAsync(); var client = new ArcGISHttpClient(); var download = await client.GetOrPostAsync(statusResult.ResultUri, null); using (var fileStream = await file.OpenStreamForWriteAsync()) { await download.EnsureSuccessStatusCode().Content.CopyToAsync(fileStream); } return(file); }
// Download a generated geodatabase file private async Task <StorageFile> DownloadGeodatabase(GeodatabaseStatusInfo statusResult) { var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(GDB_NAME, CreationCollisionOption.ReplaceExisting); var client = new ArcGISHttpClient(); var download = await client.GetOrPostAsync(statusResult.ResultUri, null); using (var fileStream = await file.OpenStreamForWriteAsync()) { await download.EnsureSuccessStatusCode().Content.CopyToAsync(fileStream); } return(file); }
// Download a generated geodatabase file private async Task DownloadGeodatabaseAsync(GeodatabaseStatusInfo statusResult) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusResult.ResultUri, null); SetGeodatabaseFileName(); await Task.Run(async() => { using (var stream = System.IO.File.Create(_gdbPath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); }
private async void DownloadGeodatabase(string url) { Console.WriteLine("Download Started"); //Create an instance of ArcGISHttpClient var client = new ArcGISHttpClient(); var geodatabaseStream = client.GetOrPostAsync(url, null); //Path on disk where the geodatabase will be downloaded to var geodatabasePath = @"<Path to location on Disk>\name.geodatabase"; await Task.Factory.StartNew(async delegate { using (var stream = System.IO.File.Create(geodatabasePath)) { await geodatabaseStream.Result.Content.CopyToAsync(stream); Console.WriteLine("Download Complete"); AddFeatureLayerToMap(geodatabasePath); } }); }
// Download a generated geodatabase file private async Task <string> DownloadGeodatabase(GeodatabaseStatusInfo statusResult) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusResult.ResultUri, null); var gdbFolder = System.IO.Path.GetTempPath(); var gdbPath = System.IO.Path.Combine(gdbFolder, statusResult.GeodatabaseName); if (!System.IO.Directory.Exists(gdbFolder)) { System.IO.Directory.CreateDirectory(gdbFolder); } await Task.Run(async() => { using (var stream = System.IO.File.Create(gdbPath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); return(gdbPath); }
// Download a generated geodatabase file private async Task<string> DownloadGeodatabase(GeodatabaseStatusInfo statusResult) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusResult.ResultUri, null); var gdbFolder = System.IO.Path.GetTempPath(); var gdbPath = System.IO.Path.Combine(gdbFolder, statusResult.GeodatabaseName); if (!System.IO.Directory.Exists(gdbFolder)) System.IO.Directory.CreateDirectory(gdbFolder); await Task.Run(async () => { using (var stream = System.IO.File.Create(gdbPath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); return gdbPath; }
// Download a generated geodatabase file private async Task DownloadGeodatabaseAsync(GeodatabaseStatusInfo statusResult) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusResult.ResultUri, null); SetGeodatabaseFileName(); await Task.Run(async () => { using (var stream = System.IO.File.Create(_gdbPath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); }
// Download a generated geodatabase file private async Task<StorageFile> DownloadGeodatabaseAsync(GeodatabaseStatusInfo statusResult) { var file = await GetGeodatabaseFileAsync(); var client = new ArcGISHttpClient(); var download = await client.GetOrPostAsync(statusResult.ResultUri, null); using (var fileStream = await file.OpenStreamForWriteAsync()) { await download.EnsureSuccessStatusCode().Content.CopyToAsync(fileStream); } return file; }
// Download a generated geodatabase file private async Task<string> DownloadGeodatabase(GeodatabaseStatusInfo statusResult, string gdbPath) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusResult.ResultUri, null); var gdbFolder = Path.GetDirectoryName(gdbPath); if (!Directory.Exists(gdbFolder)) Directory.CreateDirectory(gdbFolder); await Task.Factory.StartNew(async () => { using (var stream = File.Create(gdbPath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); return gdbPath; }
// Download a generated geodatabase file private async Task<string> DownloadGeodatabase(GeodatabaseStatusInfo statusResult, string gdbName) { var client = new ArcGISHttpClient(); var gdbStream = client.GetOrPostAsync(statusResult.ResultUri, null); var gdbPath = Path.GetFullPath(string.Format("{0}/{1}{2}", _exportMapPath, gdbName, OfflineMapItem.GeodatabaseFilenameExtension)); await Task.Factory.StartNew(async () => { using (var stream = File.Create(gdbPath)) { await gdbStream.Result.Content.CopyToAsync(stream); } }); return gdbPath; }
// Download a generated geodatabase file private async Task<StorageFile> DownloadGeodatabase(GeodatabaseStatusInfo statusResult) { var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(statusResult.GeodatabaseName, CreationCollisionOption.ReplaceExisting); var client = new ArcGISHttpClient(); var download = await client.GetOrPostAsync(statusResult.ResultUri, null); using (var fileStream = await file.OpenStreamForWriteAsync()) { await download.EnsureSuccessStatusCode().Content.CopyToAsync(fileStream); } return file; }
private async void GenerateFeatuersCompleteCallback(GeodatabaseStatusInfo statusInfo, Exception ex) { if(ex != null) { this.Dispatcher.Invoke(() => StatusMessagesList.Items.Add("An exception has occured: " + ex.Message)); return; } // if successful, download the generated geodatabase from the server var client = new ArcGISHttpClient(); var geodatabaseStream = client.GetOrPostAsync(statusInfo.ResultUri, null); // create a path for the local geodatabse var outFolder = System.AppDomain.CurrentDomain.BaseDirectory; var geodatabasePath = System.IO.Path.Combine(outFolder, "Wildlife.geodatabase"); await Task.Factory.StartNew(async delegate { using (var stream = System.IO.File.Create(geodatabasePath)) { await geodatabaseStream.Result.Content.CopyToAsync(stream); } this.localGeodatabasePath = geodatabasePath; this.Dispatcher.Invoke(() => LocalDataPathTextBlock.Text = geodatabasePath); this.Dispatcher.Invoke(() => LocalDataPathTextBlock.ToolTip = geodatabasePath); this.Dispatcher.Invoke(() => StatusMessagesList.Items.Add("Features downloaded to " + geodatabasePath)); this.Dispatcher.Invoke(() => StatusProgressBar.IsIndeterminate = false); }); }