HOW to Use env() Parameters in GeoServer for Dynamic Labels
A Complete Guide for WMS + OpenLayers Users
1. Introduction
In modern Web GIS applications, users often need dynamic control over map labels—for example:
-
Switching label fields (name, population, category)
-
Changing font size or color from a UI
-
Improving readability using halos
-
Adjusting labels without creating multiple styles
GeoServer provides a powerful but safe mechanism for this using env() parameters in Styled Layer Descriptors (SLDs).
This article explains what env() is, why it exists, and how to use it step by step for dynamic labeling in WMS services.
2. What Are env() Parameters?
env() parameters are request-time variables that GeoServer allows you to pass into an SLD via a WMS request.
They act as controlled, named placeholders, not arbitrary URL parameters.
Key characteristics:
-
Evaluated at render time
-
Passed via
&env= -
Secure and cache-aware
-
Supported only where GeoServer explicitly allows them (e.g., labels, colors, sizes)
3. Why GeoServer Uses env() Instead of Arbitrary Parameters
GeoServer intentionally blocks arbitrary WMS parameters for styling because:
-
Arbitrary parameters break tile caching
-
They can introduce SQL or expression injection
-
They make styles unpredictable
env() is GeoServer’s safe compromise:
-
Flexible
-
Explicit
-
Controlled
4. When Should You Use env()?
Use env() when you need:
-
Dynamic label fields
-
UI-driven label changes
-
One reusable style instead of many
-
Server-side rendering (WMS)
Do not use env() if:
-
You need unlimited logic → use client-side (OpenLayers vector)
-
You need SQL-level changes → use PostGIS views
5. Basic Concept: How env() Works
General syntax in SLD:
WMS request:
If the parameter is not provided, GeoServer uses the default value.
6. Step-by-Step: Creating a Dynamic Label Style
Step 1: Create a New Style in GeoServer
-
Go to GeoServer → Styles
-
Click Add a new style
-
Name it:
dynamic_label -
Choose SLD
Step 2: Full Working SLD for Dynamic Labels
Example: Point Layer Labeling
✔ Save
✔ Validate
✔ Assign to your layer7. Passing env() Parameters in WMS Requests
Single parameter
Multiple parameters
Rules:
-
Use semicolon (
;) as separator -
No spaces
-
Case-sensitive parameter names
8. Using env() with OpenLayers
OpenLayers WMS Source
Dynamically update labels
9. Scale-Dependent Dynamic Labeling
Add inside <Rule>:
This allows labels to:
-
Appear only at certain zoom levels
-
Reduce clutter
-
Improve performance
10. What Can Be Controlled with env()?
| Property | Supported |
|---|---|
| Label field | ✅ |
| Font size | ✅ |
| Font color | ✅ |
| Halo size | ✅ |
| Halo color | ✅ |
| Rotation | ✅ |
| Visibility | ✅ |
11. Limitations of env()
❌ Cannot inject SQL
❌ Cannot execute arbitrary functions
❌ Cannot override geometry
❌ Must be predefined in SLD
This is by design.
12. Performance & Caching Considerations
-
env()parameters are tile-cache aware -
Different
envvalues = different tiles -
Use reasonable defaults
-
Avoid excessive variations
13. Best Practices
✔ Use one reusable SLD
✔ Always define default values
✔ Validate attribute names from UI
✔ Combine with scale rules
✔ Use halos for readability
14. Common Use Cases
-
Administrative boundary labels
-
City labels (name ↔ population)
-
Asset IDs vs asset names
-
Thematic maps with UI controls
-
Professional GIS portals
15. Conclusion
The env() function is the most powerful and safe way to achieve dynamic labeling in GeoServer WMS.
It provides:
-
Flexibility
-
Security
-
Performance
-
Clean architecture
For GeoServer + OpenLayers applications, env() Chould be your first choice for server-side dynamic styling.
.png)
Comments
Post a Comment