use of org.opengis.style.TextSymbolizer in project geotoolkit by Geomatys.
the class KmzContextInterpreter method writeSymbolizer.
/**
* Writes KML color styles mapping SLD Symbolizers.
* Color styles are written into KML Style selector.
*/
private AbstractStyleSelector writeSymbolizer(Symbolizer symbolizer, Style styleSelector) {
if (symbolizer instanceof ExtensionSymbolizer) {
} else // LineSymbolizer mapping
if (symbolizer instanceof LineSymbolizer) {
final LineSymbolizer lineSymbolizer = (LineSymbolizer) symbolizer;
final LineStyle lineStyle = ((styleSelector.getLineStyle() == null) ? KML_FACTORY.createLineStyle() : styleSelector.getLineStyle());
lineStyle.setWidth((Double) this.writeExpression(lineSymbolizer.getStroke().getWidth(), Double.class, null));
lineStyle.setColor((Color) this.writeExpression(lineSymbolizer.getStroke().getColor(), Color.class, null));
styleSelector.setLineStyle(lineStyle);
} else // PointSymbolizezr mapping
if (symbolizer instanceof PointSymbolizer) {
// PointSymbolizer pointSymbolizer = (PointSymbolizer) symbolizer;
// IconStyle iconStyle = KML_FACTORY.createIconStyle();
// GraphicalSymbol gs = ((GraphicalSymbol) pointSymbolizer.getGraphic().graphicalSymbols().get(0));
// gs.
} else // PolygonSymbolizer mapping
if (symbolizer instanceof PolygonSymbolizer) {
final PolygonSymbolizer polygonSymbolizer = (PolygonSymbolizer) symbolizer;
final PolyStyle polyStyle = KML_FACTORY.createPolyStyle();
// Fill
if (polygonSymbolizer.getFill() == null) {
polyStyle.setFill(false);
} else {
polyStyle.setFill(true);
polyStyle.setColor((Color) this.writeExpression(polygonSymbolizer.getFill().getColor(), Color.class, null));
}
// Outline
if (polygonSymbolizer.getStroke() == null) {
polyStyle.setOutline(false);
} else if (styleSelector.getLineStyle() == null) {
polyStyle.setOutline(true);
final LineStyle lineStyle = KML_FACTORY.createLineStyle();
lineStyle.setColor((Color) this.writeExpression(polygonSymbolizer.getStroke().getColor(), Color.class, null));
lineStyle.setWidth((Double) this.writeExpression(polygonSymbolizer.getStroke().getWidth(), Double.class, null));
styleSelector.setLineStyle(lineStyle);
}
styleSelector.setPolyStyle(polyStyle);
} else if (symbolizer instanceof RasterSymbolizer) {
} else if (symbolizer instanceof TextSymbolizer) {
final TextSymbolizer textSymbolizer = (TextSymbolizer) symbolizer;
final LabelStyle labelStyle = KML_FACTORY.createLabelStyle();
if (textSymbolizer.getFont() != null) {
textSymbolizer.getFont().getSize();
}
if (textSymbolizer.getFill() != null) {
labelStyle.setColor((Color) this.writeExpression(textSymbolizer.getFill().getColor(), Color.class, null));
}
styleSelector.setLabelStyle(labelStyle);
}
return styleSelector;
}
use of org.opengis.style.TextSymbolizer in project geotoolkit by Geomatys.
the class KmzContextInterpreter method writeFeature.
/**
* Transforms a feature into KML feature (Placemak if original
* features contents a geometry, or Folder otherwise).
*/
private Feature writeFeature(final Feature feature) {
Feature kmlFeature = null;
for (final PropertyType type : feature.getType().getProperties(true)) {
final Object val = feature.getPropertyValue(type.getName().toString());
if (val instanceof Feature) {
kmlFeature = KML_FACTORY.createFolder();
kmlFeature.setPropertyValue(KmlConstants.TAG_FEATURES, writeFeature((Feature) val));
} else if (val instanceof Geometry) {
kmlFeature = KML_FACTORY.createPlacemark();
kmlFeature.setPropertyValue(KmlConstants.TAG_GEOMETRY, val);
} else {
// System.out.println("PAS FEATURE.");
}
}
// Search feature style URI
for (Entry<Rule, URI> e : IDENTIFICATORS_MAP) {
final Rule rule = e.getKey();
if (rule.getFilter().test(feature)) {
kmlFeature.setPropertyValue(KmlConstants.TAG_STYLE_URL, e.getValue());
for (Symbolizer s : rule.symbolizers()) {
if (s instanceof TextSymbolizer) {
final Expression label = ((TextSymbolizer) s).getLabel();
if (label != null) {
kmlFeature.setPropertyValue(KmlConstants.TAG_NAME, writeExpression(label, String.class, feature));
}
}
}
break;
}
}
return kmlFeature;
}
use of org.opengis.style.TextSymbolizer 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.TextSymbolizer in project geotoolkit by Geomatys.
the class SEforSLD100Test method testTextSymbolizer.
@Test
public void testTextSymbolizer() throws JAXBException {
final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
final Marshaller MARSHALLER = POOL.acquireMarshaller();
// Read test
Object obj = UNMARSHALLER.unmarshal(FILE_SE_SYMBOL_TEXT);
assertNotNull(obj);
JAXBElement<org.geotoolkit.sld.xml.v100.TextSymbolizer> jax = (JAXBElement<org.geotoolkit.sld.xml.v100.TextSymbolizer>) obj;
TextSymbolizer textSymbol = TRANSFORMER_GT.visit(jax.getValue());
assertNotNull(textSymbol);
assertEquals(textSymbol.getGeometryPropertyName(), valueGeom);
assertEquals(Units.POINT, textSymbol.getUnitOfMeasure());
assertNotNull(textSymbol.getFill());
assertEquals(floatValue(textSymbol.getFill().getOpacity()), 1.0f, DELTA);
assertEquals(stringValue(textSymbol.getFill().getColor()), "#808080");
assertEquals(floatValue(textSymbol.getHalo().getRadius()), 5f, DELTA);
assertEquals(floatValue(textSymbol.getHalo().getFill().getOpacity()), 0.52f, DELTA);
assertEquals(((ValueReference) textSymbol.getLabel()).getXPath(), "aField");
assertEquals(stringValue(textSymbol.getFont().getFamily().get(0)), "arial");
assertEquals(stringValue(textSymbol.getFont().getFamily().get(1)), "serif");
assertEquals(floatValue(textSymbol.getFont().getSize()), 17f, DELTA);
assertEquals(stringValue(textSymbol.getFont().getStyle()), "italic");
assertEquals(stringValue(textSymbol.getFont().getWeight()), "bold");
// Write test
JAXBElement<org.geotoolkit.sld.xml.v100.TextSymbolizer> pvt = TRANSFORMER_OGC.visit(textSymbol, null);
assertNotNull(pvt);
assertEquals(pvt.getValue().getGeometry().getPropertyName().getContent(), "");
assertNotNull(pvt.getValue().getFill());
MARSHALLER.marshal(pvt, TEST_FILE_SE_SYMBOL_TEXT);
POOL.recycle(MARSHALLER);
POOL.recycle(UNMARSHALLER);
}
use of org.opengis.style.TextSymbolizer in project geotoolkit by Geomatys.
the class SEforSLD110Test method testTextSymbolizer.
@Test
public void testTextSymbolizer() throws JAXBException {
final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
final Marshaller MARSHALLER = POOL.acquireMarshaller();
// Read test
Object obj = UNMARSHALLER.unmarshal(FILE_SE_SYMBOL_TEXT);
assertNotNull(obj);
JAXBElement<org.geotoolkit.se.xml.v110.TextSymbolizerType> jax = (JAXBElement<org.geotoolkit.se.xml.v110.TextSymbolizerType>) obj;
TextSymbolizer textSymbol = TRANSFORMER_GT.visit(jax.getValue());
assertNotNull(textSymbol);
assertEquals(textSymbol.getGeometryPropertyName(), valueGeom);
assertEquals(Units.FOOT, textSymbol.getUnitOfMeasure());
assertNotNull(textSymbol.getFill());
assertEquals(floatValue(textSymbol.getFill().getOpacity()), 1.0f, DELTA);
assertEquals(colorValue(textSymbol.getFill().getColor()), ObjectConverters.convert("#FFC800", Color.class));
assertEquals(floatValue(textSymbol.getHalo().getRadius()), 5f, DELTA);
assertEquals(floatValue(textSymbol.getHalo().getFill().getOpacity()), 0.52f, DELTA);
assertEquals(stringValue(textSymbol.getLabel()), "aField");
assertEquals(stringValue(textSymbol.getFont().getFamily().get(0)), "arial");
assertEquals(stringValue(textSymbol.getFont().getFamily().get(1)), "serif");
assertEquals(floatValue(textSymbol.getFont().getSize()), 17f, DELTA);
assertEquals(stringValue(textSymbol.getFont().getStyle()), "italic");
assertEquals(stringValue(textSymbol.getFont().getWeight()), "bold");
// Write test
JAXBElement<org.geotoolkit.se.xml.v110.TextSymbolizerType> pvt = TRANSFORMER_OGC.visit(textSymbol, null);
assertNotNull(pvt);
assertEquals(pvt.getValue().getGeometry(), null);
assertNotNull(pvt.getValue().getFill());
MARSHALLER.marshal(pvt, TEST_FILE_SE_SYMBOL_TEXT);
POOL.recycle(MARSHALLER);
POOL.recycle(UNMARSHALLER);
}
Aggregations