mapnik

Mapnik is the core of cartographic design and processing.

property type description
version string

current version of mapnik

module_path string

path to native mapnik binding

supports Object

indicates which of the following are supported: grid, svg, cairo, cairo_pdf, cairo_svg, png, jpeg, tiff, webp, proj4, threadsafe

versions Object

diagnostic object with versions of node, v8, boost, boost_number, mapnik, mapnik_number, mapnik_git_describe, cairo

Examples

var mapnik = require('node-mapnik');

compositeOp

Image type constants representing color and grayscale encodings. Composite operation constants

property type description
clear number
src number
dst number
src_over number
dst_over number
src_in number
dst_in number
src_out number
dst_out number
src_atop number
dst_atop number
xor number
plus number
minus number
multiply number
screen number
overlay number
darken number
lighten number
color_dodge number
color_burn number
hard_light number
soft_light number
difference number
exclusion number
contrast number
invert number
invert number

-rgb

grain_merge number
grain_extract number
hue number
saturation number
color number
linear_dodge number
linear_burn number
divide number

imageType

Image type constants representing color and grayscale encodings.

property type description
rgba8 number
gray8 number
gray8s number
gray16 number
gray16s number
gray32 number
gray32s number
gray32f number
gray63 number
gray63s number
gray63f number

mapnik.Color(value, green, blue, blue, premultiplied)

parameter type description
value string or number

either an array of [r, g, b, a], a color keyword, or a CSS color in rgba() form.

green number
blue number
blue number
premultiplied boolean

Throws

exception description
TypeError

if a rgb component is outside of the 0-255 range

Examples

var c = new mapnik.Color('green');
var c = new mapnik.Color(0, 128, 0, 255);
// premultiplied
var c = new mapnik.Color(0, 128, 0, 255, true);

get_premultiplied

Get whether this color is premultiplied

Returns

boolean :

premultiplied

hex

Get this color represented as a hexademical string

Returns

string :

hex representation

Examples

var c = new mapnik.Color('green');
c.hex();
// '#008000'

set_premultiplied(premultiplied)

Set whether this color should be premultiplied

parameter type description
premultiplied boolean

Throws

exception description
TypeError

given a non-boolean argument

Examples

var c = new mapnik.Color('green');
c.set_premultiplied(true);

toString

Get this color's representation as a string

Returns

string :

color as a string

Examples

var green = new mapnik.Color('green');
green.toString()
// 'rgb(0,128,0)'

mapnik.Datasource

A Datasource object. This is the connector from Mapnik to any kind of file, network, or database source of geographical data.

describe

Describe the datasource's contents and type.

Returns

Object :

description: an object with type, fields, encoding, geometry_type, and proj4 code

extent

Get the Datasource's extent

Returns

Array<number> :

extent [minx, miny, maxx, maxy] order feature extent.

mapnik.Feature

A single geographic feature, with geometry and properties. This is typically derived from data by a datasource, but can be manually created.

fromJSON(geojson)

parameter type description
geojson string

string

Create a feature from a GeoJSON representation.

attributes

Get the feature's attributes as an object.

Returns

Object :

attributes

extent

Get the feature's extent

Returns

Array<number> :

extent [minx, miny, maxx, maxy] order feature extent.

geometry

Get the feature's attributes as a Mapnik geometry.

Returns

mapnik.Geometry :

geometry

id

Returns

number :

id the feature's internal id

toJSON

Generate and return a GeoJSON representation of this feature

Returns

string :

geojson Feature object in stringified GeoJSON

mapnik.Featureset

An iterator of mapnik.Feature objects.

next

Return the next Feature in this featureset if it exists, or null if it does not.

Returns

mapnik.Feature or :

next feature

mapnik.Geometry

Geometry: a representation of geographical features in terms of shape alone. This class provides many useful functions for conversion to and from formats.

You'll never create a mapnik.Geometry instance manually: it is always part of a mapnik.Feature instance, which is often a part of a mapnik.Featureset instance.

extent

Get the geometry's extent

Returns

Array<number> :

extent [minx, miny, maxx, maxy] order geometry extent.

toJSON([options], callback)

Convert this geometry into a GeoJSON representation, asynchronously.

