0

I am trying to create a generic style using CSS, so that I can have a set of styles which can be applied to polygons, lines and/or points. I am able to create the style:

  1. Add a new style...
  2. Format=CSS
  3. Generate a default style: "Generic", Generate... But when I apply it to a layer it comes back with the error message:

"The requested Style can not be used with this layer. The style specifies an attribute named 'geometry', not found in the 'My_Layer_Name' layer."

The CSS style generated is:

/* @title raster */
[isCoverage()=true] {
    raster-channels: auto;
    raster-opacity: 1.0;
}

/* @title red polygon */
[dimension(geometry)=2] {
    stroke: #000000;
    stroke-width: 0.5;
    fill: #ff3300;
}

/* @title red line */
[dimension(geometry)=1] {
    stroke: #ff3300;
}

/* @title red point */
[dimension(geometry)=0] {
    mark: symbol(square);
    mark-size: 6px;
    :mark {
        fill: #ff3300;
    }
}

If I create a generic style based on SLD it works fine (as above, but setting Format=SLD), but CSS would be much easier to edit in order to create a whole bunch of new styles.

I am coming to the conclusion that CSS does not support being able to select between polygons, lines and points.

Geoserver version:2.20.2, running on Windows 10

1 Answer 1

2

No guarantees, but try to replace "geometry" with the empty string (the empty string indicates the default geometry, whatever its name is, while "geometry" means the actual attribute named "geometry", which is not there in your layer), e.g.:

[dimension("")=0] { ... }

Otherwise, if you know which layer you're targeting, just use the actual name of the geometry property.

Looking at the SLD equivalent, "geometry" is a function call, so a better match for it would be:

/* @title raster */
[isCoverage()=true] {
    raster-channels: auto;
    raster-opacity: 1.0;
}

/* @title red polygon */
[dimension(geometry())=2] {
    stroke: #000000;
    stroke-width: 0.5;
    fill: #ff3300;
}

/* @title red line */
[dimension(geometry())=1] {
    stroke: #ff3300;
}

/* @title red point */
[dimension(geometry())=0] {
    mark: symbol(square);
    mark-size: 6px;
    :mark {
        fill: #ff3300;
    }
}
3
  • Thanks Andrea. Unfortunately it comes back with: UndeclaredThrowableExceptionError at line 8. Encountered"\"" at line 1, column 11. Was expecting one of: <STRING_LITERAL> ... "true" ... "false" ... "point" ... "linestring" ... "polygon" ... "multipoint" ... "multilinestring" ... "multipolygon" ... "geometrycollection" ... "envelope" ... "srid" ... "(" ... ")" ... "[" ... "-" ... <INTEGER_LITERAL> ... ... <IDENTIFIER> ... Parsing : dimension("")=2. And repeats for subsequent filters.
    – NoelB
    Commented Feb 8, 2022 at 18:19
  • I have tried changing it to [geometry="polygon"] {... But this comes back with the error "The style specifies an attribute named 'polygon', not found in ... Surely "polygon" is the VALUE and "geometry" is the ATTRIBUTE?
    – NoelB
    Commented Feb 8, 2022 at 18:25
  • 1
    No, you need to use the attribute name, for the geometry, in your layer. E.g., if you are using shapefiles it would be "the_geom". Go to the layer definition and see what the actual name is. Commented Feb 9, 2022 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.