use of org.opengis.style.LabelPlacement in project geotoolkit by Geomatys.
the class TextSymbolizerTest method pointLabelTest.
/**
* Render a label at check it is correctly located in the image.
*/
@Test
public void pointLabelTest() throws Exception {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("geom").setCRS(CommonCRS.defaultGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
final Feature feature = type.newInstance();
feature.setPropertyValue("geom", GF.createPoint(new Coordinate(0, 0)));
final FeatureSet collection = new InMemoryFeatureSet(type, Arrays.asList(feature));
// text symbolizer style
final String name = "mySymbol";
final Description desc = DEFAULT_DESCRIPTION;
// use the default geometry of the feature
final String geometry = null;
final Unit unit = Units.POINT;
final Expression label = FF.literal("LABEL");
final Font font = SF.font(FF.literal("Arial"), FONT_STYLE_ITALIC, FONT_WEIGHT_BOLD, FF.literal(14));
final LabelPlacement placement = SF.pointPlacement();
final Halo halo = SF.halo(Color.WHITE, 0);
final Fill fill = SF.fill(Color.BLUE);
final TextSymbolizer symbol = SF.textSymbolizer(name, geometry, desc, unit, label, font, placement, halo, fill);
final MutableStyle style = SF.style(symbol);
final MapLayer layer = MapBuilder.createLayer(collection);
layer.setStyle(style);
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(layer);
final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.defaultGeographic());
env.setRange(0, -180, +180);
env.setRange(1, -90, +90);
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_COLOR_MODEL, ColorModel.getRGBdefault());
final SceneDef scenedef = new SceneDef(context, hints);
final CanvasDef canvasdef = new CanvasDef(new Dimension(360, 180), env);
canvasdef.setBackground(Color.WHITE);
final BufferedImage buffer = DefaultPortrayalService.portray(canvasdef, scenedef);
// ImageIO.write(buffer, "PNG", new File("test.png"));
// we expect to have a blue label at the center of the image
final int[] pixel = new int[4];
final int[] blue = new int[] { 0, 0, 255, 255 };
final Raster raster = buffer.getData();
boolean found = false;
for (int x = 160; x < 200; x++) {
// should be exactly at the center
raster.getPixel(x, 90, pixel);
if (Arrays.equals(blue, pixel)) {
found = true;
}
}
assertTrue("label not found", found);
}
use of org.opengis.style.LabelPlacement in project geotoolkit by Geomatys.
the class PrepareStyleVisitor method visit.
@Override
public Object visit(TextSymbolizer ts, Object o) {
Fill fill = ts.getFill();
Font font = ts.getFont();
Halo halo = ts.getHalo();
Expression label = ts.getLabel();
LabelPlacement place = ts.getLabelPlacement();
if (fill != null) {
fill = (Fill) fill.accept(this, o);
}
if (font != null) {
font = (Font) font.accept(this, o);
}
if (halo != null) {
halo = (Halo) halo.accept(this, o);
}
if (label != null) {
label = (Expression) visit(label);
}
if (place != null) {
place = (LabelPlacement) place.accept(this, o);
}
// recreate symbolizer
return SF.textSymbolizer(ts.getName(), visitGeometryExpression(ts, o), ts.getDescription(), ts.getUnitOfMeasure(), label, font, place, halo, fill);
}
use of org.opengis.style.LabelPlacement in project geotoolkit by Geomatys.
the class Styles method centeredText.
public static MutableStyle centeredText() {
// general informations
final String name = "mySymbol";
final Description desc = DEFAULT_DESCRIPTION;
// use the default geometry of the feature
final String geometry = null;
final Unit unit = Units.POINT;
final Expression label = FF.property("CNTRY_NAME");
final Font font = SF.font(FF.literal("Arial"), FONT_STYLE_ITALIC, FONT_WEIGHT_BOLD, FF.literal(14));
final LabelPlacement placement = SF.pointPlacement();
final Halo halo = SF.halo(Color.WHITE, 1);
final Fill fill = SF.fill(Color.BLUE);
final TextSymbolizer symbol = SF.textSymbolizer(name, geometry, desc, unit, label, font, placement, halo, fill);
final MutableStyle style = SF.style(DEFAULT_POLYGON_SYMBOLIZER, symbol);
return style;
}
use of org.opengis.style.LabelPlacement in project geotoolkit by Geomatys.
the class SE100toGTTransformer method visit.
/**
* Transform a SLD v1.0 text symbolizer in GT text symbolizer.
*/
public TextSymbolizer visit(final org.geotoolkit.sld.xml.v100.TextSymbolizer tst) {
if (tst == null)
return null;
final Expression label = visitExpression(tst.getLabel());
final Font font = (tst.getFont() == null) ? styleFactory.font() : visit(tst.getFont());
final LabelPlacement placement = (tst.getLabelPlacement() == null) ? styleFactory.pointPlacement() : visit(tst.getLabelPlacement());
final Halo halo = visit(tst.getHalo());
final Fill fill = visit(tst.getFill());
final Unit uom = Units.POINT;
final String geom = visitGeom(tst.getGeometry());
final String name = null;
final Description desc = StyleConstants.DEFAULT_DESCRIPTION;
if (label == null)
return null;
return styleFactory.textSymbolizer(name, geom, desc, uom, label, font, placement, halo, fill);
}
use of org.opengis.style.LabelPlacement in project geotoolkit by Geomatys.
the class DefaultStyleVisitor method visit.
@Override
public Object visit(final TextSymbolizer textSymbolizer, Object data) {
final Fill fill = textSymbolizer.getFill();
if (fill != null) {
data = fill.accept(this, data);
}
final Font font = textSymbolizer.getFont();
if (font != null) {
data = font.accept(this, data);
}
final Halo halo = textSymbolizer.getHalo();
if (halo != null) {
data = halo.accept(this, data);
}
final Expression label = textSymbolizer.getLabel();
if (label != null) {
visit(label, data);
}
final LabelPlacement place = textSymbolizer.getLabelPlacement();
if (place != null) {
data = place.accept(this, data);
}
return data;
}
Aggregations