Skip to content

VectorTileOverlay and Style Documents (Blazor)

Namespace: ThinkGeo.UI.Blazor
Applies to: ThinkGeo Web Maps, Blazor on .NET 8 or later
Tested with: ThinkGeo.UI.Blazor 15.1.0, .NET 10, Chrome


Overview

VectorTileOverlay puts your own data on a Blazor map: shapefiles, database tables, features built in code. Your code reads the data, the overlay cuts it into vector tiles, and the browser draws the tiles from a style document. This guide covers how those pieces connect, how to change what the map shows while it runs, and what to check when nothing is drawn. It continues the WorldCapitals example of the Web Maps Quick Start.


The pieces

<VectorTileOverlay Id="capitals" FeatureSources="@capitals" StyleJson="@capitalsJson" />
Piece Role
FeatureSources The data, as FeatureSources: a ShapeFileFeatureSource, an InMemoryFeatureSource, a database source. Each is keyed by a name.
Vector tiles The overlay cuts the sources into vector tiles as the browser asks for them, in Web Mercator. Under Interactive Server they are cut on the server and travel over the circuit; nothing is served from an address of yours.
StyleJson A style document: which tiles are drawn, how, and in what order. The browser draws them, so a changed style costs no new tiles.

Each key in FeatureSources is a source layer: the name the style's layers give as "source-layer". A style layer whose "source-layer" matches no key draws nothing, and nothing says so.

All the feature sources of one overlay make up one source of the style, named by the overlay's SourceId. The style need not declare it, and its layers need not name it: the overlay adds the source, and a layer without a "source" reads it. A style that declares sources of its own, for example a second tile service, sets SourceId to say which of them the feature sources are.

Small in-memory sources are not cut at all. Up to PushThreshold features, 5,000 by default, in-memory sources are handed to the browser whole, once, and panning and zooming cost the server nothing after that. Larger sources, and every source that reads a file or a database, are cut as the browser asks.


Projections

Vector tiles are Web Mercator, and so is a map with MapUnit="GeographyUnit.Meter". Data in another projection needs a ProjectionConverter on its source, which reprojects the features as they are read:

new ShapeFileFeatureSource(Path.Combine(AppContext.BaseDirectory, "Data", "WorldCapitals.shp"))
{
    // The file is in decimal degrees, EPSG:4326.
    ProjectionConverter = new ProjectionConverter(4326, 3857)
}

Without it, coordinates in degrees are read as meters, and the whole world lands in a few pixels near the point where the equator crosses the prime meridian. The .prj file next to a shapefile says what projection it is in.


Style documents

The style document is the one the Styling guides describe for the desktop map: "layers" drawn in order, each with a "type", a "source-layer", and "paint" and "layout" properties. Expressions such as ["get", "CITY_NAME"] read a feature's columns; every column of the source is in the tiles.

A style can be given two ways:

  • StyleJson: the document as text, or the address of one.
  • Style: a MapStyle object composed in code, with images for icons. A MapStyle changed in place is drawn again on RedrawAsync.

Use one or the other, not both.


Changing what the map shows

To change Do Tiles fetched again
Colours, sizes, filters, labels Give StyleJson a new string. No
One style layer on or off Set its "layout": { "visibility": "none" } in the new string. No
The whole overlay on or off Bind IsVisible. No
The data Change the source, then await overlay.RedrawAsync(). Yes, or handed over again if in memory

A new StyleJson value is a parameter change: the overlay applies it when the page renders, without RedrawAsync (Two kinds of change).

Example: a style control and a layer toggle

The WorldCapitals data, sized by population rank, with a colour picker, labels that can be switched off, and the whole overlay on a checkbox. Set the project up as the Quick Start does, with the shapefile in Data.

@page "/vector"
@rendermode InteractiveServer

<select @bind="color">
    <option value="#FFFFFF">White</option>
    <option value="#E07B00">Orange</option>
    <option value="#3060C8">Blue</option>
