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 |
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
|
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
|
exception | description |
---|---|
TypeError |
if a rgb component is outside of the 0-255 range |
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
boolean
:
premultiplied
hex
Get this color represented as a hexademical string
string
:
hex representation
var c = new mapnik.Color('green');
c.hex();
// '#008000'
set_premultiplied(premultiplied)
Set whether this color should be premultiplied
parameter | type | description |
---|---|---|
premultiplied |
boolean
|
exception | description |
---|---|
TypeError |
given a non-boolean argument |
var c = new mapnik.Color('green');
c.set_premultiplied(true);
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.
Object
:
description: an object with type, fields, encoding, geometry_type, and proj4 code
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.
Object
:
attributes
extent
Get the feature's extent
Array<number
>
:
extent [minx, miny, maxx, maxy] order feature extent.
geometry
Get the feature's attributes as a Mapnik geometry.
mapnik.Geometry
:
geometry
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.
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
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 |
[
(default {} )
|
. The only supported object is |
callback |
Function
|
called with (err, result) |
toJSONSync
Convert this geometry into a GeoJSON representation, synchronously.
string
:
GeoJSON, string-encoded representation of this geometry.
toWKB
Get the geometry's representation as Well-Known Binary
string
:
wkb representation of this geometry
toWKT
Get the geometry's representation as Well-Known Text
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 |
[
(default {} )
|
optional argument, which can have a 'key' property |
property | type | description |
---|---|---|
key |
string
|
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 |
[
(default { resolution: 4, features: false } )
|
Object
:
an encoded field with grid
, keys
, and data
members.
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
|
mapnik.Grid
:
a grid constrained to this new view
mapnik.Image(width, height, options)
parameter | type | description |
---|---|---|
width |
number
|
|
height |
number
|
|
options |
Object
|
valid options are |
exception | description |
---|---|
TypeError |
if any argument is the wrong type, like if width or height is not numeric. |
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.
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 |
[
(default {threshold:16,alpha:true} )
|
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 |
[
(default {} )
|
|
callback |
Function
|
copySync([options])
Copy this image data so that changes can be made to a clone of it.
parameter | type | description |
---|---|---|
options |
[
(default {} )
|
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 |
[
(default png )
|
image format |
callback |
Function
|
Buffer
:
encoded image data
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 |
[
(default png )
|
image format |
Buffer
:
encoded image data
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
|
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 |
number
:
color
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);
isSolidSync
Determine whether the image is solid - whether it has alpha values of greater than one.
boolean
:
whether the image is solid
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.
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 |
[
(default png )
|
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
|
mapnik.Image
:
an image constrained to this new view
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
|
exception | description |
---|---|
TypeError |
if any argument is missing or not numeric |
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 |
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
mapnik.Map
:
clone
fontDirectory
Get the currently-registered font directory, if any
string
or
:
fonts
fontFiles
Get all of the fonts currently registered as part of this map, as a mapping from font to font file
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 |
[
(default {} )
|
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 |
[
(default {} )
|
|
callback |
Function
|
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 |
mapnik.Layer
:
the layer
exception | description |
---|---|
Error |
if index is incorrect or layer is not found |
layers
Get all of the currently-added layers in this map
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 |
[
(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 |
[
(default {} )
|
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.
number
:
scale
scaleDenominator
Get the map's scale denominator.
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 |
[
(default {lazy:false} )
|
whether to lazily instantiate the data backing this projection. |
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 |
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] |
Array<number
>
:
projected coordinates
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] |
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
|
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
|
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
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 |
[
(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
|
string
:
stringified GeoJSON of all the features in this tile.
toGeoJSONSync
Get a GeoJSON representation of this tile
string
:
stringified GeoJSON of all the features in this tile.
toJSON
Get a JSON representation of this tile
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, |
callback |
Function
|
called with (err, res), where a successful result is a processed image as a Buffer |
mapnik.blend([
fs.readFileSync('foo.png'),
fs.readFileSync('bar.png'),
], function(err, result) {
if (err) throw err;
fs.writeFileSync('result.png', result);
});