Introduction to the MapnikRasterizer

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2021.0

Introduction

The MapnikRasterizer transformer lets you generate high-quality cartographic rasters using Mapnik, a free toolkit for rendering maps. When used in an FME workflow, the transformer accepts vector or raster data features through the input ports, generates a stylized map based on parameters set by the user, and outputs a raster through the output port.

The below map was generated and styled using the MapnikRasterizer:

http_download_14443297843_6032.png
 

This article will cover step-by-step instructions for building an FME Workspace that uses Mapnik to generate a stylized raster map. At the bottom, you can download additional workspaces to try more Mapnik styling examples.

How the MapnikRasterizer Works

Styling is done using Mapnik Symbolizers, which you define in the transformer parameters. The following table outlines what styling options you can set in each Symbolizer. For example, using the Polygon Symbolizer, you can set the color, smoothing, and compositing.
 
 ColorStylePositioningSmoothingPropertiesCompositing
Buildingxx   x
Linexxxx x
Line Pattern   xxx
Markersx xxxx
Point  x xx
Polygonx  x x
Polygon Pattern   xxx
Raster    xx
Shieldx x xx
Textx x xx

The styling options let you set the appearance using a color and opacity or a raster texture/pattern, add an offset and other positioning adjustments, smooth lines and edges for better aesthetics, and more. "Compositing" defines how the layers should interact, similar to the blending modes in graphics editors like Photoshop or GIMP, and includes masking operations (e.g. Src-Over) and blending operations (e.g. Multiply).​​​​

Many styling options allow conditional values, where you can use a table of test conditions to assign values. Check the Mapnik Symbology documentation for full details about Symbolizers.

Step-by-Step Instructions

In this scenario, we will create an FME workspace that reads vector geometry from a SpatiaLite database, uses Mapnik to style the geometry, and generates a raster in the form of a PNG. The source dataset contains a single polygon, and we will use multiple Mapnik Symbolizers within a single transformer to style it.

Follow the steps below to create the workspace from scratch, or open the Finland1.fmwt template in MapnikTutorial.zip to view a completed version.

Part 1: Basic Styling

In Part 1, we will walk through the MapnikRasterizer parameters and style a Finland map by setting the background, extents, and geometry colors. This basic styling is designed to get you familiarized with Mapnik Symbolizers. More sophisticated styling is done in Part 2.

1. Generate a Workspace

Open FME Workbench and generate a new workspace with the following parameters:
  • Reader Format: SpatiaLite
  • Reader Dataset: C:\<Tutorial Download>\source\finland.sl3
  • Writer Format: PNG (Portable Network Graphics)
  • Writer Dataset: C:\<Tutorial Download>\finland-styled
  • Workflow Options: Static Schema

01 Generate Workspace.PNG

Click OK to generate the workspace. The canvas will have a reader feature type on the left and a writer feature type on the right.

2. Add a MapnikRasterizer Transformer

Click anywhere on the canvas and begin typing "MapnikRasterizer". Add the transformer to the canvas and connect it between the source and destination feature types. The workspace should look like this:

02 Workspace.png

Note that the MapnikRasterizer has dynamic input ports, which means a new input port is generated for every feature stream connected to it.

3. Open the MapnikRasterizer Parameters

Double-click the transformer to open its parameters. This dialog has several sections, which we will configure as follows.

03 Parameters.PNG

(a) The "Group Processing" section includes "Group By" mode, a setting that relates to performance and can usually be left as-is. Leave this unchecked.

(b) The "Rendering Rules" section tells Mapnik how to style the input features. This where you use Mapnik Symbolizers. Layers are drawn in the order that they appear in this table, with the top drawn first. First, add a rule to style the geometry using the Polygon Symbolizer:

  • Input Port: finland
  • Symbolizer: Polygon
  • Style: Click "Edit..."
    • Color: Click [...] and choose Green; set the opacity to 1 (1 is opaque; 0 is transparent)
    • Leave the Smoothing and Compositing parameters as-is

This rule will use Mapnik's Polygon Symbolizer to make the polygon Green. Now add a second rule:

  • Input Port: finland
  • Symbolizer: Line
  • Style: Click "Edit..."
    • Color: Click [...] and choose Yellow; opacity 1
    • Style: set Width (Pixels) to 6, Join to Round, Cap to Round, Miter Limit to 4
    • Positioning: set Offset to 0
    • Smoothing: set Gamma to 1.0, Gamma Method to Power, Smooth to 0.25, Rasterizer to Full
    • Compositing: set Compositing Operation to Src-Over

This rule will use Mapnik's Line Symbolizer to treat the input polygon as a line and make it Yellow, with other styling applied to make the line appear wide and smooth.

(c) The "Raster Properties" section defines the number of rows and columns or cell size, as well as the interpretation type (RGB24 or RGBA32). Set the following raster properties:

  • Resolution Specification: Cell Spacing
  • Cell Spacing: 2500
  • Interpretation Type: RGB24

(d) The "Background" section allows setting the background image or color and opacity.

  • Color: Click [...] and set the color to Cyan
  • Color Alpha Value: 1

(e) The "Ground Extents" section allows you to manually set the extents of the output raster. By default, the extents are defined by the input features, but sometimes it's necessary to adjust the boundaries of the map depending on how you have styled it. For example, in the rasters below, the extents of the left one are defined by the input (default), while the extents of the on the right are defined manually in the parameters. The user has added a buffer to account for the wide line styling.

