Hi,
two years ago I started writing a fluent style builder to replace
the existing, old and clunky StyleBuilder in main.
The attempt sucked up too much time and I ended up abandoning
it, Jody tried to salvage the effort and put the classes in the brewer
module, but the work was not brought into any usable state either.

During the last three weekends I dusted it off and built all of the
SLD cookbook examples to kick its tires and see if it actually works.

The basic ideas are:
- there is  tree of builders, one for each SLD object
- each builder tries to be fluent, that is, it returns itself
  on most calls
- you can build a style instantiating directly a very nested object
  builder, if all you want is a solid color style you'll create a
  FillBuilder directly for example
- at any stage in the nested chain of builder you can call buildStyle
  or buildSLD to get a complete tree, without having to keep around
  the master builder
- css parameters can be provided normally in three ways, the common
  literal, an expression object, or a cql/ecql expression
- when going to a sub-builder, there is no going back, so if you
  need that you have to store the current builder in a variable.
  I played with the notion of being able to go back, but as soon
  as the style gets minimally complex good luck remembering
  where you are after calling parent().parent().parent().
  The approach where you save the variable is actually a lot
  more readable and still decently compact
- I resisted the temptation of adding shortcuts for common paths,
  as "common" is quite relative. The objective was to get something
  maintainable and usable at the same time


Here are some examples with links to the equivalent SLD
in the cookbook:

- http://docs.geoserver.org/latest/en/user/_downloads/polygon_simplepolygon.sld
Style style = new FillBuilder().color(Color.BLUE).buildStyle();

- 
http://docs.geoserver.org/latest/en/user/_downloads/polygon_simplepolygonwithstroke.sld
PolygonSymbolizerBuilder psb = new PolygonSymbolizerBuilder();
psb.fill().color(Color.BLUE);
Style style = psb.stroke().color(Color.WHITE).width(2).buildStyle()


- 
http://docs.geoserver.org/latest/en/user/_downloads/polygon_polygonwithstyledlabel.sld
RuleBuilder rb = new RuleBuilder();
PolygonSymbolizerBuilder pb = rb.polygon();
pb.fill().colorHex("#40FF40");
pb.stroke().colorHex("#FFFFFF").width(2);
TextSymbolizerBuilder tb = rb.text().label("name");
tb.pointPlacement().anchor().x(0.5).y(0.5);
tb.newFont().familyName("Arial").size(11).styleName("normal").weightName("bold");
tb.fill().color(Color.BLACK);
tb.halo().radius(3).fill().color(Color.WHITE);
tb.option("autoWrap", 60).option("maxDisplacement", 150);
Style style = rb.buildStyle();

- 
http://docs.geoserver.org/latest/en/user/_downloads/line_attributebasedline.sld
FeatureTypeStyleBuilder fts = new FeatureTypeStyleBuilder();
fts.rule().filter("type =
'local-road'").line().stroke().colorHex("#009933").width(2);
fts.rule().filter("type =
'secondary'").line().stroke().colorHex("#0055CC").width(3);
fts.rule().filter("type =
'highway'").line().stroke().colorHex("#FF0000").width(6);
Style style = fts.buildStyle();

- 
http://docs.geoserver.org/latest/en/user/_downloads/raster_threecolorgradient.sld
ColorMapBuilder cm = new ColorMapBuilder();
cm.entry().quantity(150).colorHex("#0000FF");
cm.entry().quantity(200).colorHex("#FFFF00");
cm.entry().quantity(250).colorHex("#FF0000");
Style style = cm.buildStyle();

Imho these are good enough to make style buildig by code more productive
than writing it in XML, and thus good enough for examples and the like.
Full cookbook examples with tests checking the result is good are here:
http://svn.osgeo.org/geotools/trunk/modules/extension/brewer/src/test/java/org/geotools/styling/builder/CookbookLineTest.java
http://svn.osgeo.org/geotools/trunk/modules/extension/brewer/src/test/java/org/geotools/styling/builder/CookbookPointTest.java
http://svn.osgeo.org/geotools/trunk/modules/extension/brewer/src/test/java/org/geotools/styling/builder/CookbookPolygonTest.java
http://svn.osgeo.org/geotools/trunk/modules/extension/brewer/src/test/java/org/geotools/styling/builder/CookbookRasterTest.java

The code coverage is also good, the examples bring it to 71% in the builder
package.

Next steps:
- I'd like to hear comments on the above, see anything that needs fixing?
- as last time, I'm kind of the last possible user for this code, my work in
  GS brings me to write styles in XML mostly, though I'll certainly consider
  the builder if I have to write complex styles or there is any automation
  that can be done while building them.
  Is there anybody interested in picking it up?
- where should we put this code? The brewer module does not sound like
  the best place. What about a new community module?
  Or people like it enough, along with the good coverage, that we put it
  directly in main and deprecate the old builder?

Cheers
Andrea

-- 
-------------------------------------------------------
Ing. Andrea Aime
GeoSolutions S.A.S.
Tech lead

Via Poggio alle Viti 1187
55054  Massarosa (LU)
Italy

phone: +39 0584 962313
fax:      +39 0584 962313

http://www.geo-solutions.it
http://geo-solutions.blogspot.com/
http://www.youtube.com/user/GeoSolutionsIT
http://www.linkedin.com/in/andreaaime
http://twitter.com/geowolf

-------------------------------------------------------

------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system, 
user administration capabilities and model configuration. Take 
the hassle out of deploying and managing Subversion and the 
tools developers use with it. 
http://p.sf.net/sfu/wandisco-dev2dev
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to