mapnik

package module
v3.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 10 Imported by: 1

README

go-mapnik

Description

Small wrapper for the Mapnik 3/4 API to render beautiful maps from Go.

Features:

  • Render to []byte, image.Image, or file.
  • Set scale denominator or scale factor.
  • Enable/disable single layers.

Installation

This package requires Mapnik (libmapnik on Ubuntu/Debian, mapnik in Homebrew).

Since v3 of this package you need to call go generate . to create a build_config.go file with all system dependent build flags. Before v3, go-mapnik required setting CGO_* environment variables. The new method is easier, but it does not work when you go get/import go-mapnik. It is recommended to manually vendorize the package into your repo (e.g via git subtree).

Example

func Example() {
	m := mapnik.New()
	if err := m.Load("test/map.xml"); err != nil {
		log.Fatal(err)
	}
	m.Resize(1000, 500)
	m.ZoomTo(-180, -90, 180, 90)
	opts := mapnik.RenderOpts{Format: "png32"}
	if err := m.RenderToFile(opts, "/tmp/go-mapnik-example-1.png"); err != nil {
		log.Fatal(err)
	}
}

Documentation

API documentation can be found here: http://godoc.org/github.com/omniscale/go-mapnik

License

MIT, see LICENSE file.

Author

Oliver Tonnhofer, Omniscale

Thanks

This package is inspired/based on mapnik-c-api by Dane Springmeyer and go-mapnik by Fabian Wickborn.

Documentation

Overview

Package mapnik renders beautiful maps with Mapnik.

Index

Constants

This section is empty.

Variables

View Source
var Version version

Functions

func Encode

func Encode(img image.Image, format string) ([]byte, error)

Encode image.Image with Mapniks image encoder. This is optimized for *image.NRGBA or *image.RGBA.

func LogSeverity

func LogSeverity(level LogLevel)

LogSeverity sets the global log level for Mapnik. Requires a Mapnik build with logging enabled.

func RegisterDatasources

func RegisterDatasources(path string) error

RegisterDatasources registers all input plugins found in the given path.

func RegisterFonts

func RegisterFonts(path string) error

RegisterDatasources registers all fonts found in the given path.

Types

type LayerSelector

type LayerSelector interface {
	Select(layername string) Status
}

type LogLevel

type LogLevel int

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map base type

func New

func New() *Map

New initializes a new Map.

func NewSized

func NewSized(width, height int) *Map

NewSized initializes a new Map with the given size.

func (*Map) BackgroundColor

func (m *Map) BackgroundColor() color.NRGBA

func (*Map) Free

func (m *Map) Free()

Free deallocates the map.

func (*Map) Load

func (m *Map) Load(stylesheet string) error

Load reads in a Mapnik map XML.

Note: Since Mapnik 3 all layers with status="off" are not loaded and cannot be activated by a custom LayerSelector. As a workaround, all layers with names starting with '__OFF__' are disabled on load and the '__OFF__' prefix is removed from the layer name.

func (*Map) Render

func (m *Map) Render(opts RenderOpts) ([]byte, error)

Render returns the map as an encoded image.

func (*Map) RenderImage

func (m *Map) RenderImage(opts RenderOpts) (*image.NRGBA, error)

RenderImage returns the map as an unencoded image.Image.

func (*Map) RenderToFile

func (m *Map) RenderToFile(opts RenderOpts, path string) error

RenderToFile writes the map as an encoded image to the file system.

func (*Map) ResetLayers

func (m *Map) ResetLayers()

ResetLayer resets all layers to the initial status.

func (*Map) ResetMaxExtent

func (m *Map) ResetMaxExtent()

func (*Map) Resize

func (m *Map) Resize(width, height int)

Resize changes the map size in pixel. Sizes larger than 16k pixels are ignored by Mapnik. Use NewSized to initialize larger maps.

func (*Map) SRS

func (m *Map) SRS() string

SRS returns the projection of the map.

func (*Map) ScaleDenominator

func (m *Map) ScaleDenominator() float64

ScaleDenominator returns the current scale denominator. Call after Resize and ZoomAll/ZoomTo.

func (*Map) SelectLayers

func (m *Map) SelectLayers(selector LayerSelector) bool

SelectLayers enables/disables single layers. LayerSelector or SelectorFunc gets called for each layer. Returns true if at least one layer was included (or set to default).

func (*Map) SetBackgroundColor

func (m *Map) SetBackgroundColor(c color.NRGBA)

func (*Map) SetBufferSize

func (m *Map) SetBufferSize(s int)

SetBufferSize sets the pixel buffer at the map image edges where Mapnik should not render any labels.

func (*Map) SetMaxExtent

func (m *Map) SetMaxExtent(minx, miny, maxx, maxy float64)

func (*Map) SetSRS

func (m *Map) SetSRS(srs string)

SetSRS sets the projection of the map as a Proj string ('epsg:4326', or '+init=epsg:4326' if you are using Mapnik with Proj4).

func (*Map) ZoomAll

func (m *Map) ZoomAll() error

ZoomAll zooms to the maximum extent.

func (*Map) ZoomTo

func (m *Map) ZoomTo(minx, miny, maxx, maxy float64)

ZoomTo zooms to the given bounding box.

type RenderOpts

type RenderOpts struct {
	// Scale renders the map at a fixed scale denominator.
	Scale float64
	// ScaleFactor renders the map with larger fonts sizes, line width, etc. For printing or retina/hq iamges.
	ScaleFactor float64
	// Format for the rendered image ('jpeg80', 'png256', etc. see: https://github.com/mapnik/mapnik/wiki/Image-IO)
	Format string
}

RenderOpts defines rendering options.

type SelectorFunc

type SelectorFunc func(string) Status

func (SelectorFunc) Select

func (f SelectorFunc) Select(layername string) Status

type Status

type Status int

Status defines if a layer should be rendered or not.

const (
	// Exclude layer from rendering.
	Exclude Status = -1
	// Default keeps layer at the current rendering status.
	Default Status = 0
	// Include layer for rendering.
	Include Status = 1
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL