[Geoserver-users] CSS (or SLD) styling: create label-geometry from 2 layer attributes

Hi all

I am trying to use 2 numeric attributes called label_lat and label_lng to create a point geometry for the labeling of a polygon.

Can anyone point me to the correct format, if this is indeed possible?

I cannot find the information on https://docs.geoserver.org/latest/en/user/styling/css/index.html

I have tried:

label-geometry: [label_lng], [label_lat];

label-geometry: [(label_lng, label_lat)];
etc

and the reverse coordinate orders

label-geometry: [centroid(geom)]; does work as expected, but I need more control.

I also cannot create a new column in the GeoServer SQL view that is visible in the output e.g. ST_Point (label_lng, label_lat, 4326) as label-geom

i.e. if I create the new column, it must not be visible in the output.

Thanks

Peter

There should be a function to create a point from lat, Lon but I do not see it in the function list. It is a little to do anything other than transform the main geometry - since often the data is queried using a BBOX against the main geometry.

For greater control I would recommend use the centroid as a staring location, and provide an offset to determine the label position. The offset can be expressed as an expression which you can adjust for scale.

Reference:

···


Jody Garnett

I usually use geomFromWKT(geometry) with a string like ‘POINT(’+x+’ ‘+y+’)’ as the parameter. But a nicer function would be useful.

There is also a pole of accessibility function that is better than centroid if you have oddly shaped polygons.

Ian

···


Jody Garnett

Ian Turton

Thank you very much for your answers. All 3 of them are super useful.

Ian, I do not have the luxury of building up the string in a single attribute, so I need to use the Concatenate or strConcat functions from Jody’s reference. Andrea, the WKT X and Y coordinates are not separated by a comma but rather by a space.

The strConcat function only joins 2 strings, the Concatenate function is able to join many strings.

So something like this should work:

label-geometry: [geomFromWKT(Concatenate(‘POINT(’, label_lng, ’ ', label_lat, ‘)’))];
or
label-geometry: [geomFromWKT(strConcat(strConcat(strConcat(strConcat(‘POINT(’, label_lng), ’ '), label_lat), ‘)’))];

However, the SLD created contains (for the space separator between X and Y):

ogc:Literal </ogc:Literal>

and it appears from my testing and debugging that this is evaluated as “” i.e. an empty string.

(I get the exception: bad wkt when evaluating it, and I have confirmed the above in Eclipse)

Trying to force a &nbsp; into the Literal does not work.

Is there a bug with parsing a literal space?

So, I am very close to my solution, I just need a way to represent a literal space, if anyone can assist, please?

Thanks

Peter

···


Jody Garnett

Ian Turton

White space gets reduced in the XML, so you might need a cdata block around it. Not sure how CSS handles it.

Ian

···


Jody Garnett

Ian Turton

Thanks Ian, you’re 100% right. I was able to manually edit the SLD produced by the CSS translator to:

ogc:Literal</ogc:Literal>

and then my label-geometry worked as expected.

I believe this might be a bug in the CSS extension, which I have logged in Jira: https://osgeo-org.atlassian.net/browse/GEOT-7486

Thank you Andrea, Jody and Ian for your help.

Peter

···


Jody Garnett

Ian Turton