mapnik

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2019 License: MIT Imports: 11 Imported by: 1

README

go-mapnik

Description

Small wrapper for the Mapnik 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-dev on Ubuntu/Debian, mapnik in Homebrew). Make sure mapnik-config is in your PATH.

You need to set the CGO_LDFLAGS and CGO_CXXFLAGS environment variables for successful compilation and linking with Mapnik. Refer to the Makefile how mapnik-config can be used to extract the required CGO_LDFLAGS and CGO_CXXFLAGS values. Use -ldflags to overwrite the default location of the input plugins and default fonts.

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.

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.png"); err != nil {
	log.Fatal(err)
}
Output:

Index

Examples

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.

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).

Example (Function)
m := mapnik.New()
if err := m.Load("test/map.xml"); err != nil {
	log.Fatal(err)
}

selector := func(layername string) mapnik.Status {
	if layername == "labels" {
		return mapnik.Exclude
	}
	return mapnik.Default
}
m.SelectLayers(mapnik.SelectorFunc(selector))
opts := mapnik.RenderOpts{Format: "png32"}
if err := m.RenderToFile(opts, "/tmp/go-mapnik-example.png"); err != nil {
	log.Fatal(err)
}
m.ResetLayers()
Output:

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 proj4 string ('+init=epsg:4326', etc).

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