</select>
<label><input type="checkbox" @bind="showNames" /> Names</label>
<label><input type="checkbox" @bind="showCapitals" /> Capitals</label>

<MapView Id="map" MapUnit="GeographyUnit.Meter"
         Center="@(new PointShape(0, 2000000))" Zoom="2" Width="900" Height="600">
    <OverlaysSetting>
        <ThinkGeoCloudVectorMapsOverlay Id="basemap" MapType="ThinkGeoCloudVectorMapsMapType.Light"
                                        ApiKey="SGjwpxF60knqZi3R0A9RRE3GqiuRA1LsnxHMot7rJ58~" />
        <VectorTileOverlay Id="capitals" FeatureSources="@capitals" StyleJson="@CapitalsJson"
                           IsVisible="@showCapitals" />
    </OverlaysSetting>
</MapView>

@code {
    private string color = "#FFFFFF";
    private bool showNames = true;
    private bool showCapitals = true;

    // The key, "WorldCapitals", is the "source-layer" the style's layers name.
    private readonly GeoCollection<FeatureSource> capitals = new()
    {
        { "WorldCapitals", new ShapeFileFeatureSource(Path.Combine(AppContext.BaseDirectory, "Data", "WorldCapitals.shp"))
            {
                ProjectionConverter = new ProjectionConverter(4326, 3857)
            }
        }
    };

    // A new string is a new parameter value: the overlay applies it after the page renders,
    // without fetching its tiles again.
    private string CapitalsJson => $$"""
    {
      "layers": [
        { "id": "capitals", "type": "circle", "source-layer": "WorldCapitals",
          "paint": {
            "circle-radius": ["match", ["get", "POP_RANK"], 1, 7, 2, 5, 4],
            "circle-color": "{{color}}",
            "circle-stroke-color": "#000000",
            "circle-stroke-width": 2 } },
        { "id": "names", "type": "symbol", "source-layer": "WorldCapitals",
          "layout": {
            "visibility": "{{(showNames ? "visible" : "none")}}",
            "text-field": ["get", "CITY_NAME"],
            "text-font": ["Noto Sans Regular"],
            "text-size": 12,
            "text-anchor": "top",
            "text-offset": [0, 0.8] },
          "paint": { "text-color": "#333333", "text-halo-color": "#FFFFFF", "text-halo-width": 1.5 } }
      ]
    }
    """;
}

The style is a C# raw string literal: $$""" lets the JSON keep its single braces, and {{color}} puts the field's value in.


Vector or classic

A LayerOverlay draws the same data the classic way: FeatureLayers with styles set in code, drawn into image tiles by SkiaSharp where your code runs.

VectorTileOverlay LayerOverlay
Drawn by The browser SkiaSharp, on the server under Interactive Server
Styled with A style document Style objects in C#
A style change costs Nothing: the tiles stay Every tile drawn again
On Linux Nothing extra fontconfig on the server (Deployment Guide)
In WebAssembly Works as is Needs SkiaSharp's WebAssembly build

New code uses VectorTileOverlay. LayerOverlay is there for ThinkGeo 14 code and for styles that only exist as classic styles; Classic Rendering describes them. The Styling Your Data guide shows how classic styles map to a style document.


When nothing is drawn

Check Why
Every "source-layer" in the style is a key of FeatureSources, spelled the same. A layer with no matching source layer draws nothing.
The source has a ProjectionConverter if its data is not in Web Mercator. Degrees read as meters put everything near 0, 0.
The file is where the code looks: AppContext.BaseDirectory is the build output, so the project must copy the data there. A missing file fails every tile request. The browser's console shows ThinkGeo async callback failed; turn on detailed circuit errors to see the .NET exception there.
The style is valid JSON. The browser cannot read an invalid document: the overlay is left undrawn, and the browser's console names the overlay and the error.
The layer's "type" fits the geometry: circle or symbol for points, line for lines, fill for areas. A fill layer draws nothing for points.

See also