Skip to main content

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: Bearer token 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:

Request
GET https://api.gc2.io/api/v4/legends/database/mydb/rockhall.inductees,rockhall.venues HTTP/1.1
Accept: application/json; charset=utf-8
Response
[
{
"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:

Request with a token
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:

  • classes holds 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.
  • img is a base64 encoded PNG of 18×12 pixels without a data: prefix. Prepend data:image/png;base64, to use it as an image source. It is null when 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 name set to null.
  • 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​

FieldDescription
idThe layer as a schema-qualified relation name.
titleThe layer's title, or its table name when it has none.
classesThe named classes of the layer in drawing order. Empty when the layer has none.
classes[].nameThe class name. null for the single picture of a layer whose legend comes from an external source.
classes[].expressionThe class expression, or null when the class has none.
classes[].imgThe icon as a base64 encoded PNG without data: prefix, or null when it could not be drawn.

Errors​

StatusCodeWhen
400INVALID_REQUESTThe layer list is malformed.
401TOKEN_DATABASE_MISMATCHThe token is for another database than the one in the path. Wrong Basic credentials are also 401.
404DATABASE_NOT_FOUNDThe database in the path does not exist. An unknown layer is not an error; it is left out.