3.6.1. sort-by attribute
Features are normally rendered in the order in which the data source
returns them. With sort-by all features of the layer are read into
memory first, sorted by the given column, and then rendered by all of
the layer’s styles in that order. This has two visible effects:
-
features rendered later are drawn on top of features rendered earlier
-
features rendered earlier win when labels or symbols compete for the same space, as placement is first come, first served
So e.g. sort-by="population DESC" makes sure that the names of large
cities get placed in preference to those of smaller ones, and that
the symbols of small cities are not hidden below those of large ones.
The value is a single plain column name, optionally followed by ASC
(the default) or DESC. It is not an expression, so no square
brackets, and only one column can be given. Anything else is a parse
error.
With data sources that support it, like PostGIS, you can get the same
result with an ORDER BY in the query. sort-by brings this to data
sources that can’t sort themselves, like shapefiles, CSV or GeoJSON.
Things to be aware of:
-
sort-bytakes precedence overgroup-by, when both are setgroup-byis silently ignored. -
All features within the query extent are kept in memory, like with
cache-features. -
The sort is not stable, features with equal values may be rendered in any order.
-
Strings are compared by character code, not according to locale rules, so all upper case letters sort before all lower case ones.
-
The column should only contain values of one kind. Numbers can’t be compared to strings, and neither can be compared to missing values, so the result order for mixed columns is unpredictable.
-
sort-bywas added in Mapnik v4.1.4 and is not available in the Python bindings yet.
In the following example the same three cities are rendered three times: in data source order, sorted by population in ascending order, and in descending order. Their names are too close to each other for more than one to be placed, so only the first rendered city gets its name shown. Only in the descending case is the large city labeled, and the smaller cities are drawn on top of the larger ones.
<?xml version="1.0" encoding="utf-8"?>
<Map background-color="white" minimum-version="4.1.4">
<Style name="cities">
<Rule>
<MarkersSymbolizer width="[size]" height="[size]" fill="[color]" stroke="white"
allow-overlap="true" ignore-placement="true"/>
<TextSymbolizer face-name="DejaVu Sans Book" size="14" dy="-40">[name]</TextSymbolizer>
</Rule>
</Style>
<Style name="captions">
<Rule>
<TextSymbolizer face-name="DejaVu Sans Bold" size="14">[caption]</TextSymbolizer>
</Rule>
</Style>
<Layer name="unsorted">
<StyleName>cities</StyleName>
<Datasource>
<Parameter name="type">csv</Parameter>
<Parameter name="inline">
wkt,name,population,size,color
"POINT(0.5 0)",Middletown,150000,40,seagreen
"POINT(0.9 0)",Smallville,20000,24,orange
"POINT(0 0)",Big City,900000,64,steelblue
</Parameter>
</Datasource>
</Layer>
<Layer name="ascending" sort-by="population">
<StyleName>cities</StyleName>
<Datasource>
<Parameter name="type">csv</Parameter>
<Parameter name="inline">
wkt,name,population,size,color
"POINT(4.5 0)",Middletown,150000,40,seagreen
"POINT(4.9 0)",Smallville,20000,24,orange
"POINT(4 0)",Big City,900000,64,steelblue
</Parameter>
</Datasource>
</Layer>
<Layer name="descending" sort-by="population DESC">
<StyleName>cities</StyleName>
<Datasource>
<Parameter name="type">csv</Parameter>
<Parameter name="inline">
wkt,name,population,size,color
"POINT(8.5 0)",Middletown,150000,40,seagreen
"POINT(8.9 0)",Smallville,20000,24,orange
"POINT(8 0)",Big City,900000,64,steelblue
</Parameter>
</Datasource>
</Layer>
<Layer name="captions">
<StyleName>captions</StyleName>
<Datasource>
<Parameter name="type">csv</Parameter>
<Parameter name="inline">
wkt,caption
"POINT(0.5 -1.5)",(unsorted)
"POINT(4.5 -1.5)",population
"POINT(8.5 -1.5)",population DESC
</Parameter>
</Datasource>
</Layer>
</Map>