parameter type description
options [Object] (default {})

. The only supported object is transform, which should be a valid mapnik.ProjTransform object.

callback Function

called with (err, result)

toJSONSync

Convert this geometry into a GeoJSON representation, synchronously.

Returns

string :

GeoJSON, string-encoded representation of this geometry.

toWKB

Get the geometry's representation as Well-Known Binary

Returns

string :

wkb representation of this geometry

toWKT

Get the geometry's representation as Well-Known Text

Returns

string :

wkt representation of this geometry

mapnik.Grid(width, height, [options])

Generator for UTFGrid representations of data.

parameter type description
width number
height number
options [Object] (default {})

optional argument, which can have a 'key' property

property type description
key string

addField

Get all of this grid's fields

Returns

Array<string> :

fields

addField(field)

Add a field to this grid's output

parameter type description
field string

encodeSync([options])

Get a constrained view of this field given x, y, width, height parameters.

parameter type description
options [Object] (default { resolution: 4, features: false })

Returns

Object :

an encoded field with grid, keys, and data members.

height

Get this grid's height

Returns

number :

height

view(x, y, width, height)

Get a constrained view of this field given x, y, width, height parameters.

parameter type description
x number
y number
width number
height number

Returns

mapnik.Grid :

a grid constrained to this new view

width

Get this grid's width

Returns

number :

width

mapnik.Image(width, height, options)

parameter type description
width number
height number
options Object

valid options are premultiplied, painted, type and initialize.

Throws

exception description
TypeError

if any argument is the wrong type, like if width or height is not numeric.

Examples

var im = new mapnik.Image(256, 256, {
  premultiplied: true,
  type: mapnik.imageType.gray8
});

fromBytes(buffer, callback)

Create a new image from a buffer

parameter type description
buffer Buffer
callback Function

clear(callback)

Make this image transparent, removing all image data from it.

parameter type description
callback Function

clearSync

Make this image transparent.

Examples

var im = new mapnik.Image(5,5);
im.fillSync(1);
assert.equal(im.getPixel(0, 0), 1);
im.clearSync();
assert.equal(im.getPixel(0, 0), 0);

compare(other, [options])

Compare two images visually. This is useful for algorithms and tests that confirm whether a certain image has changed significantly.

parameter type description
other mapnik.Image

another image instance

options [Object] (default {threshold:16,alpha:true})

Returns

number :

quantified visual difference between these two images

composite(other, callback)

Overlay this image with another image, creating a layered composite as a new image

parameter type description
other mapnik.Image
callback Function

copy([options], callback)

Copy this image data so that changes can be made to a clone of it.

parameter type description
options [Object] (default {})
callback Function

copySync([options])

Copy this image data so that changes can be made to a clone of it.

parameter type description
options [Object] (default {})

Returns

mapnik.Image :

copy

demultiply(callback)

Demultiply the pixels in this image, asynchronously. The opposite of premultiplying

parameter type description
callback Function

demultiplySync

Demultiply the pixels in this image. The opposite of premultiplying

encode([format], callback)

Encode this image into a buffer of encoded data

parameter type description
format [string] (default png)

image format

callback Function

Returns

Buffer :

encoded image data

Examples

var fs = require('fs');
myImage.encode('png', function(err, encoded) {
  fs.writeFileSync('myimage.png', encoded);
});

encodeSync([format])

Encode this image into a buffer of encoded data

parameter type description
format [string] (default png)

image format

Returns

Buffer :

encoded image data

Examples

var fs = require('fs');
fs.writeFileSync('myimage.png', myImage.encodeSync('png'));

fill(color, callback)

Asynchronously fill this image with a given color.

parameter type description
color mapnik.Color or number
callback Function

Examples

var im = new mapnik.Image(5, 5);
im.fill(1, function(err, im_res) {
  if (err) throw err;
  assert.equal(im_res.getPixel(0, 0), 1);
});

fillSync(color)

Fill this image with a given color

parameter type description
color mapnik.Color or number

getPixel(x, y, options)

parameter type description
x number

position within image from top left

y number

position within image from top left

options Object

the only valid option is get_color, which should be a boolean.

Returns

number :

color

Examples

