Map objects in a single layer often have different attributes. For instance, roads have a RoadType attribute giving the importance of the road, whether it is one-way, whether it has a central divider, etc. These attributes affect the way the road is drawn.

You use <condition> sections inside <layer> to treat objects with differing attributes in different ways. Each <condition> section is like an embedded <layer> section for only those objects with the specified attributes.

A <condition> section inherits all attributes defined in the containing <layer>, unless it is nested inside another condition, in which case it inherits from the containing condition. Conditions may be nested up to 15 levels deep.

You set the actual condition using a style sheet expression in the exp attribute. Expressions test integer and string attributes of map objects; if the expression is true, the object is drawn using the styles given inside the condition element

This is hard to understand without an example. Here is the road layer from a style sheet formerly used by the CartoType demo programs:

<layer name="road">
   <line width="30m" fill="orange" border="black" borderWidth="5%"/>
   <label font-size="75%,8pt"/>
   <condition exp="RoadType in [#400,#BFF]">
      <scale max="50000"/>
      <line width="18m" fill="yellow" border="black" borderWidth="5%"/>
      <label font-size="75%,8pt" maxScale="15000"/>
      <oneWayArrow path="M 0 50 H 300 L 40 300 H 180 L 505 0 L 180 -300 H 40 L 300 -50 H 0 Z" fill="teal" opacity="0.5"/>
   </condition>
   <condition exp="(RoadType bitand #FF00)==#300">
      <scale max="100000"/>
      <line width="22m,2pt" fill="orange" border="black" borderWidth="5%" centerLine="white" centerLineWidth="5%"/>
      <label font-size="75%,9pt"/>
      <oneWayArrow path="M 0 50 H 300 L 40 300 H 180 L 505 0 L 180 -300 H 40 L 300 -50 H 0 Z" fill="teal" opacity="0.5"/>
   </condition>
   <condition exp="(RoadType bitand #FF00)==#200">
      <line width="30m,2pt" fill="tomato" border="black" borderWidth="5%" centerLine="pink" centerLineWidth="5%"/>
      <label font-size="75%,10pt"/>
      <oneWayArrow path="M 0 50 H 300 L 40 300 H 180 L 505 0 L 180 -300 H 40 L 300 -50 H 0 Z" fill="yellow" opacity="0.5"/>
   </condition>
   <condition test="(RoadType bitand #FF00)==#100">
      <line width="45m,2pt" fill="mediumturquoise" border="black" borderWidth="5%" centerLine="white" centerLineWidth="5%"/>
      <label font-size="75%,10pt"/>
      <oneWayArrow path="M 0 50 H 300 L 40 300 H 180 L 505 0 L 180 -300 H 40 L 300 -50 H 0 Z" fill="yellow" opacity="0.5"/>
   </condition>
</layer>

The first condition has the test "RoadType in [#400,#BFF]", meaning that only roads with road types in the range 400 (hex) to BFF (hex) inclusive are drawn by the specifications in this condition. These are minor and unclassified roads, and are drawn by this style sheet in yellow and not drawn at smaller scales than 1:15000.

Here is an example showing the inheritance of attributes from the containing layer:

<layer name='waterway'>
<label font-style='italic' color='blue' case='assume-title' glow='white'/>
   <!-- RIVER -->
   <condition exp='(Type bitand #FFFF0000)=="riv"'>
   <line width='20m,1pt' fill='skyblue'/>
   <label font-size='75%,8pt' maxScale='1000000'/>
   </condition>
   <!-- CANAL -->
   <condition test='(Type bitand #FFFF0000)=="can"'>
   <line width='8m,1pt' fill='skyblue'/>
   <label font-size='75%,8pt' maxScale='1000000'/>
   </condition>
   <!-- STREAM -->
   <condition test='(Type bitand #FFFF0000)=="str"'>
   <line width='8m,1pt' fill='skyblue'/>
   <label font-size='75%,8pt' maxScale='250000'/>
   </condition>
   <!-- DRAIN -->
   <condition test='(Type bitand #FFFF0000)=="dra"'>
   <line width='4m,1pt' fill='skyblue'/>
   <label font-size='75%,8pt' maxScale='25000'/>
   </condition>
</layer>

And here is an example of a nested condition:

<!-- UNCLASSIFIED AND TERTIARY -->
<condition test='(RoadType bitand #F00)==#400'>
<scale max='400000'/>
<line width='25m,0.5pt' fill='ivory' border='black' borderWidth='8%,0.7,2'/>
<label font-size='70%,8pt' glow='white' color='black' case='assume-title' maxScale='20000'/>
<oneWayArrow path='#arrow' gap='2500' fill='teal' opacity='0.5' isLabel='yes'/>
<bridge width='38m,2pt' border='black' borderWidth='14%,1' endPath='l 512 512' maxScale='50000'/>
<tunnel dashArray='1,1' fill='grey' mouth='black' mouthWidth='20%,1' mouthPath='m 0 512 a 512 512 0 1 1 0 -1024'/>
   <!-- TERTIARY -->
   <condition test='(RoadType bitand #F80)==#400'>
   <line fill='khaki'/>
</condition>

Here, tertiary roads are exactly the same as unclassified roads except for their fill color, so a lot of unnecessary repetition is avoided. But note that all the nested tests must succeed for this to work. In this example, the test '(RoadType bitand #F00)==#400' selects both unclassified and tertiary roads, while '(RoadType bitand #F80)==#400' selects tertiary only. When the map is drawn, objects are matched to the innermost test that succeeds.

Style Sheets Directory