Legends
The Legends API returns the legend of one or more layers as JSON: for each layer its title and its named classes, each with the class expression and a small icon drawn from the class's styles. Use it to build a legend panel next to a map, or to check what a class edit looks like without rendering a map.
The endpoint is one of the public routes and takes the same credentials as the OGC services:
- With an
Authorization: Bearertoken or HTTP Basic, the caller gets the layers they may read. - Without credentials, the caller gets the layers that are open to everyone.
- A layer the caller may not read, or that does not exist, is left out of the answer instead of failing the request.
Rules do not affect legends.
Get legends
The layers are given in the path as a comma-separated list of schema.table. A bare schema stands for every layer of that schema. The answer is always an array, also for one layer:
- Web App
- HTTP
GET https://api.gc2.io/api/v4/legends/database/mydb/rockhall.inductees,rockhall.venues HTTP/1.1
Accept: application/json; charset=utf-8
[
{
"id": "rockhall.inductees",
"title": "Inductees",
"classes": [
{
"name": "Performers",
"expression": "[category] = 'performer'",
"img": "iVBORw0KGgoAAAANSUhEUgAAABIAAAAMCAYAAABvEu28..."
},
{
"name": "Others",
"expression": null,
"img": "iVBORw0KGgoAAAANSUhEUgAAABIAAAAMCAYAAABvEu28..."
}
]
},
{
"id": "rockhall.venues",
"title": "Venues",
"classes": []
}
]
To read layers that need credentials, send a token or HTTP Basic with the same request:
GET https://api.gc2.io/api/v4/legends/database/mydb/rockhall HTTP/1.1
Accept: application/json; charset=utf-8
Authorization: Bearer abc123
A few things to know about the answer:
classesholds the layer's named classes in drawing order. A class without a name gives no legend entry, and a layer without named classes has an empty list.imgis a base64 encoded PNG of 18×12 pixels without adata:prefix. Prependdata:image/png;base64,to use it as an image source. It isnullwhen the icon could not be drawn.- A layer whose legend is one picture from elsewhere — a layer drawn from a QGIS style, an external WMS source or a legend URL — has one entry with
nameset tonull. - An empty array means that none of the layers asked for is readable by the caller. It does not mean that they do not exist.
Using the SDK
The @mapcentia/gc2-sdk exposes the endpoint through the Legends class. A client without auth reads the legends that are open to everyone; legendImageUrl turns an img into a data: URL:
import { createCentiaClient, Legends, legendImageUrl } from '@mapcentia/gc2-sdk'
const legends = new Legends(createCentiaClient({ baseUrl }))
// One or more layers, or a bare schema for all of its layers
const result = await legends.getLegend('mydb', ['rockhall.inductees', 'rockhall.venues'])
for (const layer of result) {
for (const cls of layer.classes) {
const src = legendImageUrl(cls.img) // null when there is no icon
console.log(layer.title, cls.name, src)
}
}
Fields
| Field | Description |
|---|---|
id | The layer as a schema-qualified relation name. |
title | The layer's title, or its table name when it has none. |
classes | The named classes of the layer in drawing order. Empty when the layer has none. |
classes[].name | The class name. null for the single picture of a layer whose legend comes from an external source. |
classes[].expression | The class expression, or null when the class has none. |
classes[].img | The icon as a base64 encoded PNG without data: prefix, or null when it could not be drawn. |
Errors
| Status | Code | When |
|---|---|---|
400 | INVALID_REQUEST | The layer list is malformed. |
401 | TOKEN_DATABASE_MISMATCH | The token is for another database than the one in the path. Wrong Basic credentials are also 401. |
404 | DATABASE_NOT_FOUND | The database in the path does not exist. An unknown layer is not an error; it is left out. |