var im = new mapnik.Image(256, 256);
var view = im.view(0, 0, 256, 256);
var pixel = view.getPixel(0, 0, {get_color:true});
assert.equal(pixel.r, 0);
assert.equal(pixel.g, 0);
assert.equal(pixel.b, 0);
assert.equal(pixel.a, 0);

height

Get this image's height

Returns

number :

height

isSolidSync

Determine whether the image is solid - whether it has alpha values of greater than one.

Returns

boolean :

whether the image is solid

Examples

var im = new mapnik.Image(256, 256);
var view = im.view(0, 0, 256, 256);
assert.equal(view.isSolidSync(), true);

premultiplied

Determine whether the given image is premultiplied.

Returns

boolean :

premultiplied true if the image is premultiplied

premultiply(callback)

Premultiply the pixels in this image, asynchronously

parameter type description
callback Function

premultiplySync

Premultiply the pixels in this image

save(filename, [format])

Encode this image and save it to disk as a file.

parameter type description
filename string
format [string] (default png)

Examples

myImage.save('foo.png');

setPixel(x, y, numeric)

parameter type description
x number

position within image from top left

y number

position within image from top left

numeric Object or number

or object representation of a color

view(x, y, width, height)

Get a constrained view of this image given x, y, width, height parameters.

parameter type description
x number
y number
width number
height number

Returns

mapnik.Image :

an image constrained to this new view

width

Get this image's width

Returns

number :

width

mapnik.ImageView(left, top, width, height)

This is usually not initialized directly: you'll use the mapnik.Image#view method to instantiate an instance.

parameter type description
left number
top number
width number
height number

Throws

exception description
TypeError

if any argument is missing or not numeric

Examples

var im = new mapnik.Image(256, 256);
var view = im.view(0, 0, 256, 256);

mapnik.Map(width, width, projection)

A map in mapnik is an object that combined data sources and styles in a way that lets you produce styled cartographic output.

parameter type description
width number
width number
projection string

as a proj4 code

Examples

var map = new mapnik.Map(25, 25, '+init=epsg:3857');

add_layer(new)

Add a new layer to this map

parameter type description
new mapnik.Layer

layer

clear

Remove all layers and styles from this map

clone

Clone this map object, returning a value which can be changed without mutating the original

Returns

mapnik.Map :

clone

font

Get all of the fonts currently registered as part of this map

Returns

Array<string> :

fonts

fontDirectory

Get the currently-registered font directory, if any

Returns

string or :

fonts

fontFiles

Get all of the fonts currently registered as part of this map, as a mapping from font to font file

Returns

Object :

fonts

fromStringSync(stylesheet, [options])

Load styles, layers, and other information for this map from a Mapnik XML stylesheet given as a string.

parameter type description
stylesheet string

contents

options [Object] (default {})

Examples

var fs = require('fs');
map.fromStringSync(fs.readFileSync('./style.xml', 'utf8'));

fromStringSync(stylesheet, [options], callback)

Load styles, layers, and other information for this map from a Mapnik XML stylesheet given as a string.

parameter type description
stylesheet string

contents

options [Object] (default {})
callback Function

Examples

var fs = require('fs');
map.fromStringSync(fs.readFileSync('./style.xml', 'utf8'), function(err, res) {
  // details loaded
});

get_layer(layer)

Get a layer out of this map, given a name or index

parameter type description
layer string or number

name or index

Returns

mapnik.Layer :

the layer

Throws

exception description
Error

if index is incorrect or layer is not found

layers

Get all of the currently-added layers in this map

Returns

Array<mapnik.Layer> :

layers

load(stylesheet, [options], callback)

Load styles, layers, and other information for this map from a Mapnik XML stylesheet.

parameter type description
stylesheet string

path

options [Object] (default {})
callback Function

loadSync(stylesheet, [options])

Load styles, layers, and other information for this map from a Mapnik XML stylesheet.

parameter type description
stylesheet string

path

options [Object] (default {})

Examples

map.loadSync('./style.xml');

resize(width, height)

Give this map new dimensions

parameter type description
width number
height number

scale

Get the map's scale factor. This is the ratio between pixels and geographical units like meters.

Returns

number :

scale

scaleDenominator

Get the map's scale denominator.

Returns

number :

scale denominator

