Export vector tiles

View on GitHubSample viewer app

Export tiles from an online vector tile service.

screenshot

Use case

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

How to use the sample

When the vector tiled layer loads, zoom in to the extent you want to export. The red box shows the extent that will be exported. Click the "Export area" button to start the job. When finished, a dialog will show the exported result as a new basemap.

How it works

  1. Create an ArcGISVectorTiledLayer from the map's base layers.
  2. Create an ExportVectorTilesTask using the vector tiled layer's URL.
  3. Create default ExportVectorTilesParameters from the task, specifying extent and maximum scale.
  4. Create a ExportVectorTilesJob from the task using the parameters, and specifying a vector tile cache path and an item resource path. The resource path is required if you want to export the tiles with the style.
  5. Start the job, and once it completes successfully, get the resulting ExportVectorTilesResult.
  6. Get the VectorTileCache and ItemResourceCache from the result to create an ArcGISVectorTiledLayer that can be displayed to the map view.

Relevant API

  • ArcGISVectorTiledLayer
  • ExportVectorTilesJob
  • ExportVectorTilesParameters
  • ExportVectorTilesResult
  • ExportVectorTilesTask
  • ItemResourceCache
  • VectorTileCache

Additional information

NOTE: Downloading tiles for offline use requires authentication with the web map's server. To use this sample, you will require an ArcGIS Online account.

Vector tiles have high drawing performance and smaller file size compared to regular tiled layers due to consisting solely of points, lines, and polygons. Visit Layer types on the ArcGIS Online Developer's portal to learn more.

Tags

cache, download, offline, vector, vector tiled layer

Sample Code

ExportVectorTiles.cppExportVectorTiles.cppExportVectorTiles.hExportVectorTiles.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
// [WriteFile Name=ExportVectorTiles, Category=Layers]
// [Legal]
// Copyright 2022 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 "ExportVectorTiles.h"

// ArcGIS Maps SDK headers
#include "ArcGISVectorTiledLayer.h"
#include "Basemap.h"
#include "Envelope.h"
#include "Error.h"
#include "ExportVectorTilesJob.h"
#include "ExportVectorTilesParameters.h"
#include "ExportVectorTilesResult.h"
#include "ExportVectorTilesTask.h"
#include "GeometryEngine.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "ServiceTypes.h"
#include "SimpleLineSymbol.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "Viewpoint.h"

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

using namespace Esri::ArcGISRuntime;

ExportVectorTiles::ExportVectorTiles(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISStreetsNight, this))
{
}

ExportVectorTiles::~ExportVectorTiles() = default;

void ExportVectorTiles::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ExportVectorTiles>("Esri.Samples", 1, 0, "ExportVectorTilesSample");
}

MapQuickView* ExportVectorTiles::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void ExportVectorTiles::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);
  m_mapView->setViewpointAsync(Viewpoint(34.049, -117.181, 1e4));

  m_graphicsOverlay = new GraphicsOverlay(this);
  m_mapView->graphicsOverlays()->append(m_graphicsOverlay);

  // Create the graphic that will be used to show the export extent
  m_exportAreaGraphic = new Graphic(this);
  m_exportAreaGraphic->setSymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::green, 3, this));

  m_graphicsOverlay->graphics()->append(m_exportAreaGraphic);

  emit mapViewChanged();
}

void ExportVectorTiles::startExport(double xSW, double ySW, double xNE, double yNE)
{
  if (!m_map->basemap() || m_map->basemap()->baseLayers()->isEmpty() || m_map->basemap()->baseLayers()->first()->layerType() != LayerType::ArcGISVectorTiledLayer)
    return;

  // Get the first layer of the basemap baselayers as a vector tiled layer for export
  ArcGISVectorTiledLayer* vectorTiledLayer = static_cast<ArcGISVectorTiledLayer*>(m_map->basemap()->baseLayers()->first());
  ExportVectorTilesTask* exportTask = new ExportVectorTilesTask(vectorTiledLayer->url(), this);

  // Create an envelope from the QML rectangle corners
  const Point corner1 = m_mapView->screenToLocation(xSW, ySW);
  const Point corner2 = m_mapView->screenToLocation(xNE, yNE);
  const Envelope extent = Envelope(corner1, corner2);
  // Normalize the central meridian to export tiles if the export area crosses the antemeridian
  const Geometry exportArea = GeometryEngine::normalizeCentralMeridian(GeometryEngine::project(extent, vectorTiledLayer->spatialReference()));

  m_exportAreaGraphic->setGeometry(exportArea);

  // Instantiate export parameters to create the export job with
  exportTask->createDefaultExportVectorTilesParametersAsync(exportArea, m_mapView->mapScale() * 0.1)
      .then(this, [this, exportTask](ExportVectorTilesParameters exportParameters)
  {
    // Using the reduced fonts service will reduce the download size of a vtpk by around 80 Mb
    // It is useful for taking the basemap offline but not recommended if you plan to later upload the vtpk
    exportParameters.setEsriVectorTilesDownloadOption(EsriVectorTilesDownloadOption::UseReducedFontsService);

    // Create a path to store the vector tile package, the file cannot already exist
    const QString vectorTileCachePath = m_tempDir.path() + QString("/vectorTiles%1.vtpk").arg(QDateTime::currentMSecsSinceEpoch());
    // Create a path to an empty directory to store the styling resources (in this case, the night mode version of the layer)
    const QString itemResourcePath = m_tempDir.path() + QString("/itemResources%1").arg(QDateTime::currentMSecsSinceEpoch());

    // Create a job that will download the vector tiles to the given paths
    m_exportJob = exportTask->exportVectorTiles(exportParameters, vectorTileCachePath, itemResourcePath);

    connect(m_exportJob, &ExportVectorTilesJob::jobDone, this, [this]()
    {
      if (m_exportJob->error().isEmpty())
      {
        VectorTileCache* vectorTileCache = m_exportJob->result()->vectorTileCache();
        ItemResourceCache* itemResourceCache = m_exportJob->result()->itemResourceCache();

        // Create a vector tiled layer when the download is completed
        ArcGISVectorTiledLayer* exportedLayer = new ArcGISVectorTiledLayer(vectorTileCache, itemResourceCache, this);
        m_map->setBasemap(new Basemap(exportedLayer, this));
        m_isUsingOfflineBasemap = true;
        m_exportJob->disconnect();
      }
    });

    connect(m_exportJob, &Job::progressChanged, this, [this]()
    {
      m_exportProgress = m_exportJob->progress();
      emit exportProgressChanged();
    });

    connect(m_exportJob, &Job::statusChanged, this, [this](JobStatus s)
    {
      m_jobStatus = static_cast<int>(s);
      emit jobStatusChanged();
    });
    m_exportJob->start();
  });
}

void ExportVectorTiles::cancel()
{
  m_exportJob->cancelJobAsync().then(this, [this](bool succeeded)
  {
    emit jobCancelDone(succeeded);;
  });
  reset();
}

void ExportVectorTiles::reset()
{
  if (m_isUsingOfflineBasemap)
  {
    m_map->setBasemap(new Basemap(BasemapStyle::ArcGISStreetsNight, this));
    m_isUsingOfflineBasemap = false;
  }

  m_exportAreaGraphic->setGeometry(Geometry());
  m_jobStatus = 0; // Override the job status to set the UI button to "Export area" again
  emit jobStatusChanged();
}

int ExportVectorTiles::exportProgress() const
{
  return m_exportProgress;
}

int ExportVectorTiles::jobStatus() const
{
  return m_jobStatus;
}

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