Search in sources :

Example 1 with LabelPlacement

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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Fill(org.opengis.style.Fill) Description(org.opengis.style.Description) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) MapLayer(org.apache.sis.portrayal.MapLayer) Unit(javax.measure.Unit) Feature(org.opengis.feature.Feature) Font(org.opengis.style.Font) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) SceneDef(org.geotoolkit.display2d.service.SceneDef) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Raster(java.awt.image.Raster) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) Point(org.locationtech.jts.geom.Point) LabelPlacement(org.opengis.style.LabelPlacement) Coordinate(org.locationtech.jts.geom.Coordinate) Expression(org.opengis.filter.Expression) TextSymbolizer(org.opengis.style.TextSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) Halo(org.opengis.style.Halo) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 2 with LabelPlacement

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);
}
Also used : GraphicFill(org.opengis.style.GraphicFill) Fill(org.opengis.style.Fill) LabelPlacement(org.opengis.style.LabelPlacement) Expression(org.opengis.filter.Expression) Font(org.opengis.style.Font) Halo(org.opengis.style.Halo)

Example 3 with LabelPlacement

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;
}
Also used : Fill(org.opengis.style.Fill) GraphicFill(org.opengis.style.GraphicFill) Description(org.opengis.style.Description) LabelPlacement(org.opengis.style.LabelPlacement) MutableStyle(org.geotoolkit.style.MutableStyle) Expression(org.opengis.filter.Expression) TextSymbolizer(org.opengis.style.TextSymbolizer) Unit(javax.measure.Unit) Font(org.opengis.style.Font) Halo(org.opengis.style.Halo)

Example 4 with LabelPlacement

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);
}
Also used : Fill(org.opengis.style.Fill) GraphicFill(org.opengis.style.GraphicFill) Description(org.opengis.style.Description) LabelPlacement(org.opengis.style.LabelPlacement) Expression(org.opengis.filter.Expression) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) InternationalString(org.opengis.util.InternationalString) Unit(javax.measure.Unit) Font(org.opengis.style.Font) Halo(org.opengis.style.Halo)

Example 5 with LabelPlacement

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;
}
Also used : GraphicFill(org.opengis.style.GraphicFill) Fill(org.opengis.style.Fill) LabelPlacement(org.opengis.style.LabelPlacement) Expression(org.opengis.filter.Expression) Font(org.opengis.style.Font) Halo(org.opengis.style.Halo)

Aggregations

Expression (org.opengis.filter.Expression)9 Fill (org.opengis.style.Fill)9 Font (org.opengis.style.Font)9 Halo (org.opengis.style.Halo)9 LabelPlacement (org.opengis.style.LabelPlacement)9 GraphicFill (org.opengis.style.GraphicFill)7 Unit (javax.measure.Unit)6 Description (org.opengis.style.Description)6 TextSymbolizer (org.opengis.style.TextSymbolizer)4 MutableStyle (org.geotoolkit.style.MutableStyle)3 Dimension (java.awt.Dimension)1 BufferedImage (java.awt.image.BufferedImage)1 Raster (java.awt.image.Raster)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JAXBException (javax.xml.bind.JAXBException)1 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 MapLayer (org.apache.sis.portrayal.MapLayer)1 MapLayers (org.apache.sis.portrayal.MapLayers)1