Style Documents¶
A style document is the JSON that tells ThinkGeo what to draw and how. It follows the MapLibre Style Specification, an open format, so the documents, editors and knowledge of that ecosystem carry over. ThinkGeo reads the document with its own parser and draws it with its own renderer: on the desktop and MAUI MapView.Basemap, as CPU-drawn tiles in a classic ThinkGeoVectorTileOverlay, and onto paper through MapStyleFeatureAsyncLayer. In the browser the Blazor VectorTileOverlay hands the document to MapLibre GL JS, the specification's reference renderer, and your features with it.
In code the document is a MapStyle (ThinkGeo.Core.Styling). This page is about the document itself; Basemaps and Styling Your Data are about what to put in it.
Anatomy¶
{
"version": 8,
"sources": {
"parks": { "type": "vector", "tiles": ["https://example.com/tiles/{z}/{x}/{y}.pbf"] }
},
"layers": [
{ "id": "background",
"type": "background",
"paint": { "background-color": "#1E2329" } },
{ "id": "parks-fill",
"type": "fill",
"source": "parks",
"source-layer": "Parks",
"minzoom": 10,
"filter": ["==", ["get", "TYPE"], "park"],
"paint": { "fill-color": "#2E4A3B", "fill-outline-color": "#4F7A5F" } },
{ "id": "parks-name",
"type": "symbol",
"source": "parks",
"source-layer": "Parks",
"layout": { "text-field": ["get", "NAME"], "text-font": ["Noto Sans Bold"], "text-size": 11 },
"paint": { "text-color": "#9AD3A8", "text-halo-color": "#1E2329", "text-halo-width": 1.5 } }
]
}
| Part | What it is |
|---|---|
sources | Where the data is. A vector source is a tile service or a tile archive; a raster source is an image tile service. In ThinkGeo the sources are usually objects you add in code, not entries you write here. |
layers | What to draw, in order: the first layer is at the bottom. |
type | background, fill, line, circle, symbol (icons and text), raster, heatmap, fill-extrusion (3D volumes) or hillshade. |
source, source-layer | Which source the layer reads, and which named layer inside its tiles. A shapefile added to a FeatureSourceVectorTileSource becomes a source-layer of that name. |
filter | An expression that must be true for a feature to be drawn by this layer. |
minzoom, maxzoom | The zoom range in which the layer is drawn. |
layout | Properties that decide what is placed where: visibility, text-field, text-font, text-size, symbol-placement, icon-image, line-cap. |
paint | Properties that decide how it looks once placed: colors, widths, opacity, halos, dash arrays. |
glyphs, sprite | Where label glyphs and icon sheets come from. Optional; see Fonts and Images. |
The short form¶
When a document is handed to one source in code, ThinkGeo fills in what the source already knows: version, sources, each layer's id and source can all be left out. source-layer stays, because it names the layer inside the tiles. The document above, written for a FeatureSourceVectorTileSource holding Parks.shp, becomes:
{
"layers": [
{ "type": "background",
"paint": { "background-color": "#1E2329" } },
{ "type": "fill",
"source-layer": "Parks",
"paint": {
"fill-color": "#2E4A3B",
"fill-outline-color": "#4F7A5F" }
},
{ "type": "symbol",
"source-layer": "Parks",
"layout": {
"text-field": ["get", "NAME"],
"text-font": ["Noto Sans Bold"],
"text-size": 11 },
"paint": {
"text-color": "#9AD3A8",
"text-halo-color": "#1E2329",
"text-halo-width": 1.5 }
}
]
}
var parks = new FeatureSourceVectorTileSource();
parks.FeatureSources.Add(new ShapeFileFeatureSource(@"Data\Parks.shp"));
Map.Basemap = new GpuBasemap(new MapStyle(parksJson, parks));
A document that stands on its own, with its sources written out, works as well: MapStyle.LoadAsync(pathOrUrl) reads one, and the ThinkGeo Cloud presets are documents of that kind.
Expressions¶
Anywhere a value goes, an expression can go instead. The ones you will use most:
| Expression | Means |
|---|---|
["get", "NAME"] | The value of a column of the feature. |
["geometry-type"] | "Point", "LineString" or "Polygon". |
["zoom"] | The current zoom level; allowed in filter, layout and paint. |
["match", input, v1, out1, v2, out2, fallback] | One output per value: a ValueStyle. |
["step", input, out0, stop1, out1, stop2, out2] | One output per range: a ClassBreakStyle. |
["case", cond1, out1, cond2, out2, fallback] | The first condition that holds. |
["interpolate", ["linear"], ["zoom"], 1, 0.5, 6, 4] | A value that changes smoothly between stops. |
["==", a, b], ["!=", …], [">", …], ["<", …], ["all", …], ["any", …], ["!", …] | Comparisons and logic, mostly in filters. |
["concat", ["get", "NAME"], "\n", "Population: ", ["get", "POP"]] | Text from several columns, the {NAME} template of the classic TextStyle. |
["to-number", ["get", "RANK"]] | Read a text column as a number. |
Styling Your Data shows each of the rule expressions on a world map.
Getting a document onto the map¶
| Code | Use it for |
|---|---|
new MapStyle(ThinkGeoVectorStyles.Light, cloud) | A document by address, drawn from the given source. |
new MapStyle(jsonText, tiles) | A document written inline, drawn from the given source. Text that starts with { is JSON; anything else is an address or a path. |
style.AddStyle(jsonOrAddress, source) | Add another document to a style. Its layers draw above the ones already there. Returns the style, so calls chain. |
style.AddStyle(address, source, StyleLayerType.Line, StyleLayerType.Symbol) | Take only some layer types out of a document: the roads and labels of the street style over your own imagery, for example. |
await MapStyle.LoadAsync(pathOrUrl) | A complete document with its own sources. |
style.ToJson() | The composed document back as text, to save or to inspect. |
The MapStyle then goes into the map with Map.Basemap = new GpuBasemap(style), or replaces the current one with await Map.Basemap.SetStyleAsync(style).
A document that does not compile changes nothing: new MapStyle(json, …) and SetStyleAsync throw with the reason, and the map keeps drawing the last document that worked. The samples with a style editor show that message under the editor; type something wrong into one to see it.
In Blazor the document goes to a VectorTileOverlay, as text or an address in StyleJson, or as a MapStyle in Style; VectorTileOverlay and Style Documents shows both. A StyleJson the browser cannot read leaves that overlay undrawn, and the browser's console says why.
Images: icons and patterns¶
A symbol layer's icon-image, a fill layer's fill-pattern and a line layer's line-pattern name an image. Register the images on the style under those names:
style.Images.Add("dot", PointStyle.CreateSimpleCircleStyle(GeoColor.FromHtml("#5E35B1"), 10, GeoColors.White, 2)); // a point style, drawn once
style.Images.Add("hotel", new GeoImage(@"Icons\hotel.png")); // a bitmap
style.Images.Add("hatch", new GeoHatchBrush(GeoHatchStyle.DiagonalCross, GeoColor.FromHtml("#5E9E62"), GeoColor.FromArgb(110, 180, 224, 182))); // a classic hatch pattern
{ "type": "symbol", "source-layer": "Hotels", "layout": { "icon-image": "hotel", "text-field": ["get", "NAME"], "text-optional": true } }
{ "type": "fill", "source-layer": "Parks", "paint": { "fill-pattern": "hatch" } }
A document with a sprite entry gets its images from that sprite sheet, the way MapLibre does; the Cloud styles work like that.
Fonts¶
text-font names a family and a weight, ["Noto Sans Bold"] or ["Open Sans Semibold"]. Where the shapes of the characters come from depends on the document:
- The document has a
glyphsentry, a template such ashttps://…/{fontstack}/{range}.pbf. Glyphs are fetched from that server in ranges of 256 characters, as MapLibre does. The ThinkGeo Cloud styles work this way. - No
glyphs. The first font oftext-fontis looked up on the machine: a font file inGpuBasemap.LocalFontsDirectory, if you set one, then the installed fonts. When neither has it, the Noto Sans built intoThinkGeo.Coredraws the text, so a document looks the same on a machine that lacks the font. Characters none of these fonts have are drawn from whatever installed font has them.
GpuBasemap.LocalFontsDirectory is a folder of .ttf or .otf files, shared by every map in the process. It is the way to ship a CJK or other script with your application instead of relying on what the machine has installed.
Editing documents¶
- The HowDoI samples Render Points, Lines and Polygons, Render Based on Rules and Advanced Labels have the document in an editor beside the map; edit it and the map follows.
- Maputnik is an open-source visual editor for MapLibre style documents. Export its JSON and hand it to a
MapStyle. - Any text editor works. Keep one property per line and a blank line between layers, and a long document stays readable.
Licensing of data and styles. The style format is open, but the content a document points at may not be: Mapbox-hosted tiles, styles, glyphs and sprites are licensed for Mapbox's own renderers only. ThinkGeo Cloud, OpenMapTiles, MapLibre's demo tiles, PMTiles and data you have licensed for third-party rendering are fine.