Styling Your Data¶
Your data reaches the map in three steps: a FeatureSource reads it, a FeatureSourceVectorTileSource cuts it into tiles, and the style document draws the tiles. Reading the format is the only line that changes between a shapefile, a GeoJSON file, a database table or features built in code. Everything on this page applies to all of them.
From a file or a table to the map¶
using ThinkGeo.Core;
using ThinkGeo.Core.Styling;
var data = new FeatureSourceVectorTileSource();
data.FeatureSources.Add(new ShapeFileFeatureSource(@"Data\Parks.shp")
{
ProjectionConverter = new ProjectionConverter(2276, 3857) // the file's projection -> Web Mercator
});
var style = new MapStyle(ThinkGeoVectorStyles.Dark, cloud); // the basemap
style.AddStyle(parksJson, data); // your data, drawn above it
Map.Basemap = new GpuBasemap(style);
The line that differs:
| Data | FeatureSource |
|---|---|
| Shapefile | ShapeFileFeatureSource(path) |
| GeoJSON | GeoJsonFeatureSource(path) |
| GPX | GpxFeatureSource(path) |
| MapInfo TAB | TabFeatureSource(path) |
| TinyGeo | TinyGeoFeatureSource(path) |
| KML, GeoPDF, GeoPackage and the other GDAL vector formats | KmlGdalFeatureSource, GeoPdfGdalFeatureSource, GdalFeatureSource (package ThinkGeo.Gdal) |
| CAD (DWG, DXF) | CadFeatureSource(path) (package ThinkGeo.Cad) |
| SQLite | SqliteFeatureSource(connectionString, table, idColumn, geometryColumn) |
| Esri File Geodatabase | FileGeoDatabaseFeatureSource(path, table) |
| PostgreSQL / PostGIS | PostgreSqlFeatureSource(connectionString, table, idColumn, srid) |
| SQL Server | SqlServerFeatureSource(connectionString, table, idColumn) |
| WFS, OGC API Features | WfsV2AsyncFeatureSource, OgcApiAsyncFeatureSource |
| Features built in code | InMemoryFeatureSource(columns, features) |
Every source stores its coordinates in some projection: degrees for a GPS track, a state plane for a cadastral file, whatever the surveyor drew in for a CAD file. The map is Web Mercator, so give the source a ProjectionConverter from its EPSG code to 3857. Projections has the details.
The HowDoI samples Vector File Formats and Databases open each of these in turn through one unchanged document.
Naming the source layer¶
The document finds your data by source-layer. A source added with FeatureSources.Add(source) gets its own name, the file name without extension or the table name: Parks.shp becomes "source-layer": "Parks". FeatureSources.Add("parks", source) names it yourself, which you need for an InMemoryFeatureSource or any source that has no name of its own.
One FeatureSourceVectorTileSource can hold several sources, each its own source-layer:
var tiles = new FeatureSourceVectorTileSource();
tiles.FeatureSources.Add(new ShapeFileFeatureSource(@"Data\Parks.shp") { ProjectionConverter = new ProjectionConverter(2276, 3857) });
tiles.FeatureSources.Add(new ShapeFileFeatureSource(@"Data\Streets.shp") { ProjectionConverter = new ProjectionConverter(2276, 3857) });
tiles.FeatureSources.Add(new ShapeFileFeatureSource(@"Data\Hotels.shp") { ProjectionConverter = new ProjectionConverter(2276, 3857) });
Draw by geometry¶
A fill layer draws polygons, a line layer draws lines (and polygon outlines), a circle layer draws points. A document that draws any file, whatever it holds:
{
"layers": [
{ "type": "fill",
"source-layer": "data",
"filter": ["==", ["geometry-type"], "Polygon"],
"paint": {
"fill-color": "rgba(90,140,200,0.35)",
"fill-outline-color": "#8AB4F0" }
},
{ "type": "line",
"source-layer": "data",
"paint": {
"line-color": "#8AB4F0",
"line-width": 2 }
},
{ "type": "circle",
"source-layer": "data",
"filter": ["==", ["geometry-type"], "Point"],
"paint": {
"circle-radius": 4,
"circle-color": "#C0392B",
"circle-stroke-color": "#FFFFFF",
"circle-stroke-width": 1.5 }
}
]
}
A cased road is two line layers on the same source-layer, the wider dark one first:
{ "type": "line", "source-layer": "Streets", "paint": { "line-color": "#13171B", "line-width": 6 } },
{ "type": "line", "source-layer": "Streets", "paint": { "line-color": "#505A66", "line-width": 4 } }
Rules¶
The classic ValueStyle, ClassBreakStyle, FilterStyle and ZoomLevelSet are expressions and properties of a layer. On a world countries file:
By value: match. One color per value of a column, with a fallback last.
{ "type": "fill",
"source-layer": "Countries02",
"paint": {
"fill-outline-color": "#FFFFFF",
"fill-color": ["match", ["get", "CURR_TYPE"],
"Dollar", "rgba(31,119,180,0.8)",
"Franc", "rgba(44,160,44,0.8)",
"Pound", "rgba(214,39,40,0.8)",
"Peso", "rgba(148,103,189,0.8)",
"rgba(233,232,214,0.8)"] }
}
By range: step. The first output is for values below the first stop.
"fill-color": ["step", ["get", "POP_CNTRY"],
"rgba(233,232,214,0.85)",
1000000, "rgba(253,208,162,0.85)",
10000000, "rgba(253,141,60,0.85)",
50000000, "rgba(217,71,1,0.85)",
200000000, "rgba(127,39,4,0.85)"]
Only what passes: filter. A second layer over the first, drawing only the features that match.
{ "type": "fill",
"source-layer": "Countries02",
"paint": { "fill-color": "rgba(233,232,214,0.8)", "fill-outline-color": "#FFFFFF" } },
{ "type": "fill",
"source-layer": "Countries02",
"filter": ["==", ["get", "LANDLOCKED"], "Y"],
"paint": { "fill-color": "rgba(46,139,87,0.8)", "fill-outline-color": "#FFFFFF" } }
Any condition: case.
"fill-color": ["case",
["all", [">", ["get", "POP_CNTRY"], 10000000],
["==", ["get", "LANDLOCKED"], "Y"]],
"rgba(255,255,0,0.8)",
"rgba(233,232,214,0.8)"]
By zoom level. minzoom and maxzoom switch a layer on and off; interpolate and step on ["zoom"] change a value with the zoom.
{ "type": "line",
"source-layer": "Countries02",
"paint": {
"line-color": "#8C8C7A",
"line-width": ["interpolate", ["linear"], ["zoom"], 1, 0.5, 6, 4] }
},
{ "type": "symbol",
"source-layer": "Countries02",
"minzoom": 2,
"maxzoom": 4,
"layout": { "text-field": ["get", "ISO_3DIGIT"], "text-font": ["Noto Sans Bold"], "text-size": 11 },
"paint": { "text-color": "#5A5A50" } },
{ "type": "symbol",
"source-layer": "Countries02",
"minzoom": 4,
"layout": {
"text-field": ["get", "CNTRY_NAME"],
"text-font": ["Noto Sans Regular"],
"text-size": ["step", ["zoom"], 12, 6, 15] },
"paint": { "text-color": "#333333" } }
The HowDoI sample Render Based on Rules has all five in an editor beside the map.
Labels¶
Labels are symbol layers. The engine places them, drops the ones that collide, and keeps them upright as the map rotates.
{ "type": "symbol",
"source-layer": "Streets",
"layout": {
"symbol-placement": "line",
"text-field": ["get", "FULL_NAME"],
"text-font": ["Noto Sans Bold"],
"text-size": 11 },
"paint": {
"text-color": "#C9D1D9",
"text-halo-color": "#1E2329",
"text-halo-width": 1.5 }
}
| Property | Does |
|---|---|
text-field | The text: a column with ["get", "NAME"], or several with concat. |
text-font, text-size | Family and weight, and size in pixels. See Fonts. |
symbol-placement: "point" or "line" | Where the label sits: at the point, or laid along the line so a street name bends with its street. |
text-anchor, text-offset | Which side of the point the text hangs off, and how far, in ems. The anchor can come from the data: ["get", "ANCHOR"]. |
symbol-sort-key | Who wins when labels collide: lowest first. |
text-max-width, text-transform | Wrap long names at a width in ems; "uppercase" without touching the data. |
icon-image with text-optional | An icon with the name beside it; when it is crowded the name yields and the icon stays. |
text-halo-color, text-halo-width | The halo that keeps text readable over the basemap. |
A label from two columns:
"text-field": ["concat",
["get", "NAME"], "\n",
"Population: ", ["get", "POPULATION"]]
The HowDoI sample Advanced Labels is seven of these lessons on data built for each. CJK text on a line running up the screen writes vertically on its own.
Icons and patterns¶
Register an image on the style and name it from the document:
style.Images.Add("dot", PointStyle.CreateSimpleCircleStyle(GeoColor.FromHtml("#5E35B1"), 10, GeoColors.White, 2));
style.Images.Add("hatch", new GeoHatchBrush(GeoHatchStyle.DiagonalCross, GeoColor.FromHtml("#5E9E62"), GeoColor.FromArgb(110, 180, 224, 182)));
{ "type": "symbol", "source-layer": "Hotels", "layout": { "icon-image": "dot", "text-field": ["get", "NAME"], "text-optional": true, "text-anchor": "top", "text-offset": [0, 0.7] } },
{ "type": "fill", "source-layer": "Parks", "paint": { "fill-pattern": "hatch" } }
A GeoImage, any classic PointStyle drawn once, or any GeoHatchBrush can be an image. The HowDoI sample Hatch Styles lets you pick every GeoHatchStyle pattern.
Large or remote data¶
- Cap the cut.
FeatureSourceVectorTileSource.MaxDataZoomis the deepest zoom at which tiles are cut; beyond it the covering tile is reused. Whole-country polygons from a database over a network are worth capping at 8 or so, instead of re-querying and re-simplifying the same continents at every zoom. - Cache the cut.
VectorTileCache = new FileTileCache(folder)keeps the cut tiles on disk between runs. - Open off the UI thread. A GeoPDF or a DWG is tens of megabytes of parsing;
Open(),GetBoundingBox()andClose()inside aTask.Runkeep the window responsive. - Keep the source. When a document is edited or swapped, pass the same
FeatureSourceVectorTileSourceagain; the map recognizes it and keeps its tiles. See Changing the Map While It Runs.
Export to PDF¶
The same document over the same sources draws into a PDF as vector paths and text:
var sources = new GeoCollection<FeatureSource>();
sources.Add("Parks", new ShapeFileFeatureSource(@"Data\Parks.shp") { ProjectionConverter = new ProjectionConverter(2276, 3857) });
var layer = new MapStyleFeatureAsyncLayer(new MapStyle(parksJson), sources);
await layer.OpenAsync();
var pdf = new PdfGeoCanvas();
pdf.SetPageSize(PrinterPageSize.AnsiA, PrinterOrientation.Landscape);
using (var stream = File.Create("Parks.pdf"))
{
pdf.BeginDrawing(stream, Map.CurrentExtent, Map.MapUnit);
await layer.DrawAsync(pdf, new Collection<SimpleCandidate>());
pdf.EndDrawing();
}
await layer.CloseAsync();
Give the layer sources of its own: the map is reading the ones it was given. MapStyleFeatureAsyncLayer is a classic layer, so it also goes into a LayerOverlay or a PrinterLayer wherever a classic layer can be drawn.
Coming from classic layers¶
An application written for ThinkGeo 14 has FeatureLayers styled on their ZoomLevelSets. They can be brought into a style as they are:
var style = new MapStyle(ThinkGeoVectorStyles.Light, cloud);
foreach (var layer in myClassicLayers)
{
var translation = FeatureLayerTranslator.Translate(layer, Map.ZoomScales);
style.AddFeatureLayer(translation);
warnings.AddRange(translation.Warnings);
}
Map.Basemap = new GpuBasemap(style);
AreaStyle (hatches included), LineStyle, PointStyle, TextStyle, ValueStyle, ClassBreakStyle, RegexStyle and CompositeStyle translate. FilterStyle, ClusterPointStyle and a Style subclass of your own do not; they are named in Warnings, and those layers stay on a classic LayerOverlay above the basemap. The HowDoI sample Migrate a Classic Layer draws the same layers both ways, side by side. Classic Rendering is the chapter on that path.