Export vector tiles

View on GitHubSample viewer app

Export tiles from an online vector tile service.

Image of export vector tiles

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 Vector Tiles" button to start the job. An error will show if the extent is larger than the maximum limit allowed. When finished, a dialog will show the exported result in a new map view.

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 from the result to create an ArcGISVectorTiledLayer that can be displayed to the map view.

Relevant API

  • ArcGISVectorTiledLayer
  • ExportVectorTilesJob
  • ExportVectorTilesParameters
  • ExportVectorTilesResult
  • ExportVectorTilesTask
  • 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. However, in ArcGIS Runtime SDK they cannot be displayed in scenes. Visit the ArcGiS Online Developer's portal to Learn more about the characteristics of ArcGIS vector tiled layers.

Tags

cache, download, offline, vector

Sample Code

MainActivity.kt
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* 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.
 *
 */

package com.esri.arcgisruntime.sample.exportvectortiles

import android.app.AlertDialog
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.concurrent.Job
import com.esri.arcgisruntime.geometry.Envelope
import com.esri.arcgisruntime.layers.ArcGISVectorTiledLayer
import com.esri.arcgisruntime.loadable.LoadStatus
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.Basemap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.exportvectortiles.databinding.ActivityMainBinding
import com.esri.arcgisruntime.sample.exportvectortiles.databinding.ProgressDialogLayoutBinding
import com.esri.arcgisruntime.symbology.SimpleLineSymbol
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesJob
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesParameters
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesResult
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesTask
import java.io.File


class MainActivity : AppCompatActivity() {

    private val TAG = MainActivity::class.java.simpleName

    private var downloadArea: Graphic? = null
    private var exportVectorTilesJob: ExportVectorTilesJob? = null
    private var dialog: AlertDialog? = null
    private var isJobFinished: Boolean = true

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val mapPreviewLayout: ConstraintLayout by lazy {
        activityMainBinding.mapPreviewLayout
    }

    private val exportVectorTilesButton: Button by lazy {
        activityMainBinding.exportVectorTilesButton
    }

    private val previewMapView: MapView by lazy {
        activityMainBinding.previewMapView
    }

    private val dimBackground: View by lazy {
        activityMainBinding.dimBackground
    }

    private val closeButton: Button by lazy {
        activityMainBinding.closeButton
    }

    private val previewTextView: TextView by lazy {
        activityMainBinding.previewTextView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

        // authentication with an API key or named user is required to access basemaps and other
        // location services
        ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)


