Mapnik mapper

Map

class shelley.mappers.mapnik.Map(layers, background, srs)

Defines a map that can be drawn

Parameters:
  • layers – list of Layer objects
  • background (string or Color object) – color of the map’s background
  • srs – the projection of the map

Layer

class shelley.mappers.mapnik.Layer(feature_source, styles)

Applies styles to the features in the datasource

Parameters:
  • feature_source – a FeatureSource object
  • styles – a list of FeatureStyle objects

Style

class shelley.mappers.mapnik.Style(name=None, rules=[])
Collects a list of rules, that can be applied to features, together with a name.
>>> feature = Feature(
...     geometry=Geometry(type='LineString', coordinates=[[2, 2], [4, 8], [6, 2], [8, 8]]),
...     properties={'road': 'major'}
... )
>>> style = Style(
...     rules=[Rule(symbolizers=[LineSymbolizer(color=Color(0, 255, 255), width=24, join='round')]),
...            Rule(symbolizers=[LineSymbolizer(color=Color(0, 0, 255), width=8, join='round')])]
... )
_images/style.png

Rule

class shelley.mappers.mapnik.Rule(symbolizers, filter=None, min_scale=None, max_scale=None)

Applies a list of symbolizers to a datasource’s features.

Parameters:
  • symbolizers (list) – symbolizers to apply to geometries
  • filter – Filter object
  • min_scale (float) – minimum scale to draw geometries at
  • max_scale (float) – scale from which geometries will not be drawn

If the filter is specified then the feature must pass the filter before the symbolizers are applied.

If the min or max scale is supplied then the geometries are drawn if the drawing context’s scale >= min_scale and < max_scale

Rule scale

A map scale is the proportion of device units to the map units they represent. For example if a device represents 1000km as 1cm then the scale is said to be 1/100000.

In Shelley the scale is defined by the denominator of the scale assuming a numerator of 1. So a scale of 1/1000 is represented as 1000.

Because a pixel in an image can be different sizes dependant on the monitor being used we assume a pixel size of 0.28mm square.

So for a scale of 1/1000 (denominator=1000) a pixel would represent 0.28mm X 1000 = 2800mm = 28cm = 0.28m

>>> style = Style(
...     rules=[Rule(symbolizers=[LineSymbolizer(width=1)], max_scale_denom=10),
...            Rule(symbolizers=[LineSymbolizer(width=5)], min_scale_denom=10)]
... )

TODO need to show map bounds to illustrate determination of scale

_images/rule_scale.png

Rule filter

>>> style = Style(
...     rules=[Rule(symbolizers=[LineSymbolizer(width=1)], filter=Filter("[road] = 'minor'")),
...            Rule(symbolizers=[LineSymbolizer(width=5)], filter=Filter("[road] = 'major'"))]
... )
_images/rule_filter.png

Filter expressions

>>> feature = Feature(properties={'building': 'factory', 'stories': 5})
>>> Filter("[building] = 'school'").passes(feature)
False

>>> Filter("[building] = 'factory'").passes(feature)
True

>>> Filter("[building] <> 'mosque'").passes(feature)
True

>>> Filter("[building] = 'factory' or [building] = 'school'").passes(feature)
True

>>> Filter("[building] = 'factory' and ([stories] <= 2 or [stories] > 4)").passes(feature)
True

>>> Filter("not ([building] = 'shop') and [stories] < 6").passes(feature)
True

Mapnik XML files

Mapnik has an xml map definition format.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map>
<Map bgcolor="#b5d0d0" srs="+proj=latlong +datum=WGS84">
    <Style name="style1">
        <Rule>
            <PolygonSymbolizer>
                <CssParameter name="fill">#006620</CssParameter>
            </PolygonSymbolizer>
            <LineSymbolizer>
                <CssParameter name="stroke">#00aa20</CssParameter>
                <CssParameter name="stroke-width">0.2</CssParameter>
                <CssParameter name="stroke-linejoin">round</CssParameter>
            </LineSymbolizer>
        </Rule>
    </Style>
    <Layer name="world">
        <StyleName>style1</StyleName>
        <Datasource>
            <Parameter name="type">shape</Parameter>
            <Parameter name="file">${buildout:directory}/parts/world-data/TM_WORLD_BORDERS-0.3</Parameter>
        </Datasource>
    </Layer>
</Map>

We can read this map definition with Shelley.

_images/mapnik.png

Table Of Contents

Previous topic

GeoDjango Datasource

This Page