MassGIS, the Commonwealth’s Office of Geographic and Environmental Information, is the official Massachusetts state agency assigned to the collection, storage and dissemination of geographic data. Since 2006, MassGIS has effectively provided much of its geospatial data via OGC standards using GeoServer with support from Boundless. They are one of our oldest clients, so we figured this issue would be a great way to launch a new recurring feature on our blog that highlights real questions, their solutions, and the value of our support.
Not long ago, they asked an interesting question about how to properly label features, specifically multipoint geometries. Our solution involved using Web Processing Service to find and label the centroids using rendering transformations in SLD.
Features in GeoServer will often be a single point, linestring, or polygon but they can also be a more complex multipoint, multistring, or multipolygon. Representing a country or another administrative area as a polygon may make sense if it is one contiguous landmass, but most countries are not: they have islands, are split by bodies of water, or might even have an exclave. Thus, it is common to use a single multipolygon geometry to represent all the parts that make up a single administrative area.
GeoServer employs an algorithm to determine where to place a label on the multipolygon: it will place the label at the centre of the largest polygon in the visible area. You can see this algorithm in action on a default installation of OpenGeo Suite.
Depending on which of Indonesia’s many islands are visible, the label will “jump” around as we zoom and pan so that it remains on the largest visible landmass.
It is relatively easy to find the most “important” polygon, since polygons have areas that we can compare. Points, however, prove more challenging as GeoServer has no metric to determine which is the “largest”. Therefore, when we assign a label to a multipoint, GeoServer will simply attach the label to the first visible point in the set.
alpha = MULTIPOINT(0 10,10 10,10 0,0 0)
beta = MULTIPOINT(25 10,30 0,20 0)
If we have the two multipoints above, GeoServer will label them as follows:
You can download multipoint.zip to try the same layer on GeoServer.
This default label placement may be desirable in certain circumstances, but MassGIS was interested in a solution for placing the label in a more natural position: the geographic centre of all the points in each feature.
Finding the centroid with Web Processing Service
We have previously explored using WPS, but it’s always interesting to find new applications for server-side processing. OpenGeo Suite ships with a WPS extension for GeoServer that comes with
gs:Centroid, a handy process that will calculate the centroid of a geometry. Using the WPS request builder in GeoServer, we can quickly check that it works:
When executing this process, we should get back the following GeoJSON, which shows the two centroids with the same attributes as the multipoints they were derived from:
We now know we can easily get the centroid for our two features using WPS, but what is great to remember is that we can call these processes from an SLD. We’ll use this trick to style our multipoint features with a label at the positions calculated above.
Using Rendering Transforms
gs:Centroid from our SLD, we simply need to add the following XML block:
This will call
gs:Centroid with the input parameter named
features (we can see the name of the parameter using the WPS request builder). The transformed features, which will each be a single point, can now be styled with a label as normal.
This is almost enough, but we want both the original multipoint feature as well as the transformed centroid to be drawn. We can accomplish this by providing two
<FeatureTypeStyle> blocks in our SLD.
The final product is this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Title>Multipoint with labelled centroid</Title>
<Abstract>A 6 pixel red square</Abstract>
This is just one example of using a WPS in an SLD. OpenGeo Suite ships with a large number of similar processes which could be used to style our output. In an upcoming post from the support team, we will look at an even more powerful way of controlling the rendering process using GeoScript.
Benjamin Trigona-Harany leads our global support team from our offices in Victoria, BC. Interested in support or training for your enterprise? Contact us to learn more.