mapnik.Projection(projection, [options])

A geographical projection: this class makes it possible to translate between locations in different projections

parameter type description
projection string

projection as a proj4 definition string

options [Object] (default {lazy:false})

whether to lazily instantiate the data backing this projection.

Throws

exception description
TypeError

if the projection string or options argument is the wrong type

Error

the projection could not be initialized - it was not found in proj4's tables or the string was malformed

Examples

var wgs84 = new mapnik.Projection('+init=epsg:4326');

forward(position)

Project from a position in WGS84 space to a position in this projection.

parameter type description
position Array<number>

as [x, y] or extent as [minx,miny,maxx,maxy]

Returns

Array<number> :

projected coordinates

Examples

var merc = new mapnik.Projection('+init=epsg:3857');
var long_lat_coords = [-122.33517, 47.63752];
var projected = merc.forward(long_lat_coords);

inverse(position)

Unproject from a position in this projection to the same position in WGS84 space.

parameter type description
position Array<number>

as [x, y] or extent as [minx,miny,maxx,maxy]

Returns

Array<number> :

unprojected coordinates

mapnik.VectorTile(z, x, y)

A generator for the Mapbox Vector Tile specification of compressed and simplified tiled vector data

parameter type description
z number
x number
y number

Examples

var vtile = new mapnik.VectorTile(9,112,195);

addData(raw, name)

Add raw data to this tile as a Buffer

parameter type description
raw Buffer

data

name string

of the layer to be added

addGeoJSON(geojson, name)

Add features to this tile from a GeoJSON string

parameter type description
geojson string

as a string

name string

of the layer to be added

clear(callback)

Remove all data from this vector tile

parameter type description
callback Function

compositeSync(vector)

Composite an array of vector tiles into one vector tile

parameter type description
vector Array<mapnik.VectorTile>

tiles

empty(whether)

Return whether this vector tile is empty - whether it has no layers and no features

parameter type description
whether boolean

the layer is empty

getData(map, renderable, callback)

Render this vector tile to a surface, like a mapnik.Image

parameter type description
map mapnik.Map

object

renderable mapnik.Image

surface

callback Function

getData

Get the data in this vector tile as a buffer

Returns

Buffer :

raw data

height(height)

Get the vector tile's height

parameter type description
height number

isSolid(callback)

Test whether this tile is solid - whether it has features

parameter type description
callback Function

isSolidSync

Test whether this tile is solid - whether it has features

Returns

boolean :

whether the tile is solid

names(layer)

Get the names of all of the layers in this vector tile

parameter type description
layer Array<string>

names

painted(painted)

Get whether the vector tile has been painted

parameter type description
painted boolean

query(longitude, latitude, [options], callback)

Query a vector tile by longitude and latitude

parameter type description
longitude number
latitude number
options [Object] (default {tolerance:0})

tolerance: allow results that are not exactly on this longitude, latitude position.

callback Function

setData(raw, name)

Replace the data in this vector tile with new raw data

parameter type description
raw Buffer

data

name string

of the layer to be added

toGeoJSON(callback)

Get a GeoJSON representation of this tile

parameter type description
callback Function

Returns

string :

stringified GeoJSON of all the features in this tile.

toGeoJSONSync

Get a GeoJSON representation of this tile

Returns

string :

stringified GeoJSON of all the features in this tile.

toJSON

Get a JSON representation of this tile

Returns

Object :

json representation of this tile with name, extent, and version properties

width(width)

Get the vector tile's width

parameter type description
width number

mapnik.blend(buffers, options, callback)

Composite multiple images on top of each other, with strong control over how the images are combined, resampled, and blended.

parameter type description
buffers Array<Buffer>

an array of buffers

options Object

can include width, height, compression, reencode, palette, mode can be either hextree or octree, quality. JPEG & WebP quality quality ranges from 0-100, PNG quality from 2-256. Compression varies by platform - it references the internal zlib compression algorithm.

callback Function

called with (err, res), where a successful result is a processed image as a Buffer

Examples

mapnik.blend([
 fs.readFileSync('foo.png'),
 fs.readFileSync('bar.png'),
], function(err, result) {
 if (err) throw err;
 fs.writeFileSync('result.png', result);
});