Meanwhile I found a solution to this problem (there might be others of course), using variable substitution in the
WMS getMap request.
At first, create the static elements of my style as desired. I prefer using CSS Styling over writing the sld file directly but this is up to you.
Then, open the style in the Style Editor in GeoServer's Web Interface and add the following at the bottom of the SLD xml definitions, below the last
<sld:FeatureTypeStyle>...</sld:FeatureTypeStyle> entry but still above the closing
Note the new
<ogc:Filter> in the upper part. It defines a new rule which would be in plain text kind of like
Only style features where the attribute "id" equals the value that is passed in the environment variable that is called "lowestID".
To apply the same filter also for the highest ID, just copy and paste it, changing
highestID and modifying the styling to your needs.
The lower part, between
<sld:PointSymbolizer>...</sld:PointSymbolizer> defines the styling that is to be applied to the features that pass the filter. In this example a simple black square is used but it can be replaced with any valid styling description of course.
Then, the lowest ID has to be found out somehow before sending the
GetMap request to GeoServer. As my instance of GeoServer pulls its geodata from a connected PostGIS database for which I have access, fortunately, I use the following SQL commands to find out the features with the lowest and highest IDs. Of course, the lowest ID will probably always be
1 but the number of features (and hence the highest ID) is not known in my use-case and maybe you want to filter for something completely different anyway.
id = (SELECT min(id) FROM tableName)
max(id) instead of
GetMap request to GeoServer, saving the result in a variable for later use.
Then, insert this variable in your
GetMap request to GeoServer (e.g. in Leaflet, OpenLayers, ...) as follows (line breaks are inserted for the sake of readability):
&bbox=yourBoundingBox(normally determined by Leaflet/OpenLayers)
The relevant part of the request is the last line. Make sure to insert the results of your SQL query here, e.g. via variable substitution, string formatting or whatever the preferred way of your language may be.
This will return a png image from GeoServer with your style applied as defined above, dynamically styling the features differently that you passed via the variables in the URL to the GetMap request.