Adding filters to your style

Having a map with a single symbol (that is, the same appearance for all features) is not very common. This approach is useful, and it can be used when building an index map to provide a reference showing you which part of the world is represented in the main map.

The real power of styling comes from the representation of features according to their properties, geometrical or nongeometrical. A map created to show attribute values is a thematic map. To create a thematic map, we will need to use filters to select content based on an attribute value.

In this recipe, you will build a map of countries grouped according to the continent they belong.

Tip

The full CSS style for the recipe is located in the continent.css file.

How to do it…

  1. Open the CSS Style section from the GeoServer web interface. Then, select the Choose a different layer link. From the list of layers, select PostGISLocal:Countries.
  2. Select the Create a new style link. In the form, select the NaturalEarth workspace and insert Continents as the style name.
  3. Replace the code in the textbox with the following snippet:
    /* @title outline */
    * { 
        stroke: #000000;
        stroke-width: 0.1;
      }
    
    /* @title North America */
    [continent = 'North America']
    * { fill: #66C2A5;}
  4. Press the Submit button and look at your map. It should show all countries with a thin black outline and the North American countries with a color fill.
  5. Add filling for other continents, as shown in the following code snippet:
    /* @title Europe */
    [continent = 'Europe']
    * { fill: #8DA0CB; }
    
    /* @title Oceania */
    [continent = 'Oceania']
    * { fill: #E78AC3; }
    
    /* @title Asia */
    [continent = 'Asia']
    * { fill: #A6D854; }
    
    /* @title Africa */
    [continent = 'Africa']
    * { fill: #FFD92F; }
    
    /* @title Antartica */
    [continent = 'Antarctica']
    * { fill: #E5C494; }
    
    /* @title South America */
    [continent = 'South America']
    * { fill: #FC8D62; }
  6. Press the Submit button. Your map should look like the following screenshot:
    How to do it…

How it works…

This time, you created a style with a few more rules. You may have noted in the previous screenshot that the CSS module creates a simple table of contents (TOC) for you with elements describing symbols used in the style and a label for each symbol.

You gave directions to GeoServer with the @title marker:

* @title outline */

In this case, you are defining the symbol for the outline. So, you typed the word outline after the marker. The label is free text, so you can insert anything and use more than a word. You must have guessed from the * selector that the outline is applied to all features.

We use the key property stroke to generate a line and stroke-width for a thin black line:

* { 
    stroke: #000000;
    stroke-width: 0.1;
  }

Next, you create a filter for countries belonging to North America. To group them, we use the continent field attribute, that is, an attribute of the countries layer, and of course, we define a very simple filter that selects just features that hold the North America value in that field:

/* @title North America */
[continent = 'North America']
* { fill: #66C2A5;}

We use the key property fill to ask these features to be drawn as a polygon.

We use the @title marker again to define a label. Please note that when more than one rule applies to a feature, the corresponding label is the merger of all the strings:

How it works…

We then inserted similar filters for all remaining continents. The code is very easy; just copy and paste and change the color codes, labels, and filter values.

When we submit our code that is just 26 lines, including labels, the resulting XML is a bit more complex. It is translated into 194 lines of XML code. If you're curious about it, you can switch to the Generated SLD tab in the CSS panel. This shows you the XML code formatted according to the SLD syntax.

Here is a fragment that is the corresponding code for the North America filter shown previously:

<sld:Rule>
  <sld:Title>North America  with Outline</sld:Title>
  <ogc:Filter>
    <ogc:PropertyIsEqualTo>
      <ogc:PropertyName>continent</ogc:PropertyName>
      <ogc:Literal>North America</ogc:Literal>
    </ogc:PropertyIsEqualTo>
  </ogc:Filter>
  <sld:PolygonSymbolizer>
    <sld:Fill>
      <sld:CssParameter name="fill">#66C2A5</sld:CssParameter>
    </sld:Fill>
  </sld:PolygonSymbolizer>
  <sld:LineSymbolizer>
    <sld:Stroke>
      <sld:CssParameter name="stroke-width">0.1</sld:CssParameter>
    </sld:Stroke>
  </sld:LineSymbolizer>
</sld:Rule>
<sld:Rule>

Isn't the CSS syntax far more simple and readable?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.212