Export tiles

View on GitHubSample viewer app

Download tiles to a local tile cache file stored on the device.

screenshot

Use case

Field workers with limited network connectivity can use exported tiles as a basemap for use offline.

How to use the sample

Pan and zoom into the desired area, making sure the area is within the red boundary. Click 'Export tiles' to start the process. The application will export tiles from the raster imagery baselayer and not include the vector labels baselayer from the Imagery BasemapStyle. On successful completion you will see a preview of the downloaded tile package.

How it works

  1. Create an ArcGISTiledLayer from a raster baselayer of a basemap style.
  2. Create an ExportTileCacheTask, passing in the URL of the tiled layer.
  3. Create default ExportTileCacheParameters for the task, specifying extent, minimum scale and maximum scale. Limiting the difference between the minimum and maximum scales will decrease the size of the resulting tile package and the time it takes to create.
  4. Use the parameters and a path to create an ExportTileCacheJob from the task.
  5. Start the job, and when it completes successfully, get the resulting TileCache.
  6. Use the tile cache to create an ArcGISTiledLayer, and display it in the map.

Relevant API

  • ArcGISTiledLayer
  • ExportTileCacheJob
  • ExportTileCacheParameters
  • ExportTileCacheTask
  • TileCache

Additional information

ArcGIS tiled layers do not support reprojection, query, select, identify, or editing. See the Layer types discussion in the developers guide to learn more about the characteristics of ArcGIS tiled layers.

At this time, ExportTileCacheTask only supports raster layers.

Tags

cache, download, offline

Sample Code

ExportTiles.cppExportTiles.cppExportTiles.hExportTiles.qml
Use dark colors for code blocksCopy
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
// [WriteFile Name=ExportTiles, Category=Layers]
// [Legal]
// Copyright 2016 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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

// sample headers
#include "ExportTiles.h"

// ArcGIS Maps SDK headers
#include "ArcGISTiledLayer.h"
#include "Basemap.h"
#include "Envelope.h"
#include "Error.h"
#include "ExportTileCacheJob.h"
#include "ExportTileCacheParameters.h"
#include "ExportTileCacheTask.h"
#include "GeometryEngine.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "SpatialReference.h"
#include "TaskTypes.h"
#include "TileCache.h"
#include "Viewpoint.h"

// Qt headers
#include <QFuture>
#include <QUrl>
#include <QUuid>

using namespace Esri::ArcGISRuntime;

ExportTiles::ExportTiles(QQuickItem* parent) :
  QQuickItem(parent)
{
}

ExportTiles::~ExportTiles() = default;

void ExportTiles::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ExportTiles>("Esri.Samples", 1, 0, "ExportTilesSample");
}

void ExportTiles::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // create a tiled basemap
  Basemap* basemap = new Basemap(BasemapStyle::ArcGISImagery, this);

  // create an export tile cache task when basemap has finished loading
  connect(basemap, &Basemap::doneLoading, this, [this]()
  {
    if (!m_map->basemap()->baseLayers()->isEmpty())
      createExportTileCacheTask();
  });

  // create a new map instance
  m_map = new Map(basemap, this);

  // set an initial viewpoint
  m_map->setInitialViewpoint(Viewpoint(35, -117, 1e7));

  // set map on the map view
  m_mapView->setMap(m_map);
}

void ExportTiles::createExportTileCacheTask()
{
  // Get a tile layer from the basemap
  ArcGISTiledLayer* tiledLayer = dynamic_cast<ArcGISTiledLayer*>(m_map->basemap()->baseLayers()->at(0));

  // create the task with the url and load it
  m_exportTileCacheTask = new ExportTileCacheTask(tiledLayer->url(), this);

  connect(m_exportTileCacheTask, &ExportTileCacheTask::doneLoading, this, [this](const Error& error)
  {
    if (!error.isEmpty())
    {
      emit updateStatus("Export failed");
      emit hideWindow(5000, false);
    }
  });

  m_exportTileCacheTask->load();
}

void ExportTiles::exportTileCacheFromCorners(double xCorner1, double yCorner1, double xCorner2, double yCorner2)
{
  // create an envelope from the QML rectangle corners
  const Point corner1 = m_mapView->screenToLocation(xCorner1, yCorner1);
  const Point corner2 = m_mapView->screenToLocation(xCorner2, yCorner2);
  const Envelope extent(corner1, corner2);
  const Geometry tileCacheExtent = GeometryEngine::project(extent, SpatialReference::webMercator());

  // generate parameters
  m_exportTileCacheTask->createDefaultExportTileCacheParametersAsync(tileCacheExtent, m_mapView->mapScale(), m_mapView->mapScale() * 0.1)
      .then(this, [this](const ExportTileCacheParameters& parameters)
      {
        onDefaultExportTileCacheParametersCompleted_(parameters);
      });
}

void ExportTiles::onDefaultExportTileCacheParametersCompleted_(const ExportTileCacheParameters& parameters)
{
  //! [ExportTiles start job]
  // execute the task and obtain the job
  ExportTileCacheJob* exportJob = m_exportTileCacheTask->exportTileCache(parameters, m_tempPath.path() + "/offlinemap.tpkx");

  // check if there is a valid job
  if (exportJob)
  {
    connect(exportJob, &ExportTileCacheJob::progressChanged, this, [this, exportJob]()
    {
      m_exportTilesProgress = exportJob->progress();
      emit exportTilesProgressChanged();
    });

    // connect to the job's status changed signal
    connect(exportJob, &ExportTileCacheJob::statusChanged, this, [this, exportJob](JobStatus jobStatus)
    {
      // connect to the job's status changed signal to know once it is done
      switch (jobStatus) {
        case JobStatus::Failed:
          emit updateStatus("Export failed");
          emit hideWindow(5000, false);
          break;
        case JobStatus::NotStarted:
          emit updateStatus("Job not started");
          break;
        case JobStatus::Paused:
          emit updateStatus("Job paused");
          break;
        case JobStatus::Started:
          emit updateStatus("In progress...");
          break;
        case JobStatus::Succeeded:
          emit updateStatus("Adding TPKX...");
          emit hideWindow(1500, true);
          displayOutputTileCache(exportJob->result());
          break;
        default:
          break;
      }
    });

    // start the export job
    exportJob->start();
  }
  //! [ExportTiles start job]
  else
  {
    emit updateStatus("Export failed");
    emit hideWindow(5000, false);
  }
}

// display the tile cache once the task is complete
void ExportTiles::displayOutputTileCache(TileCache* tileCache)
{
  // create a new tiled layer from the output tile cache
  ArcGISTiledLayer* tiledLayer = new ArcGISTiledLayer(tileCache, this);

  // add the new layer to a basemap
  Basemap* basemap = new Basemap(tiledLayer, this);

  // set the new basemap on the map
  m_map->setBasemap(basemap);

  // zoom to the new layer and hide window once loaded
  connect(tiledLayer, &ArcGISTiledLayer::doneLoading, this, [this, tiledLayer]()
  {
    if (tiledLayer->loadStatus() == LoadStatus::Loaded)
    {
      const double prevMapScale = m_mapView->mapScale();
      m_map->setMinScale(prevMapScale);
      m_map->setMaxScale(prevMapScale * 0.1);
      m_mapView->setViewpointScaleAsync(prevMapScale * 0.5);
    }
  });
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.