        // create a graphic to show a red outline square around the vector tiles to be downloaded
        downloadArea = Graphic().apply {
            symbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 2F)
        }
        // create a graphics overlay and add the downloadArea graphic
        val graphicsOverlay = GraphicsOverlay().apply {
            graphics.add(downloadArea)
        }

        mapView.apply {
            // set the map to BasemapType navigation night
            map = ArcGISMap(BasemapStyle.ARCGIS_STREETS_NIGHT)
            // disable rotation
            rotation = 0F
            // set the viewpoint of the sample to ESRI Redlands, CA campus
            setViewpoint(Viewpoint(34.056295, -117.195800, 100000.0))
            // add the graphics overlay to the MapView
            graphicsOverlays.add(graphicsOverlay)

            // update the red square whenever the viewpoint changes
            addViewpointChangedListener {
                updateDownloadAreaGeometry()
            }

            // when the map has loaded, create a vector tiled layer from the basemap and export the tiles
            map.addDoneLoadingListener {
                if (map.loadStatus == LoadStatus.LOADED) {
                    // check that the layer from the basemap is a vector tiled layer
                    val vectorTiledLayer = map.basemap.baseLayers[0] as ArcGISVectorTiledLayer
                    handleExportButton(vectorTiledLayer)
                } else {
                    showError(map.loadError.message.toString())
                }
            }
        }
    }

    /**
     * Sets up the ExportVectorTilesTask using the [vectorTiledLayer]
     * on export button click. Then call handleExportVectorTilesJob()
     */
    private fun handleExportButton(vectorTiledLayer: ArcGISVectorTiledLayer) {
        exportVectorTilesButton.setOnClickListener {
            // update the download area's geometry using the current viewpoint
            updateDownloadAreaGeometry()
            // create a new export vector tiles task
            val exportVectorTilesTask = ExportVectorTilesTask(vectorTiledLayer.uri)
            // the max scale parameter is set to 10% of the map's scale so the
            // number of tiles exported are within the vector tiled layer's max tile export limit
            val exportVectorTilesParametersFuture = exportVectorTilesTask
                .createDefaultExportVectorTilesParametersAsync(
                    downloadArea?.geometry,
                    mapView.mapScale * 0.1
                )

            exportVectorTilesParametersFuture.addDoneListener {
                try {
                    // get the loaded export vector tile parameters
                    val exportVectorTilesParameters =
                        exportVectorTilesParametersFuture.get()
                    // only start a new job, if the previous job has finished cancelling
                    if(isJobFinished){
                        // create a job to export vector tiles
                        handleExportVectorTilesJob(
                            exportVectorTilesParameters,
                            exportVectorTilesTask
                        )
                    } else {
                        showError("Previous job is cancelling asynchronously")
                    }
                } catch (e: Exception) {
                    showError(e.message.toString())
                }
            }
        }
    }

    /**
     * Start the export vector tiles job using [exportVectorTilesTask] and the
     * [exportVectorTilesParameters]. The vector tile package is exported as "file.vtpk"
     */
    private fun handleExportVectorTilesJob(
        exportVectorTilesParameters: ExportVectorTilesParameters,
        exportVectorTilesTask: ExportVectorTilesTask
    ) {
        // create a .vtpk and directory in the app's cache for saving exported tiles
        val vtpkFile = File(externalCacheDir, "/StyleItemResources/myVectorTiles.vtpk")
        val resDir = File(externalCacheDir, "/StyleItemResources")
        resDir.deleteRecursively()
        resDir.mkdir()

        // create a job with the export vector tile parameters
        // and exports the vector tile package as "file.vtpk"
        exportVectorTilesJob = exportVectorTilesTask.exportVectorTiles(
            exportVectorTilesParameters,
            vtpkFile.absolutePath, resDir.absolutePath
        ).apply {
            // inflate the progress dialog
            val dialogLayoutBinding = createProgressDialog()
            // start the export vector tile cache job
            start()
            // since job is now started, set to false
            isJobFinished = false

            // display the progress dialog
            dialog?.show()
            // update the dialog using the progress of the job
            addProgressChangedListener {
                dialogLayoutBinding.progressBar.progress = progress
                dialogLayoutBinding.progressTextView.text = "$progress% completed"
            }
            // on job done loading
            addJobDoneListener {
                dialog?.dismiss()
                if (status == Job.Status.SUCCEEDED) {
                    // display the map preview using the result from the completed job
                    showMapPreview(result)
                } else {
                    showError(error.message.toString())
                    isJobFinished = true
                }
            }
        }
    }

    /**
     * Updates the [downloadArea]'s geometry on ViewPoint change
     * or when export tiles button is clicked.
     */
    private fun updateDownloadAreaGeometry() {
        try {
            mapView.apply {
                if (mapView.map.loadStatus == LoadStatus.LOADED) {
                    // upper left corner of the downloaded tile cache area
                    val minScreenPoint: android.graphics.Point = android.graphics.Point(150, 175)
                    // lower right corner of the downloaded tile cache area
                    val maxScreenPoint: android.graphics.Point = android.graphics.Point(
                        mapView.width - 150,
                        mapView.height - 250
                    )
                    // convert screen points to map points
                    val minPoint: com.esri.arcgisruntime.geometry.Point =
                        mapView.screenToLocation(minScreenPoint)
                    val maxPoint: com.esri.arcgisruntime.geometry.Point =
                        mapView.screenToLocation(maxScreenPoint)
                    // use the points to define and return an envelope
                    downloadArea?.geometry = Envelope(minPoint, maxPoint)
                }
            }
        } catch (e: Exception) {
            // Silently fail, since mapView has not been rendered yet.
        }
    }

    /**
     * Create a progress dialog to track the progress of the [exportVectorTilesJob]
     */
    private fun createProgressDialog(): ProgressDialogLayoutBinding {
        val dialogLayoutBinding = ProgressDialogLayoutBinding.inflate(layoutInflater)
        val dialogBuilder = AlertDialog.Builder(this@MainActivity).apply {
            setTitle("Exporting vector tiles")
            setNegativeButton("Cancel job") { _, _ ->
                // cancels the export job asynchronously
                val future = exportVectorTilesJob?.cancelAsync()
                future?.addDoneListener {
                    // cancel is completed, so set to true
                    isJobFinished = true
                }
            }
            setCancelable(false)
            setView(dialogLayoutBinding.root)
        }
        dialog = dialogBuilder.create()
        return dialogLayoutBinding
    }

    /**
     * Display the preview of the exported map using the
     * [vectorTilesResult] from the completed job
     */
    private fun showMapPreview(vectorTilesResult: ExportVectorTilesResult?) {
        // get the layer exported for the preview MapView
        val vectorTiledLayer = ArcGISVectorTiledLayer(
            vectorTilesResult?.vectorTileCache,
            vectorTilesResult?.itemResourceCache
        )
        // set up the preview MapView
        previewMapView.apply {
            map = ArcGISMap(Basemap(vectorTiledLayer))
            setViewpoint(mapView.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE))
        }
        // control UI visibility
        previewMapView.getChildAt(0).visibility = View.VISIBLE
        show(closeButton, dimBackground, previewTextView, previewMapView)
        exportVectorTilesButton.visibility = View.GONE

        // required for some Android devices running older OS (combats Z-ordering bug in Android API)
        mapPreviewLayout.bringToFront()
    }

    /**
     * Makes the given views in the UI visible.
     * @param views the views to be made visible
     */
    private fun show(vararg views: View) {
        for (view in views) {
            view.visibility = View.VISIBLE
        }
    }

    /**
     * Makes the given views in the UI visible.
     * @param views the views to be made visible
     */
    private fun hide(vararg views: View) {
        for (view in views) {
            view.visibility = View.INVISIBLE
        }
    }

    /**
     * Called when close preview MapView is clicked
     */
    fun clearPreview(view: View) {
        // control UI visibility
        hide(closeButton, dimBackground, previewTextView, previewMapView)
        show(exportVectorTilesButton, mapView)
        downloadArea?.isVisible = true
        // required for some Android devices running older OS (combats Z-ordering bug in Android API)
        mapView.bringToFront()
    }

    private fun showError(message: String) {
        Log.e(TAG, message)
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

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