/** * create a textsymbolizer which doesn't change * * @param color the color of the text * @param font the font to use * @param label the label to use * @return the new textsymbolizer */ public TextSymbolizer createStaticTextSymbolizer(Color color, Font font, String label) { return createTextSymbolizer( createFill(color), new Font[] {font}, null, literalExpression(label), null, null); }
/** * create a textsymbolizer which doesn't change * * @param color the color of the text * @param fonts an array of fonts to use from the first to last * @param label the label to use * @return the new textsymbolizer */ public TextSymbolizer createStaticTextSymbolizer(Color color, Font[] fonts, String label) { return createTextSymbolizer( createFill(color), fonts, null, literalExpression(label), null, null); }
/** * create a textsymbolizer * * @param color the color of the text * @param fonts an array of fonts to use from the first to last * @param attributeName the attribute to use for the label * @return the new textsymbolizer * @throws org.geotools.filter.IllegalFilterException - if the attribute name does not exist */ public TextSymbolizer createTextSymbolizer(Color color, Font[] fonts, String attributeName) throws org.geotools.filter.IllegalFilterException { return createTextSymbolizer( createFill(color), fonts, null, attributeExpression(attributeName), null, null); }
/** * * <!-- begin-user-doc --> * <!-- end-user-doc --> * * @generated modifiable */ public Object parse(ElementInstance instance, Node node, Object value) throws Exception { Color color = (Color) value; TextSymbolizer textSymbolizer = sb.createTextSymbolizer(); textSymbolizer.setFill(sb.createFill(color)); return textSymbolizer; } }
/** * create a textsymbolizer * * @param color the color of the text * @param font the font to use * @param attributeName the attribute to use for the label * @return the new textsymbolizer * @throws org.geotools.filter.IllegalFilterException - if the attribute name does not exist */ public TextSymbolizer createTextSymbolizer(Color color, Font font, String attributeName) throws org.geotools.filter.IllegalFilterException { return createTextSymbolizer( createFill(color), new Font[] {font}, null, attributeExpression(attributeName), null, null); }
@Test public void testTextSymbolizerArraySingleValue() throws Exception { TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, (Font) null, "label"); ts.getOptions().put(TextSymbolizer.GRAPHIC_MARGIN_KEY, "10"); ts.accept(visitor); TextSymbolizer clone = (TextSymbolizer) visitor.getCopy(); assertEquals("20", clone.getOptions().get(TextSymbolizer.GRAPHIC_MARGIN_KEY)); }
@Test public void testTextSymbolizer() throws Exception { TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, (Font) null, "label"); ts.getOptions().put(TextSymbolizer.MAX_DISPLACEMENT_KEY, "10"); ts.getOptions().put(TextSymbolizer.GRAPHIC_MARGIN_KEY, "10 20"); ts.accept(visitor); TextSymbolizer clone = (TextSymbolizer) visitor.getCopy(); assertEquals("20", clone.getOptions().get(TextSymbolizer.MAX_DISPLACEMENT_KEY)); assertEquals("20 40", clone.getOptions().get(TextSymbolizer.GRAPHIC_MARGIN_KEY)); }
@Test public void testUsesCustomLabelPainter() throws Exception { LabelPainter painter = Mockito.mock(LabelPainter.class); Graphics2D graphics = Mockito.mock(Graphics2D.class); cache.setConstructPainter((x, y) -> painter); TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, (Font) null, "name"); SimpleFeature f1 = createFeature("label1", L1); cache.put( LAYER_ID, ts, f1, new LiteShape2((Geometry) f1.getDefaultGeometry(), null, null, false), ALL_SCALES); cache.endLayer(LAYER_ID, graphics, new Rectangle(0, 0, 256, 256)); cache.end(graphics, new Rectangle(0, 0, 256, 256)); Mockito.verify(painter).setLabel(Mockito.any(LabelCacheItem.class)); Mockito.verify(painter, Mockito.atLeastOnce()).getLabel(); }
@Test public void testDecimateSmallRing() throws Exception { Font font = sb.createFont("Bitstream Vera Sans", 12); TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, font, "name"); ts.getOptions().put(TextSymbolizer.FOLLOW_LINE_KEY, "true");
@Test public void testGroupDifferentSymbolizers() throws Exception { TextSymbolizer ts1 = sb.createTextSymbolizer(Color.BLACK, (Font) null, "name"); ts1.getOptions().put(TextSymbolizer.GROUP_KEY, "true"); TextSymbolizer ts2 = sb.createTextSymbolizer(Color.YELLOW, (Font) null, "name"); ts2.getOptions().put(TextSymbolizer.GROUP_KEY, "true"); SimpleFeature f1 = createFeature("label1", L1); SimpleFeature f2 = createFeature("label1", L2); cache.put( LAYER_ID, ts1, f1, new LiteShape2((Geometry) f1.getDefaultGeometry(), null, null, false), ALL_SCALES); cache.put( LAYER_ID, ts2, f2, new LiteShape2((Geometry) f2.getDefaultGeometry(), null, null, false), ALL_SCALES); // two different symbolizers, we should have two List<LabelCacheItem> labels = cache.getActiveLabels(); assertEquals(2, labels.size()); }
@Test public void testRendererListener() throws Exception { TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, (Font) null, "name"); AtomicReference<Exception> exception = new AtomicReference<Exception>(null); RenderListener listener = new RenderListener() { @Override public void featureRenderer(SimpleFeature feature) { // TODO Auto-generated method stub } @Override public void errorOccurred(Exception e) { exception.set(e); } }; cache.addRenderListener(listener); SimpleFeature f1 = createFeature("label1", L1); cache.put( LAYER_ID, ts, f1, new LiteShape2((Geometry) f1.getDefaultGeometry(), null, null, false), ALL_SCALES); cache.endLayer("layerId", null, null); TestGraphics testGraphics = new TestGraphics(); testGraphics.setRenderingHints(Collections.emptyMap()); cache.end(testGraphics, new Rectangle(0, 0, 10, 10)); // got here, did we get the exception assertNotNull(exception.get()); }
private static Style createLabelOffsetStyle() { StyleBuilder sb = new StyleBuilder(); PointPlacement pp = sb.createPointPlacement(0.5, 0.5, 50, 0, 0); TextSymbolizer ts = sb.createTextSymbolizer(); ts.setFont(sb.createFont("Bitstream Vera Sans", 20)); ts.setLabel(sb.getFilterFactory().literal("name")); ts.setLabelPlacement(pp); ts.getOptions().put("partials", "true"); return sb.createStyle(ts); }
@Test public void testSimpleGrouping() throws Exception { TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, (Font) null, "name"); ts.getOptions().put(TextSymbolizer.GROUP_KEY, "true"); SimpleFeature f1 = createFeature("label1", L1); SimpleFeature f2 = createFeature("label1", L2); cache.put( LAYER_ID, ts, f1, new LiteShape2((Geometry) f1.getDefaultGeometry(), null, null, false), ALL_SCALES); cache.put( LAYER_ID, ts, f2, new LiteShape2((Geometry) f2.getDefaultGeometry(), null, null, false), ALL_SCALES); // we have just one List<LabelCacheItem> labels = cache.getActiveLabels(); assertEquals(1, labels.size()); LabelCacheItem item = labels.get(0); assertEquals("label1", item.getLabel()); assertEquals(2, item.getGeoms().size()); }
@Test public void testNoUnit() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); LineSymbolizer ls = sb.createLineSymbolizer(original); ls.accept(visitor); Stroke clone = ((LineSymbolizer) visitor.getCopy()).getStroke(); assertEquals(4.0d, clone.getWidth().evaluate(null, Double.class), 0d); assertEquals(10.0f, clone.getDashArray()[0], 0f); assertEquals(20.0f, clone.getDashArray()[1], 0f); TextSymbolizer ts = sb.createTextSymbolizer(); ts.getOptions().put(TextSymbolizer.SPACE_AROUND_KEY, "10"); ts.accept(visitor); TextSymbolizer clonedTs = (TextSymbolizer) visitor.getCopy(); assertEquals("20.0", clonedTs.getOptions().get(TextSymbolizer.SPACE_AROUND_KEY)); }
@Test public void testTextDisplaced() { TextSymbolizer ts = sb.createTextSymbolizer(); ts.setFont(sb.createFont("Arial", 20)); PointPlacement pp = sb.createPointPlacement(1, 1, 10, 10, 0); ts.setLabelPlacement(pp); Style style = sb.createStyle(ts); MetaBufferEstimator rbe = new MetaBufferEstimator(); rbe.visit(style); // 20 (font) + 20 * 0.5 (anchor) + 10 (offset) assertEquals(40, rbe.getBuffer()); assertTrue(rbe.isEstimateAccurate()); }
@Test public void testTextNoStroke() { Style style = sb.createStyle(sb.createTextSymbolizer()); MetaBufferEstimator rbe = new MetaBufferEstimator(); assertEquals(0, rbe.getBuffer()); assertTrue(rbe.isEstimateAccurate()); rbe.visit(style); assertEquals(15, rbe.getBuffer()); assertTrue(rbe.isEstimateAccurate()); }
@Test public void testAllMeters() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); LineSymbolizer ls = sb.createLineSymbolizer(original); ls.setUnitOfMeasure(SI.METRE); ls.accept(visitor); Stroke clone = ((LineSymbolizer) visitor.getCopy()).getStroke(); assertEquals(2d, clone.getWidth().evaluate(null, Double.class), 0d); assertEquals(5f, clone.getDashArray()[0], 0f); assertEquals(10f, clone.getDashArray()[1], 0f); TextSymbolizer ts = sb.createTextSymbolizer(); ts.getOptions().put(TextSymbolizer.SPACE_AROUND_KEY, "10"); ts.setUnitOfMeasure(SI.METRE); ts.accept(visitor); TextSymbolizer clonedTs = (TextSymbolizer) visitor.getCopy(); assertEquals("10.0", clonedTs.getOptions().get(TextSymbolizer.SPACE_AROUND_KEY)); }
@Test public void testAllFeet() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); LineSymbolizer ls = sb.createLineSymbolizer(original); ls.setUnitOfMeasure(USCustomary.FOOT); ls.accept(visitor); Stroke clone = ((LineSymbolizer) visitor.getCopy()).getStroke(); assertEquals(2d, clone.getWidth().evaluate(null, Double.class), 0d); assertEquals(5f, clone.getDashArray()[0], 0f); assertEquals(10f, clone.getDashArray()[1], 0f); TextSymbolizer ts = sb.createTextSymbolizer(); ts.getOptions().put(TextSymbolizer.SPACE_AROUND_KEY, "10"); ts.setUnitOfMeasure(USCustomary.FOOT); ts.accept(visitor); TextSymbolizer clonedTs = (TextSymbolizer) visitor.getCopy(); assertEquals("10.0", clonedTs.getOptions().get(TextSymbolizer.SPACE_AROUND_KEY)); }
@Test public void testSymbolizerPixelOverrideInMeters() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); original.setWidth(ff.literal("2m")); LineSymbolizer ls = sb.createLineSymbolizer(original); ls.accept(visitor); Stroke clone = ((LineSymbolizer) visitor.getCopy()).getStroke(); // this one has not been rescaled assertEquals("2m", clone.getWidth().evaluate(null, String.class)); // the dash array did , it's supposed to be pixels assertEquals(10f, clone.getDashArray()[0], 0f); assertEquals(20f, clone.getDashArray()[1], 0f); TextSymbolizer ts = sb.createTextSymbolizer(); ts.getOptions().put(TextSymbolizer.SPACE_AROUND_KEY, "10m"); ts.accept(visitor); TextSymbolizer clonedTs = (TextSymbolizer) visitor.getCopy(); // this one has not been rescaled assertEquals("10.0m", clonedTs.getOptions().get(TextSymbolizer.SPACE_AROUND_KEY)); } }
@Test public void testSymbolizerMeterOverrideInPixels() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); original.setWidth(ff.literal("2px")); LineSymbolizer ls = sb.createLineSymbolizer(original); ls.setUnitOfMeasure(SI.METRE); ls.accept(visitor); Stroke clone = ((LineSymbolizer) visitor.getCopy()).getStroke(); // this one has been rescaled assertEquals(4d, clone.getWidth().evaluate(null, Double.class), 0d); // the dash array did not, it's supposed to be meters assertEquals(5f, clone.getDashArray()[0], 0f); assertEquals(10f, clone.getDashArray()[1], 0f); TextSymbolizer ts = sb.createTextSymbolizer(); ts.getOptions().put(TextSymbolizer.SPACE_AROUND_KEY, "10px"); ts.setUnitOfMeasure(SI.METRE); ts.accept(visitor); TextSymbolizer clonedTs = (TextSymbolizer) visitor.getCopy(); // this one has been rescaled assertEquals("20.0", clonedTs.getOptions().get(TextSymbolizer.SPACE_AROUND_KEY)); }