Class MapnikMap

java.lang.Object
dev.avelar.mapnik.MapnikMap
All Implemented Interfaces:
AutoCloseable

public final class MapnikMap extends Object implements AutoCloseable
A Mapnik map. Not thread-safe: use one instance per thread.
  • Constructor Details

    • MapnikMap

      public MapnikMap(int width, int height)
  • Method Details

    • load

      public MapnikMap load(Path styleXml)
      Load a Mapnik XML style from a file. Its layers are added to the map.
    • load

      public MapnikMap load(Path styleXml, boolean strict)
      As load(Path). With strict, Mapnik reports problems it would otherwise skip, such as unknown attributes.
    • loadString

      public MapnikMap loadString(String xml, Path basePath)
      Load a Mapnik XML style from a string. Relative paths in it resolve against basePath.
    • loadString

      public MapnikMap loadString(String xml, Path basePath, boolean strict)
      As loadString(String, Path), optionally strict as in load(Path, boolean).
    • validate

      public static List<String> validate(String xml, Path basePath)
      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

      public String toXml(boolean explicitDefaults)
      The map as Mapnik XML. With explicitDefaults, settings at their default value are written too.
    • toXml

      public String toXml()
    • save

      public void save(Path xmlFile, boolean explicitDefaults)
    • width

      public int width()
    • height

      public int height()
    • resize

      public MapnikMap resize(int width, int height)
    • srs

      public String srs()
      The map projection, as given in the style or by setSrs(java.lang.String), e.g. epsg:4326.
    • setSrs

      public MapnikMap setSrs(String srs)
      Set the map projection: anything Mapnik accepts, such as epsg:3857 or a PROJ string. Layers keep their own projection and are reprojected when rendered. The extent passed to zoomToBox(double, double, double, double) is in this projection, so set it first. An invalid value fails at render time.
    • background

      public Optional<String> background()
      The background colour as Mapnik prints it, for example rgb(255,255,255) or rgba(0,0,255,0.5), if set.
    • setBackground

      public MapnikMap setBackground(String color)
      Set the background colour: a name ("white"), "#rrggbb", or "rgba(r,g,b,a)".
    • setBackground

      public MapnikMap setBackground(Color color)
    • backgroundImage

      public Optional<String> backgroundImage()
    • setBackgroundImage

      public MapnikMap setBackgroundImage(Path image)
    • backgroundImageCompOp

      public Optional<String> backgroundImageCompOp()
      How the background image is blended onto the background colour, by name, such as multiply. Empty if unset.
    • setBackgroundImageCompOp

      public MapnikMap setBackgroundImageCompOp(String name)
    • setBackgroundImageCompOp

      public MapnikMap setBackgroundImageCompOp(BlendMode mode)
    • backgroundImageOpacity

      public double backgroundImageOpacity()
    • setBackgroundImageOpacity

      public MapnikMap setBackgroundImageOpacity(double opacity)
    • bufferSize

      public int bufferSize()
      Extra pixels rendered around the map so labels and symbols at the edge are not cut off.
    • setBufferSize

      public MapnikMap setBufferSize(int pixels)
    • registerFonts

      public MapnikMap registerFonts(Path dir, boolean recurse)
      Fonts directory for this map.
    • loadFonts

      public MapnikMap loadFonts()
    • fontDirectory

      public Optional<String> fontDirectory()
      The directory set for fonts on this map, if any.
    • setFontDirectory

      public MapnikMap setFontDirectory(Path dir)
    • parameters

      public Map<String,Object> parameters()
      Arbitrary key/value pairs stored on the map and written as <Parameters> in its XML, for your own metadata. In key order. Values are String, Boolean, Long, Double or null.
    • setParameter

      public MapnikMap setParameter(String key, Object value)
      Set a parameter: a String, Boolean, Integer, Long, Double or Float. Replaces an existing one.
    • removeParameter

      public MapnikMap removeParameter(String key)
    • toPixel

      public Point2d 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. Needs an extent, for example from zoomToBox(double, double, double, double).
    • toPixel

      public Point2d toPixel(Point2d world)
    • toWorld

      public Point2d toWorld(double pixelX, double pixelY)
      The map coordinates under a pixel of the rendered image.
    • toWorld

      public Point2d toWorld(Point2d pixel)
    • basePath

      public String basePath()
    • setBasePath

      public MapnikMap setBasePath(Path dir)
      Directory that relative paths in the style are resolved against.
    • aspectFixMode

      public AspectFixMode aspectFixMode()
    • setAspectFixMode

      public MapnikMap setAspectFixMode(AspectFixMode mode)
      How zoomToBox(double, double, double, double) treats a box whose aspect ratio differs from the map's.
    • extent

      public Box2d extent()
      The extent currently shown, in the map's projection.
    • bufferedExtent

      public Box2d bufferedExtent()
      extent() grown by the buffer size.
    • maximumExtent

      public Optional<Box2d> maximumExtent()
      The extent beyond which nothing is drawn, if set.
    • setMaximumExtent

      public MapnikMap setMaximumExtent(Box2d box)
    • resetMaximumExtent

      public MapnikMap resetMaximumExtent()
    • zoomToBox

      public MapnikMap zoomToBox(double minx, double miny, double maxx, double maxy)
    • zoomToBox

      public MapnikMap zoomToBox(Box2d box)
    • zoomAll

      public MapnikMap zoomAll()
      Fit the extent of all layers' data.
    • zoom

      public MapnikMap zoom(double factor)
      Scale the extent about its centre by factor: above 1 zooms out (shows more), below 1 zooms in. zoom(0.5) shows half the width and height.
    • pan

      public MapnikMap pan(int x, int y)
      Move the view by a pixel offset.
    • panAndZoom

      public MapnikMap panAndZoom(int x, int y, double zoom)
      Centre the view on pixel (x, y), then scale the extent by zoom as zoom(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

      public List<String> layerNames()
      Layer names in drawing order: the first is drawn first, so it ends up at the bottom.
    • layers

      public List<Layer> layers()
      Views onto all layers, in drawing order. See Layer for how long they stay valid.
    • layer

      public Layer layer(int index)
      A view onto the layer at index.
    • layer

      public Layer layer(String name)
      A view onto the first layer with this name. Throws IllegalArgumentException if there is none.
    • addLayer

      public MapnikMap addLayer(Layer layer)
      Add a copy of layer on top of the existing layers. Invalidates layer views from this map.
    • removeLayer

      public MapnikMap removeLayer(int index)
    • removeLayer

      public MapnikMap removeLayer(String name)
    • removeAll

      public MapnikMap removeAll()
      Remove all layers and styles.
    • isLayerActive

      public boolean isLayerActive(String name)
      Whether the named layer is drawn. Layers are active by default.
    • setLayerActive

      public MapnikMap setLayerActive(String name, boolean active)
      Turn one layer on or off. Throws IllegalArgumentException for an unknown name.
    • setActiveLayers

      public MapnikMap setActiveLayers(Collection<String> names)
      Draw only these layers; all others are turned off. Drawing order stays the style's order, not the order of names. Throws IllegalArgumentException for an unknown name, and changes nothing in that case.
    • activateAllLayers

      public MapnikMap activateAllLayers()
      Turn every layer back on.
    • addStyle

      public MapnikMap addStyle(Style style)
      Add a style built in code, checked by Mapnik's strict XML loader. Throws IllegalArgumentException if the map already has a style of that name (Mapnik would silently ignore the new one), and MapnikException if 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

      public MapnikMap addStyle(Style style, boolean strict)
      As addStyle(Style). With strict false, 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

      public MapnikMap replaceStyle(Style style)
      Add a style, replacing one of the same name if there is one.
    • hasStyle

      public boolean hasStyle(String name)
    • addFontSet

      public MapnikMap addFontSet(FontSet fontSet)
      Add a font set for text symbolizers to use with fontset-name. The fonts must already be registered (see Mapnik.registerFonts(java.lang.String)), or Mapnik throws MapnikException.
    • queryPoint

      public Featureset queryPoint(String layer, double x, double y)
      Features of a layer at a point, given in the map's projection. The layer must be queryable (see Layer.setQueryable(boolean)) for some plugins. Close the result.
    • queryMapPoint

      public Featureset queryMapPoint(String layer, double px, double py)
      Features of a layer under pixel (px, py) of the map image. Close the result.
    • styleNames

      public List<String> styleNames()
      Names of the styles defined in the map, sorted.
    • styles

      public List<Style> 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 as rgb(255,0,0) and filters in parentheses, and leaves out attributes that are at their default. Adding a style read back with addStyle(dev.avelar.mapnik.Style) to another map draws the same.
    • style

      public Optional<Style> style(String name)
      One style by name, read back as in styles(), or empty if the map has none of that name.
    • removeStyle

      public MapnikMap removeStyle(String name)
    • renderToFile

      public void renderToFile(Path out, String format)
      Render to a file. format is an image format (png, jpeg, webp, tiff, with options such as png8, png256, jpeg90) or a vector format (pdf, svg, ps, which need Cairo support in Mapnik).
    • renderToFile

      public void renderToFile(Path out, String format, RenderOptions options)
    • renderToFile

      public void renderToFile(Path out, ImageFormat format)
      As renderToFile(Path, String), with a format built by ImageFormat.
    • renderToBytes

      public byte[] renderToBytes(ImageFormat format)
      As renderToBytes(String), with a format built by ImageFormat, such as ImageFormat.png8().colors(64).
    • renderToBytes

      public byte[] renderToBytes(ImageFormat format, RenderOptions options)
    • renderToBytes

      public byte[] renderToBytes(String format)
      Render to memory. Accepts the same formats as renderToFile(java.nio.file.Path, java.lang.String).
    • renderToBytes

      public byte[] renderToBytes(String format, RenderOptions options)
    • renderToPng

      public byte[] renderToPng()
    • renderToImage

      public Image renderToImage()
      Render to a new Image the size of the map. Close it when done.
    • renderToImage

      public Image renderToImage(RenderOptions options)
    • render

      public MapnikMap render(Image image)
      Draw onto an existing image, blending over what is there. The image must be the same size as the map.
    • render

      public MapnikMap render(Image image, RenderOptions options)
    • renderTile

      public Image renderTile(Tiles.Tile tile, int buffer)
      Render one tile of the standard web map grid (see Tiles) 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.

      buffer is 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

      public Image renderTile(int z, int x, int y, int buffer)
    • renderTileToBytes

      public byte[] renderTileToBytes(Tiles.Tile tile, String format, int buffer)
      A tile encoded as png, jpeg and so on, as for renderToBytes(dev.avelar.mapnik.ImageFormat).
    • renderLayers

      public Image renderLayers(Collection<String> names)
      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 of names. Throws IllegalArgumentException for an unknown name. Close the result.
    • renderGrid

      public UtfGrid 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. 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 draw
      key - "__id__" to name features by their id, or the attribute whose value names them
      fields - attributes to include in the data for each feature
      resolution - 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

      public UtfGrid renderGrid(String layer)
      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:
      close in interface AutoCloseable