Package dev.avelar.mapnik
Class MapnikMap
java.lang.Object
dev.avelar.mapnik.MapnikMap
- All Implemented Interfaces:
AutoCloseable
A Mapnik map. Not thread-safe: use one instance per thread.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionTurn every layer back on.addFontSet(FontSet fontSet) Add a font set for text symbolizers to use withfontset-name.Add a copy oflayeron top of the existing layers.Add a style built in code, checked by Mapnik's strict XML loader.AsaddStyle(Style).The background colour as Mapnik prints it, for examplergb(255,255,255)orrgba(0,0,255,0.5), if set.How the background image is blended onto the background colour, by name, such asmultiply.doublebasePath()extent()grown by the buffer size.intExtra pixels rendered around the map so labels and symbols at the edge are not cut off.voidclose()extent()The extent currently shown, in the map's projection.The directory set for fonts on this map, if any.booleanintheight()booleanisLayerActive(String name) Whether the named layer is drawn.layer(int index) A view onto the layer atindex.A view onto the first layer with this name.intLayer names in drawing order: the first is drawn first, so it ends up at the bottom.layers()Views onto all layers, in drawing order.Load a Mapnik XML style from a file.Asload(Path).loadString(String xml, Path basePath) Load a Mapnik XML style from a string.loadString(String xml, Path basePath, boolean strict) AsloadString(String, Path), optionally strict as inload(Path, boolean).The extent beyond which nothing is drawn, if set.pan(int x, int y) Move the view by a pixel offset.panAndZoom(int x, int y, double zoom) Centre the view on pixel (x, y), then scale the extent byzoomaszoom(double)does.Arbitrary key/value pairs stored on the map and written as<Parameters>in its XML, for your own metadata.queryMapPoint(String layer, double px, double py) Features of a layer under pixel (px, py) of the map image.queryPoint(String layer, double x, double y) Features of a layer at a point, given in the map's projection.registerFonts(Path dir, boolean recurse) Fonts directory for this map.Remove all layers and styles.removeLayer(int index) removeLayer(String name) removeParameter(String key) removeStyle(String name) Draw onto an existing image, blending over what is there.render(Image image, RenderOptions options) renderGrid(String layer) A UTFGrid of the layer with features named by their id, including no attributes, at 4 pixels per cell.renderGrid(String layer, String key, Collection<String> fields, int resolution) Render one layer as a UTFGrid: for each small cell of the picture, which feature is there.renderGrid(String layer, String key, Collection<String> fields, int resolution, RenderOptions options) renderLayers(Collection<String> names) Render only the named layers, then put every layer's active flag back as it was.renderTile(int z, int x, int y, int buffer) renderTile(Tiles.Tile tile, int buffer) Render one tile of the standard web map grid (seeTiles) as an image the size of the map, which must be square: its width is the tile size, usually 256 or 512.byte[]renderTileToBytes(Tiles.Tile tile, String format, int buffer) byte[]renderToBytes(ImageFormat format) AsrenderToBytes(String), with a format built byImageFormat, such asImageFormat.png8().colors(64).byte[]renderToBytes(ImageFormat format, RenderOptions options) byte[]renderToBytes(String format) Render to memory.byte[]renderToBytes(String format, RenderOptions options) voidrenderToFile(Path out, ImageFormat format) AsrenderToFile(Path, String), with a format built byImageFormat.voidrenderToFile(Path out, String format) Render to a file.voidrenderToFile(Path out, String format, RenderOptions options) Render to a newImagethe size of the map.renderToImage(RenderOptions options) byte[]replaceStyle(Style style) Add a style, replacing one of the same name if there is one.resize(int width, int height) voiddoublescale()Map units per pixel.doubleScale denominator, as used by layers' and rules' scale ranges.setActiveLayers(Collection<String> names) Draw only these layers; all others are turned off.HowzoomToBox(double, double, double, double)treats a box whose aspect ratio differs from the map's.setBackground(Color color) setBackground(String color) Set the background colour: a name ("white"),"#rrggbb", or"rgba(r,g,b,a)".setBackgroundImage(Path image) setBackgroundImageOpacity(double opacity) setBasePath(Path dir) Directory that relative paths in the style are resolved against.setBufferSize(int pixels) setFontDirectory(Path dir) setLayerActive(String name, boolean active) Turn one layer on or off.setMaximumExtent(Box2d box) setParameter(String key, Object value) Set a parameter: a String, Boolean, Integer, Long, Double or Float.Set the map projection: anything Mapnik accepts, such asepsg:3857or a PROJ string.srs()The map projection, as given in the style or bysetSrs(java.lang.String), e.g.One style by name, read back as instyles(), or empty if the map has none of that name.Names of the styles defined in the map, sorted.styles()The styles in the map as objects, read back from the map's XML: the ones you added in code, and the ones a loaded style file defined.toPixel(double x, double y) Where a point in map coordinates (the map's projection) lands in the rendered image: x to the right, y down from the top left.toWorld(double pixelX, double pixelY) The map coordinates under a pixel of the rendered image.toXml()toXml(boolean explicitDefaults) The map as Mapnik XML.Check a style without keeping it: load it into a throwaway map in strict mode and report what Mapnik objects to.intwidth()zoom(double factor) Scale the extent about its centre byfactor: above 1 zooms out (shows more), below 1 zooms in.zoomAll()Fit the extent of all layers' data.zoomToBox(double minx, double miny, double maxx, double maxy)
-
Constructor Details
-
MapnikMap
public MapnikMap(int width, int height)
-
-
Method Details
-
load
Load a Mapnik XML style from a file. Its layers are added to the map. -
load
Asload(Path). Withstrict, Mapnik reports problems it would otherwise skip, such as unknown attributes. -
loadString
Load a Mapnik XML style from a string. Relative paths in it resolve againstbasePath. -
loadString
AsloadString(String, Path), optionally strict as inload(Path, boolean). -
validate
Check a style without keeping it: load it into a throwaway map in strict mode and report what Mapnik objects to. The list is empty if the style is fine. A strict load stops at the first kind of failure, so fix what is reported and check again. -
toXml
The map as Mapnik XML. WithexplicitDefaults, settings at their default value are written too. -
toXml
-
save
-
width
public int width() -
height
public int height() -
resize
-
srs
The map projection, as given in the style or bysetSrs(java.lang.String), e.g.epsg:4326. -
setSrs
Set the map projection: anything Mapnik accepts, such asepsg:3857or a PROJ string. Layers keep their own projection and are reprojected when rendered. The extent passed tozoomToBox(double, double, double, double)is in this projection, so set it first. An invalid value fails at render time. -
background
The background colour as Mapnik prints it, for examplergb(255,255,255)orrgba(0,0,255,0.5), if set. -
setBackground
Set the background colour: a name ("white"),"#rrggbb", or"rgba(r,g,b,a)". -
setBackground
-
backgroundImage
-
setBackgroundImage
-
backgroundImageCompOp
How the background image is blended onto the background colour, by name, such asmultiply. Empty if unset. -
setBackgroundImageCompOp
-
setBackgroundImageCompOp
-
backgroundImageOpacity
public double backgroundImageOpacity() -
setBackgroundImageOpacity
-
bufferSize
public int bufferSize()Extra pixels rendered around the map so labels and symbols at the edge are not cut off. -
setBufferSize
-
registerFonts
Fonts directory for this map. -
loadFonts
-
fontDirectory
The directory set for fonts on this map, if any. -
setFontDirectory
-
parameters
-
setParameter
Set a parameter: a String, Boolean, Integer, Long, Double or Float. Replaces an existing one. -
removeParameter
-
toPixel
Where a point in map coordinates (the map's projection) lands in the rendered image: x to the right, y down from the top left. Needs an extent, for example fromzoomToBox(double, double, double, double). -
toPixel
-
toWorld
The map coordinates under a pixel of the rendered image. -
toWorld
-
basePath
-
setBasePath
Directory that relative paths in the style are resolved against. -
aspectFixMode
-
setAspectFixMode
HowzoomToBox(double, double, double, double)treats a box whose aspect ratio differs from the map's. -
extent
The extent currently shown, in the map's projection. -
bufferedExtent
extent()grown by the buffer size. -
maximumExtent
The extent beyond which nothing is drawn, if set. -
setMaximumExtent
-
resetMaximumExtent
-
zoomToBox
-
zoomToBox
-
zoomAll
Fit the extent of all layers' data. -
zoom
Scale the extent about its centre byfactor: above 1 zooms out (shows more), below 1 zooms in.zoom(0.5)shows half the width and height. -
pan
Move the view by a pixel offset. -
panAndZoom
Centre the view on pixel (x, y), then scale the extent byzoomaszoom(double)does. -
scale
public double scale()Map units per pixel. -
scaleDenominator
public double scaleDenominator()Scale denominator, as used by layers' and rules' scale ranges. -
layerCount
public int layerCount() -
layerNames
Layer names in drawing order: the first is drawn first, so it ends up at the bottom. -
layers
Views onto all layers, in drawing order. SeeLayerfor how long they stay valid. -
layer
A view onto the layer atindex. -
layer
A view onto the first layer with this name. ThrowsIllegalArgumentExceptionif there is none. -
addLayer
Add a copy oflayeron top of the existing layers. Invalidates layer views from this map. -
removeLayer
-
removeLayer
-
removeAll
Remove all layers and styles. -
isLayerActive
Whether the named layer is drawn. Layers are active by default. -
setLayerActive
Turn one layer on or off. ThrowsIllegalArgumentExceptionfor an unknown name. -
setActiveLayers
Draw only these layers; all others are turned off. Drawing order stays the style's order, not the order ofnames. ThrowsIllegalArgumentExceptionfor an unknown name, and changes nothing in that case. -
activateAllLayers
Turn every layer back on. -
addStyle
Add a style built in code, checked by Mapnik's strict XML loader. ThrowsIllegalArgumentExceptionif the map already has a style of that name (Mapnik would silently ignore the new one), andMapnikExceptionif Mapnik rejects the style. Strict loading catches what plain loading skips: a misspelled attribute, an attribute that does not belong on that kind of symbolizer, an unknown font face or a missing image file. The message names the problem. A rejected style is not kept. -
addStyle
AsaddStyle(Style). Withstrictfalse, Mapnik ignores attributes it does not know and keeps going if a font or file is missing, so the style loads but may not draw. -
replaceStyle
Add a style, replacing one of the same name if there is one. -
hasStyle
-
addFontSet
Add a font set for text symbolizers to use withfontset-name. The fonts must already be registered (seeMapnik.registerFonts(java.lang.String)), or Mapnik throwsMapnikException. -
queryPoint
Features of a layer at a point, given in the map's projection. The layer must be queryable (seeLayer.setQueryable(boolean)) for some plugins. Close the result. -
queryMapPoint
Features of a layer under pixel (px, py) of the map image. Close the result. -
styleNames
Names of the styles defined in the map, sorted. -
styles
The styles in the map as objects, read back from the map's XML: the ones you added in code, and the ones a loaded style file defined. Mapnik writes values in its own spelling, for example colours asrgb(255,0,0)and filters in parentheses, and leaves out attributes that are at their default. Adding a style read back withaddStyle(dev.avelar.mapnik.Style)to another map draws the same. -
style
One style by name, read back as instyles(), or empty if the map has none of that name. -
removeStyle
-
renderToFile
Render to a file.formatis an image format (png,jpeg,webp,tiff, with options such aspng8,png256,jpeg90) or a vector format (pdf,svg,ps, which need Cairo support in Mapnik). -
renderToFile
-
renderToFile
AsrenderToFile(Path, String), with a format built byImageFormat. -
renderToBytes
AsrenderToBytes(String), with a format built byImageFormat, such asImageFormat.png8().colors(64). -
renderToBytes
-
renderToBytes
Render to memory. Accepts the same formats asrenderToFile(java.nio.file.Path, java.lang.String). -
renderToBytes
-
renderToPng
public byte[] renderToPng() -
renderToImage
Render to a newImagethe size of the map. Close it when done. -
renderToImage
-
render
Draw onto an existing image, blending over what is there. The image must be the same size as the map. -
render
-
renderTile
Render one tile of the standard web map grid (seeTiles) as an image the size of the map, which must be square: its width is the tile size, usually 256 or 512. The map is put in Web Mercator for the render and then restored, including its projection, extent, size, aspect mode and buffer size, so you can keep using it. Close the result.bufferis a metatile margin in pixels: Mapnik draws a bigger picture that extends that far past the tile on every side and the margin is then cropped off, so that labels and wide lines that cross a tile edge match in the neighbouring tile. 0 draws the tile alone. -
renderTile
-
renderTileToBytes
-
renderLayers
Render only the named layers, then put every layer's active flag back as it was. Drawing order is the style's, not the order ofnames. ThrowsIllegalArgumentExceptionfor an unknown name. Close the result. -
renderGrid
Render one layer as a UTFGrid: for each small cell of the picture, which feature is there. A web map shows it beside the image to highlight a feature or pop up its details when the pointer is over it, without asking the server again.- Parameters:
layer- the layer to drawkey-"__id__"to name features by their id, or the attribute whose value names themfields- attributes to include in the data for each featureresolution- pixels per cell; 4 is usual, 1 gives one cell per pixel
-
renderGrid
public UtfGrid renderGrid(String layer, String key, Collection<String> fields, int resolution, RenderOptions options) -
renderGrid
A UTFGrid of the layer with features named by their id, including no attributes, at 4 pixels per cell. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-