Documentation
¶
Index ¶
- Constants
- func DefaultCacheDir() (string, error)
- func FetchTileURLTemplate(sourceURL string) (string, error)
- func FitOverlaysBounds(overlays []Overlay, width, height float64) (lat, lng float64, zoom int, err error)
- func Render(ctx context.Context, req RenderRequest) (*image.RGBA, error)
- func RenderCanvas(ctx context.Context, req RenderRequest) (*canvas.Canvas, error)
- type FontManager
- type LayoutProps
- type MapStyle
- type Overlay
- type PaintProps
- type PanFrame
- type RenderRequest
- type Sprite
- type StyleLayer
- type TileCache
- type TileJSON
Constants ¶
const TileSize = 512
Variables ¶
This section is empty.
Functions ¶
func DefaultCacheDir ¶
DefaultCacheDir returns the default tile cache directory (~/.cache/maprender) based on the current user's home directory.
func FetchTileURLTemplate ¶
func FitOverlaysBounds ¶
func FitOverlaysBounds(overlays []Overlay, width, height float64) (lat, lng float64, zoom int, err error)
FitOverlaysBounds computes the center (lat, lng) and integer zoom so that the combined bounds of the overlays fit within a viewport of width x height logical pixels. MultiPolygons and collections are handled via their combined envelope.
func RenderCanvas ¶
RenderCanvas renders the map to a vector canvas that can be rasterized or exported to other formats (SVG, PDF, EPS, ...) via canvas.Write / WriteFile.
Types ¶
type FontManager ¶
type FontManager struct {
// contains filtered or unexported fields
}
FontManager resolves Mapbox GL Style font stacks ("Noto Sans Regular", "Noto Sans Bold", ...) to canvas font faces backed by system fonts.
func DefaultFonts ¶
func DefaultFonts() *FontManager
DefaultFonts returns a lazily-initialized, shared FontManager that loads the most common sans-serif fonts from the system.
func NewFontManager ¶
func NewFontManager(families ...string) *FontManager
NewFontManager loads the given (or default) font families from the system. Any family that cannot be found is skipped.
func (*FontManager) Face ¶
func (fm *FontManager) Face(fontStacks []string, sizePx float64, col color.Color, haloColor color.Color, haloWidthPx float64) *canvas.FontFace
Face returns a canvas font face for the given font stacks (in priority order). sizePx is the font size in logical pixels. When haloWidthPx is greater than zero, the face is decorated with a text halo (stroke) of the given color. It returns nil if no usable font family was loaded.
type LayoutProps ¶
type LayoutProps struct {
TextField any `json:"text-field"`
TextFont []string `json:"text-font"`
TextSize any `json:"text-size"`
TextAnchor any `json:"text-anchor"`
TextTransform any `json:"text-transform"`
IconImage any `json:"icon-image"`
IconSize any `json:"icon-size"`
IconAnchor any `json:"icon-anchor"`
}
type MapStyle ¶
type MapStyle struct {
Layers []StyleLayer `json:"layers"`
SourceURL string
SpriteURL string
GlyphsURL string
}
func FetchStyle ¶
func (*MapStyle) ResolveTileJSON ¶
func (*MapStyle) ResolveTileURL ¶
type Overlay ¶
type Overlay struct {
// Geometry is the geometry to draw. Coordinates are interpreted as
// (longitude, latitude).
Geometry geom.Geometry
// Properties are optional free-form properties (e.g. from a GeoJSON
// feature). They are used to derive stroke/fill colors when the explicit
// colors below are nil.
Properties map[string]any
// StrokeColor is the outline color. When nil, it is derived from
// Properties (keys "stroke", "stroke-color", "strokeColor") and finally
// defaults to red.
StrokeColor color.Color
// FillColor is the polygon fill color. When nil, it is derived from
// Properties (keys "fill", "fill-color", "fillColor") and finally defaults
// to transparent (no fill).
FillColor color.Color
// StrokeWidth is the outline width in pixels. Zero means the default (2).
StrokeWidth float64
}
Overlay is a geometry (in WGS84 / lon-lat coordinates) drawn on top of the rendered map.
func OverlayFromGeoJSON ¶
OverlayFromGeoJSON parses GeoJSON (a Geometry, Feature, or FeatureCollection) into overlays. Feature properties are retained for color extraction.
func OverlayFromWKB ¶
OverlayFromWKB parses a WKB byte slice into an Overlay.
func OverlayFromWKT ¶
OverlayFromWKT parses a WKT string into an Overlay.
type PaintProps ¶
type PaintProps struct {
BackgroundColor any `json:"background-color"`
FillColor any `json:"fill-color"`
FillOpacity any `json:"fill-opacity"`
LineColor any `json:"line-color"`
LineWidth any `json:"line-width"`
LineOpacity any `json:"line-opacity"`
LineDashArray any `json:"line-dasharray"`
TextColor any `json:"text-color"`
TextHaloColor any `json:"text-halo-color"`
TextHaloWidth any `json:"text-halo-width"`
TextOpacity any `json:"text-opacity"`
}
type PanFrame ¶
type PanFrame struct {
Image *image.RGBA
Base *image.RGBA
CenterLat float64
CenterLng float64
Zoom int
}
PanFrame is the result of an incremental pan. Image is the complete frame (text labels, icons and marker included) ready for display; Base is the same frame without labels — pass it back as Prev on the next RenderIncremental call so labels are never stacked onto already drawn ones.
func RenderIncremental ¶
RenderIncremental renders req while reusing pixels from prev (the PanFrame returned by an earlier call at the same zoom, size and style): the previous label-free base is shifted according to the pan delta, only the newly exposed strips are re-rendered, and text labels/icons/marker are drawn fresh onto a copy of the result. This makes panning dramatically cheaper than a full render while keeping every frame identical to one.
Everything else in req must be unchanged since prev was produced; when that does not hold (zoom change, resize, new overlays, ...) use Render instead. A nil prev (or a mismatching one) performs a full redraw through the same pipeline, so callers do not need special cases.
type RenderRequest ¶
type RenderRequest struct {
CenterLat float64
CenterLng float64
Zoom int
Width int // physical pixels
Height int // physical pixels
DevicePixelRatio float64
Style *MapStyle
TileURLTemplate string
SourceMinZoom int // optional; when TileURLTemplate is set, use this source min zoom for underzoom
SourceMaxZoom int // optional; when TileURLTemplate is set, use this source max zoom for overzoom
Fonts *FontManager
Sprite *Sprite
Overlays []Overlay
FitOverlays bool // when true, center and zoom are computed to fit Overlays
MarkerLat *float64
MarkerLng *float64
TileCacheDir string // directory for downloaded tiles; empty defaults to ~/.cache/maprender
TileCacheTTL time.Duration // tile cache expiry; 0 defaults to 2 weeks, negative disables expiry
Logger *slog.Logger
// contains filtered or unexported fields
}
type Sprite ¶
type Sprite struct {
// contains filtered or unexported fields
}
Sprite is a Mapbox sprite: a set of named icons cropped from a sprite sheet.
func FetchSprite ¶
FetchSprite loads a Mapbox sprite from the given base URL (the style's `sprite` field). It fetches `<url>.json` and `<url>.png`.
type StyleLayer ¶
type StyleLayer struct {
ID string `json:"id"`
Type string `json:"type"`
SourceLayer string `json:"source-layer"`
Paint PaintProps `json:"paint"`
Layout LayoutProps `json:"layout"`
Filter []any `json:"filter"`
MinZoom *float64 `json:"minzoom"`
MaxZoom *float64 `json:"maxzoom"`
}
func GetLayerByID ¶
func GetLayerByID(style *MapStyle, id string) *StyleLayer
type TileCache ¶
type TileCache struct {
// contains filtered or unexported fields
}
TileCache stores downloaded tiles on disk so they can be reused across renders and processes. It is safe for concurrent use by multiple processes sharing the same directory: tiles are written to a temporary file and then atomically moved into place, so a reader never observes a partial tile.
func NewTileCache ¶
NewTileCache returns a TileCache rooted at dir. When dir is empty the default cache directory (~/.cache/maprender) is used. Entries older than ttl are treated as missing and re-downloaded; a non-positive ttl disables expiry.
func (*TileCache) Fetch ¶
Fetch returns the tile data for url, using the cache when possible and downloading (and caching) it otherwise.
func (*TileCache) Get ¶
Get returns the cached tile data for url, or ok=false if it is not cached or has expired.
