VectorSpriteSheet

Added in version 1.1.

class VectorSpriteSheet(**properties: Any)

Superclasses: Object

A collection of VectorSprite’s.

Sprites are used as icons in symbols or as the pattern for a fill layer.

Most MapLibre stylesheets provide their spritesheet as a PNG image and a JSON description of the sprites. This spritesheet can be added using add_page. Sprites can also be added individually using add_sprite.

Some map styles rely on application code to provide some or all of their sprites. This is supported using a fallback function, which can be set using set_fallback. This function can generate sprites on demand. For example, it could load a symbolic icon from the IconTheme or render a custom highway shield.

HiDPI support

Map styles should provide a double resolution spritesheet for high DPI displays. That spritesheet can be added as a separate page. The VectorSpriteSheet will pick the best sprites for the display’s scale factor.

If a fallback function is set, it receives the requested scale factor as an argument. It should use this to generate the sprite at the correct size. For example, if the scale factor is 2, the image should be twice as large (but the sprite’s width and height should be the same).

Thread Safety

VectorSpriteSheet is thread safe.

Constructors

class VectorSpriteSheet
classmethod new() VectorSpriteSheet

Creates a new, empty VectorSpriteSheet.

Added in version 1.1.

Methods

class VectorSpriteSheet
add_page(texture: Texture, json: str, default_scale: float) bool

Adds a page to the spritesheet.

See <https://maplibre.org/maplibre-gl-js-docs/style-spec/sprite/> for details about the spritesheet format. Most stylesheets provide these files along with the main style JSON.

Map styles should provide a double resolution spritesheet for high DPI displays. That spritesheet should be added as its own page, with a default_scale of 2.

Added in version 1.1.

Parameters:
  • texture – a Texture

  • json – a JSON string

  • default_scale – the default scale factor of the page

add_sprite(name: str, sprite: VectorSprite) None

Adds a sprite to the spritesheet.

Added in version 1.1.

Parameters:
get_sprite(name: str, scale: float) VectorSprite | None

Gets a sprite from the spritesheet.

The returned sprite might not be at the requested scale factor if an exact match is not found.

Added in version 1.1.

Parameters:
  • name – an icon name

  • scale – the scale factor of the icon

set_fallback(fallback: Callable[[...], VectorSprite | None] | None = None, *user_data: Any) None

Sets a fallback function to generate sprites.

The fallback function is called when a texture is not found in the sprite sheet. It receives the icon name and scale factor, and should return a VectorSprite, or None if the icon could not be generated. It may be called in a different thread, and it may be called multiple times for the same icon name.

If a previous fallback function was set, it will be replaced and any sprites it generated will be cleared.

fallback may be None to clear the fallback function.

Added in version 1.1.

Parameters:
  • fallback – a ShumateVectorSpriteFallbackFunc or None

  • user_data – user data to pass to fallback