Loading *.tpk file for use as Basemap UWP

1093
2
Jump to solution
12-02-2020 09:42 AM
justinfernandes
New Contributor II

I am trying to load a basemap for offline use.  I used ArcGISPro to creat a .tpk file.

 

The following is my code to load the tpk.  I am expecting the content of the tpk to be rendered in my Map, but it doesn't.

 

 

 try
            {

                var picker = new Windows.Storage.Pickers.FileOpenPicker();
                picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
                picker.FileTypeFilter.Add(".tpk");
                picker.FileTypeFilter.Add(".mmpk");

                Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
                if (file != null)
                {
                    // Application now has read/write access to the picked file
                    var result  = "Picked photo: " + file.Name;
                }
                else
                {
                    var error = "Operation cancelled.";
                }

                TileCache cache = new TileCache(file.Path);
                
                ArcGISTiledLayer layer = new ArcGISTiledLayer(cache);
                ViewModel.Map = new Map(new Basemap(layer));
               
              
            }
            catch (Exception ex)
            {
                var tmp = ex.ToString();
            }

 

 

 

 

If i try to add a "await ViewModel.Map.LoadAsync()"  at the end of the code above.  It doesnt seem to load and i dont catch an exception either.  

 

Not sure what i am doing wrong.   I chose this code based on the example posted here: https://developers.arcgis.com/net/latest/uwp/guide/work-with-offline-layers.htm

 

attached is a zipped up tpk file that i am attempting load.

0 Kudos
2 Solutions

Accepted Solutions
dotMorten_esri
Esri Notable Contributor

Because UWP is running in a sandbox you can't read the TPK directly from disk.

Typically after picking the file, you'd need to copy it inside the sandbox using Storage APIs, typically the apps' Temp or Local Data folder.

As an alternative, you could give read access to the Windows system account user "ALL_APPLICATION_PACKAGES", which would allow your app to read it directly (that's good for quick testing, but probably not in a production scenario).

 

The documentation is a bit problematic because it doesn't account for this. We'll try and get that fixed

View solution in original post

justinfernandes
New Contributor II

Thank you that worked !

 

full solution is here: 

 try
            {

                var picker = new Windows.Storage.Pickers.FileOpenPicker();
                picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
                picker.FileTypeFilter.Add(".tpk");
                picker.FileTypeFilter.Add(".mmpk");

                Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
                var localFile = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(file.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                await file.CopyAndReplaceAsync(localFile);

                if (localFile != null)
                {
                    // Application now has read/write access to the picked file
                    var result  = "Picked photo: " + localFile.Name;
                }
                else
                {
                    var error = "Operation cancelled.";
                }

                TileCache cache = new TileCache(localFile.Path);
                
                ArcGISTiledLayer layer = new ArcGISTiledLayer(cache);
                ViewModel.Map = new Map(new Basemap(layer));
               
              
            }
            catch (Exception ex)
            {
                var tmp = ex.ToString();
            }

 

the key snippet i added based on your suggestion was :

var localFile = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(file.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                await file.CopyAndReplaceAsync(localFile);

View solution in original post

0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor

Because UWP is running in a sandbox you can't read the TPK directly from disk.

Typically after picking the file, you'd need to copy it inside the sandbox using Storage APIs, typically the apps' Temp or Local Data folder.

As an alternative, you could give read access to the Windows system account user "ALL_APPLICATION_PACKAGES", which would allow your app to read it directly (that's good for quick testing, but probably not in a production scenario).

 

The documentation is a bit problematic because it doesn't account for this. We'll try and get that fixed

justinfernandes
New Contributor II

Thank you that worked !

 

full solution is here: 

 try
            {

                var picker = new Windows.Storage.Pickers.FileOpenPicker();
                picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
                picker.FileTypeFilter.Add(".tpk");
                picker.FileTypeFilter.Add(".mmpk");

                Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
                var localFile = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(file.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                await file.CopyAndReplaceAsync(localFile);

                if (localFile != null)
                {
                    // Application now has read/write access to the picked file
                    var result  = "Picked photo: " + localFile.Name;
                }
                else
                {
                    var error = "Operation cancelled.";
                }

                TileCache cache = new TileCache(localFile.Path);
                
                ArcGISTiledLayer layer = new ArcGISTiledLayer(cache);
                ViewModel.Map = new Map(new Basemap(layer));
               
              
            }
            catch (Exception ex)
            {
                var tmp = ex.ToString();
            }

 

the key snippet i added based on your suggestion was :

var localFile = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(file.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                await file.CopyAndReplaceAsync(localFile);
0 Kudos