[GIS] Dynamically style geoserver layer with timestamp column –> age of entry

dateenvironment variablegeoserversld

I have a a postgis table with point geometries and a timestamp column, which has the information when the point was created. This table is already loaded as a layer into geoserver.

So now in Geoserver, I would like to style this layer via SLD, so that the color of the point changes by its age, i.e. the difference between the point's timestamp and the server time.

Lets say red for older than 2 years, yellow for older than 1 year, green for younger than 1 year.

Is there an approach via sld , maybe by using an environment varialble that can solve this requirement?

Best Answer

The easiest way is to add a filter comparing two dates:

   <sld:Rule>
      <ogc:Filter>
        <ogc:PropertyIsGreaterThanOrEqualTo>
          <ogc:PropertyName>date</ogc:PropertyName>
          <ogc:Literal>2012-01-01</ogc:Literal>
        </ogc:PropertyIsGreaterThanOrEqualTo>
      </ogc:Filter>
     ....
   </sld:Rule>

If you want to write one SLD and have it work for ever it's a bit trickier and you need to look at converting your dates into periods, which probably involves the function dateParse and some Maths functions like <ogc:Sub> to handle a year ago. Then you will need an environment variable (or custom function) to get the current date.

Related Question