Skip to content

Tile Generation

Introduction to Tile Generation

In web maps applications, vector tiles are used to display map data. These tiles are generated from geometric feature data and can be used to visualize map data in web maps, mobile apps, or desktop GIS software. SedonaMaps provides a set of APIs to generate vector tiles from a given set of geometries. These tiles will render more efficiently for large datasets than rendering feature formats (e.g. geojson) directly, and allow for a higher degree of customizability of their display.

Import Tile Generation Functions

To leverage the tile generation functionalities offered by SedonaMaps, you need to import the VectorTile class from the sedonamaps module:

from sedonamaps.core import VectorTiles
import com.wherobots.sedonamaps.VectorTiles.{GenerationConfig, generate}
import com.wherobots.sedonamaps.VectorTiles.GenerationConfig;
import com.wherobots.sedonamaps.VectorTiles.generate;

Loading Feature Data

Once you've accessed the VectorTile class, the next step is to load feature data that will be used to generate the tiles. You can load this data the same way you would load data for other SedonaMaps operations. You will want to have a DataFrame with a geometry column and a layer column. The geometry column should contain the geometries of the features to be included in the tiles. The layer column should contain a string of the layer that each feature belongs to. Layers are used to group features together and can be used to style the features in the tiles.

featureDf = sedona.read.parquet("path/to/features") \
  .withColumn("layer", lit("roads")) \
  .withColumn("geometry", st_geomFromWKT("geometry"))
val featureDf = sedona.read.parquet("path/to/features")
  .withColumn("layer", lit("roads"))
  .withColumn("geometry", st_geomFromWKT($"geometry"))
Dataset featureDf = sedona.read.parquet("path/to/features")
        .withColumn("layer", lit("roads"))
        .withColumn("geometry", st_geomFromWKT(col("geometry")));

Create Sedona DataFrame of Tiles

Once you have a dataframe ready for tile generation, you can use the generate method to create a Sedona DataFrame. In the example below, tiles are generate from zoom 5 to 17 inclusive with a resolution of 4096 and a buffer factor of .1. the GenerationConfig contruct can be passed with no arguments to use the default values.

tilesDf = tilesDf = VectorTiles.generate(featureDf, VectorTiles.GenerationConfig(5, 17, 4096, .1))
val tilesDf = generate(featureDf, GenerationConfig(5, 17, 4096, .1))
Dataset tilesDf = generate(featureDf, new GenerationConfig(5, 17, 4096, .1));

Write tiles to File Storage

Warning

When generating tiles from zoom 0 to x, you will generate up to 4^(x+1) tiles (files) depending on the spatial extent of the data. This can be a very large number (billions) of tiles. The runtime and s3 transfer costs can thus also be high.

Tiles can be written to file storage using the mvt format. You can target any storage destination that the spark runtime supports. The mvt format is the standard file format for slippy tiles. Rather than the standard part files for other file formats, mvt writes out a directory structure that mirrors the z/x/y layout of the tiles.

tilesDf.write.format("mvt").save("s3://bucket/prefix")
tilesDf.write.format("mvt").save("s3://bucket/prefix")
tilesDf.write.format("mvt").save("s3://bucket/prefix");

Visualizing the result using Pydeck

To visualize the map matching result using pydeck, you can use the following code. If you wrote the tiles to s3, you can use s3 as your tile server. This requires the files be publicly available. Private object support is forthcoming.

import pydeck

pydeck.Deck(
  layers=[
    pydeck.Layer(
      "MVTLayer",
      "https://<BUCKET_NAME>.s3.<REGION>.amazonaws.com/<PREFIX>/{z}/{x}/{y}.mvt",
    )
  ],
  map_style="light",
)

Below is a minimally styled visual representation of example tiles:

  • Grey lines are the basemap provided by pydeck
  • Black lines are the contents of the tiles generated from Overture data.

PyDeck

Learn more about SedonaMaps

As a comprehensive map application toolbox, SedonaMaps provides many off-the-shelf scalable tools. In this tutorial, we just focus on a minimum example. Detailed explanation of each tool can be found at References.


Last update: March 9, 2024 00:18:26