Skip to content

Vector Symbols

Overview

In this exercise we'll look at how SYMBOLs can be used to apply complex cartographic effects to vector data. The Mapfile displays OpenStreetMap railways in Tartu, Estonia.

Vector Symbols

We've looked at ELLIPSE, and TRUETYPE symbols in the Points Styling exercise. In this example we'll be looking at Vector Symbols - which use vector drawings to define the shape of a symbol.

First you define the SYMBOLs - either be directly in the Mapfile, or in a separate SYMBOLSET file. The advantage of using a separate file is that you can share symbols between maps. A minor disadvantage is we need to deploy an extra file, and ensure the Mapfile has the correct path to the SYMBOLSET - this can be a full path or relative to the Mapfile.

In this example as we only have two symbols we will add them directly to the Mapfile. First we define the symbols. We'll start with a simple vertical line, by defining POINTS as x,y coordinates:

SYMBOL
    NAME "vertline" # we can use this to reference the SYMBOL in STYLEs
    TYPE VECTOR
    FILLED FALSE # the symbol will be treated as a LINE
    POINTS
        0 0
        0 10
    END
END

Next we'll create a simple square symbol:

SYMBOL
    NAME "square"
    TYPE VECTOR
    FILLED TRUE # the symbol will be treated as a POLYGON
    POINTS
        0 0
        0 10
        10 10
        10 0
    END
END

Now we can refer to these symbols by their NAMEs in a STYLE definitions. As out vertline symbol has FILLED is set to FALSE the symbol is treated as a line rather than a polygon. This means a WIDTH can be set on the STYLE to change the width of the symbol.

A GAP is added to the style. The value specifies the distance of the centre of one vertline symbol to the next. The negative value renders the symbols relative to the tangent of the line - without this the lines will be displayed vertically, whereas we want them to cross the underlying line.

STYLE
    COLOR 102 102 102
    SYMBOL "vertline"
    WIDTH 0.4
    GAP -50
END

We'll use the square symbol to represent stations and stops. We can apply a SIZE and COLOR to the symbol:

STYLE
    SYMBOL "square"
    SIZE 16
    COLOR 102 102 102
END

Code

Javascript
import '../css/style.css';
import ImageWMS from 'ol/source/ImageWMS.js';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import { Image as ImageLayer } from 'ol/layer.js';
import { FullScreen, defaults as defaultControls } from 'ol/control.js';

const mapserverUrl = import.meta.env.VITE_MAPSERVER_BASE_URL;
const mapfilesPath = import.meta.env.VITE_MAPFILES_PATH;

const layers = [
    new ImageLayer({
        extent: [2968743.65508978, 8038921.67212233, 2982981.8632402, 8053818.05714347],
        source: new ImageWMS({
            url: mapserverUrl + mapfilesPath + 'railways.map&',
            params: { 'LAYERS': 'tracks,stops'},
            ratio: 1
        }),
    }),
];
const map = new Map({
    layers: layers,
    target: 'map',
    controls: defaultControls().extend([
        new FullScreen()
    ]),
    view: new View({
        center: [2971862.75916499, 8046369.8646329],
        zoom: 15,
    }),
});
Mapfile
MAP
    NAME "Railways"
    EXTENT 26.668678 58.339241 26.796582 58.40941
    UNITS DD
    SIZE 800 600
    PROJECTION
        "init=epsg:4326"
    END
    SYMBOL
        NAME "vertline" # we can use this to reference the SYMBOL in STYLEs
        TYPE VECTOR
        FILLED FALSE # the symbol will be treated as a LINE
        POINTS
            0 0
            0 10
        END
    END
    SYMBOL
        NAME "square"
        TYPE VECTOR
        FILLED TRUE # the symbol will be treated as a POLYGON
        POINTS
            0 0
            0 10
            10 10
            10 0
        END
    END
    WEB
        METADATA
            "ows_enable_request" "*" 
            "ows_srs" "EPSG:4326 EPSG:3857"
        END
    END
    LAYER
        NAME "tracks"
        STATUS OFF
        TYPE LINE
        CONNECTIONTYPE FLATGEOBUF
        DATA "data/osm/railways.fgb"
        CLASS
            EXPRESSION ( "[name]" != "" )
            STYLE
                COLOR 102 102 102
                WIDTH 4.0
            END
            STYLE
                COLOR 255 255 255
                WIDTH 2.0
                LINECAP BUTT
                PATTERN
                    8 12
                END
            END
            LABEL
                TEXT "[name]"
                COLOR 102 102 102
                SIZE 12
                ANGLE FOLLOW
                OFFSET 8 -99
            END
        END
        CLASS
            STYLE
                COLOR 102 102 102
                WIDTH 0.8
            END
            STYLE
                COLOR 102 102 102
                SYMBOL "vertline"
                WIDTH 0.4
                GAP -50
            END
        END
    END

    LAYER
        NAME "stops"
        STATUS OFF
        TYPE POINT
        TEMPLATE "void"
        METADATA
            "gml_include_items" "all"
            "gml_featureid" "osm_id"
        END
        CONNECTIONTYPE FLATGEOBUF
        DATA "data/osm/transport.fgb"
        CLASSITEM "fclass"
        CLASS
            EXPRESSION {railway_station,railway_halt}
            STYLE
                SYMBOL "square"
                SIZE 16
                COLOR 102 102 102
            END
            LABEL
                TEXT "[name]"
                POSITION ur
                COLOR 102 102 102
                SIZE 18
            END
        END
        END
    END
END

Exercises

  1. Try experimenting with the GAP values to see how this affects the output.

    STYLE
        COLOR 102 102 102
        SYMBOL "vertline"
        WIDTH 0.4
        GAP -50
    END
    
  2. Change the square symbol used for stations into a different shape such as a triangle.

Further Reading