private void styleBuilderExample() throws Exception {
// styleBuilderExample start
//
// We are using the GeoTools StyleBuilder that is helpful for quickly making things
StyleBuilder builder = new StyleBuilder();
FilterFactory2 ff = builder.getFilterFactory();
// RULE 1
// first rule to draw cities
// define a point symbolizer representing a city
Graphic city = builder.createGraphic();
city.setSize(ff.literal(10));
city.graphicalSymbols().add(builder.createExternalGraphic("file:city.svg", "svg")); // svg
// preferred
city.graphicalSymbols().add(builder.createExternalGraphic("file:city.png", "png")); // png next
city.graphicalSymbols().add(
builder.createMark(StyleBuilder.MARK_CIRCLE, Color.BLUE, Color.BLACK, 1));
PointSymbolizer pointSymbolizer = builder.createPointSymbolizer(city, "the_geom");
Rule rule1 = builder.createRule(pointSymbolizer);
rule1.setName("rule1");
rule1.getDescription().setTitle("City");
rule1.getDescription().setAbstract("Rule for drawing cities");
rule1.setFilter(ff.less(ff.property("POPULATION"), ff.literal(50000)));
//
// RULE 2 Default
Graphic dotGraphic = builder.createGraphic(null, builder.createMark(StyleBuilder.MARK_CIRCLE),
null);
PointSymbolizer dotSymbolize = builder.createPointSymbolizer(dotGraphic);
Rule rule2 = builder.createRule(dotSymbolize);
rule2.setIsElseFilter(true);
//
// define feature type styles used to actually define how features are rendered
Rule rules[] = new Rule[] { rule1, rule2 };
FeatureTypeStyle featureTypeStyle = builder.createFeatureTypeStyle("Feature", rules);
//
// create a "user defined" style
Style style = builder.createStyle();
style.setName("style");
style.getDescription().setTitle("User Style");
style.getDescription().setAbstract("Definition of Style");
style.featureTypeStyles().add(featureTypeStyle);
// styleBuilderExample end