http_download_1444329784868_6032.png
For this scenario, set the extents as follows:

  • Ground Extents: Specify ground extents
  • Minimum X: 3176500
  • Minimum Y: 6630000
  • Maximum X: 3743000
  • Maximum Y: 7790000

Tip: To find out what extents you need, you can generate a bounding box that encompasses all the features using the BoundingBoxAccumulator transformer and buffer it using the Bufferer transformer. Then extract the min/max coordinates of the box into attributes using the BoundsExtractor, log them using the Logger, and use the values in the "Ground Extents" section. You could also send the bounding box to the MapnikRasterizer as a polygon and set its rendering rule to full transparency, then let Mapnik automatically use this transparent box as the extents.

4. Run the workspace

Run the workspace. The finland.sl3 dataset will be styled according to the MapnikRasterizer parameters and output to a PNG file in the finland-styled folder.

http_download_1444329785269_6032.png

Part 2: Styling with Patterns and Text

Now that you are familiar with the MapnikRasterizer parameters, let's modify the Rendering Rules and add nicer styling to the Finland map, including setting the appearance using a raster pattern instead of colors and adding text.

Follow the steps below to modify the existing workspace from Part 1. The completed workspace for Part 2 is attached in MapnikTutorial.zip, named Finland2.fmwt.

1. Open the MapnikRasterizer parameters in the existing workspace

In your existing workspace from Part 1 (or working from the Finland1.fmwt workspace), open the MapnikRasterizer parameters. We will use 5 Rendering Rules to style the input dataset.

2. Change the first rule to use the Polygon Pattern Symbolizer

First we will create a Rendering Rule to set the Finland polygon to a nice image instead of solid green. Change the Polygon rule to have the following parameters:

  • Input Port: finland
  • Symbolizer: Polygon Pattern
  • Style: Click 'Edit...' and configure it as follows
polygon pattern.PNG

3. Keep the Line Symbolizer rule

Keep the next rule that uses the Line Symbolizer as-is. We will style the border as yellow.

4. Create a new rule using the Line Pattern Symbolizer

The third rule will be to add a boundary symbol pattern to the yellow border. Add a new rule with the following parameters:

  • Input Port: finland
  • Symbolizer: Line Pattern
  • Style: Click 'Edit...' and configure it as follows
 LinePattern.PNG

5. Create a new rule using the Text Symbolizer

The fourth rule will be to add a label based on the data's "name" attribute (which we know contains "Finland"). Add a new rule with the following parameters:

  • Input Port: finland
  • Symbolizer: Text
  • Style: Click 'Edit...' and configure it as follows
Text.PNG

The "Positioning" section has quite a few parameters, but you only need to set the following:
  • Character Spacing: 10
  • Avoid Edges: Yes
  • Placement: Interior
Leave the rest as-is and click OK.

6. Create a new rule using the Point Symbolizer

The fifth and final rule will be to add a flag to the middle of the image as a point. Add a new rule with the following parameters:

  • Input Port: finland
  • Symbolizer: Point
  • Style: Click 'Edit...' and configure it as follows
Point.PNG

You should now have five Symbolizers configured and the Rendering Rules section should look like this:

Finished transformer.PNG

Click OK to close the MapnikRasterizer parameters.

7. Run the workspace

Run the workspace. The finland.sl3 dataset will be styled according to the MapnikRasterizer parameters and output to a PNG file in the finland-styled folder.
 

http_download_1444329785740_6032.png
 

Additional Examples & Downloads

Download these additional workspaces to see more examples of Mapnik styling and experiment with different Symbolizers.
 
http_download_144432978632_6032.pngLineStyling.fmwCreates various line styles using the Line Symbolizer, including dashed lines and different caps and joins.
http_download_1444329786572_6032.pngCompositing.fmwStyles a line multiple times with different widths, and the layers are blended using a "Plus" compositing operation. Also applies smoothing.
rtaImage.pngRapidTransitMap.fmwtGenerates a map of a transit system, including line and point styling, text labels, and a background orthophoto.
Screen Shot 2021-03-12 at 12.14.24 PM.pngConditionalStreets.fmwtUses test conditions to set the symbology based on the value of an attribute, e.g. styling main arteries vs. residential streets.
Screen Shot 2021-03-12 at 11.50.27 AM.pngExternalSymbology.fmwtGenerates a labelled map using external symbology from an Excel file. You can use this method to gather symbology from an external database, as well.
http_download_1444329787325_6032.jpegBuildingStyling.fmwtAdds a 3D element to a 2D map by styling buildings using a Height parameter and extruding polygons and text labels.
http_download_1444329787650_6032.pngRandomSymbolPlacement.fmwtUses a point cloud to randomly place symbols to represent natural features – in this case, tree symbols to represent a forest.
http_download_1444329788635_6032.pngTextures.zipApplies random textures to polygons and lines.
http_download_144432978893_6032.pngContouredFaces.fmwtCreates contour art from an image of a face. Inspired by a collection of maps with faces made by Ed Fairburn.

 

Additional Resources

Webinar: The Secret to Mapnik Mastery

Blog: 5 Ways to Do More with Mapnik
 
 

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.