/** * Retrieve the width of a Stroke object. * * @param stroke the Stroke object. * @return width or {@linkplain #NOTFOUND} if not available. */ public static int width(Stroke stroke) { if (stroke == null) { return NOTFOUND; } return Filters.asInt(stroke.getWidth()); }
/** * Retrieves the outline width from a PolygonSymbolizer * * <p>If you are using something fun like rules you will need to do your own thing. * * @param symbolizer Polygon symbolizer information. * @return outline width or {@linkplain #NOTFOUND} if unavailable. */ public static int polyWidth(PolygonSymbolizer symbolizer) { if (symbolizer == null) { return NOTFOUND; } Stroke stroke = symbolizer.getStroke(); if (stroke != null) { return Filters.asInt(stroke.getWidth()); } return NOTFOUND; }
/** @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Stroke) */ public void visit(Stroke stroke) { try { Expression width = stroke.getWidth(); if (!isNull(width)) { evaluateWidth(width); } if (stroke.getGraphicStroke() != null) { stroke.getGraphicStroke().accept(this); } } catch (ClassCastException e) { estimateAccurate = false; LOGGER.info("Could not parse stroke width, " + "it's a literal but not a Number..."); } }
/** Reset stroke to default values. */ public StrokeBuilder reset() { color = Stroke.DEFAULT.getColor(); width = Stroke.DEFAULT.getWidth(); opacity = Stroke.DEFAULT.getOpacity(); lineCap = Stroke.DEFAULT.getLineCap(); lineJoin = Stroke.DEFAULT.getLineJoin(); dashArray = Stroke.DEFAULT.getDashArray(); dashArrayExpressions = Stroke.DEFAULT.dashArray(); dashOffset = Stroke.DEFAULT.getDashOffset(); graphicFill.unset(); graphicStroke.unset(); unset = false; return this; }
@Test public void testDynamicStroke() throws Exception { Stroke original = sb.createStroke(Color.RED, 2); original.setDashArray(Arrays.asList((Expression) ff.literal("5 10"))); original.accept(visitor); Stroke clone = (Stroke) visitor.getCopy(); assertEquals(4.0d, Double.valueOf((String) clone.getWidth().evaluate(null)), 0.001); assertNotNull(original.dashArray()); assertEquals(1, original.dashArray().size()); assertEquals("10.0 20.0", ((Expression) clone.dashArray().get(0)).evaluate(null)); }
public void visit(Stroke stroke) { Stroke copy = sf.getDefaultStroke(); copy.setColor(copy(stroke.getColor())); copy.setDashArray(copyExpressions(stroke.dashArray())); copy.setDashOffset(copy(stroke.getDashOffset())); copy.setGraphicFill(copy(stroke.getGraphicFill())); copy.setGraphicStroke(copy(stroke.getGraphicStroke())); copy.setLineCap(copy(stroke.getLineCap())); copy.setLineJoin(copy(stroke.getLineJoin())); copy.setOpacity(copy(stroke.getOpacity())); copy.setWidth(copy(stroke.getWidth())); if (STRICT && !copy.equals(stroke)) { throw new IllegalStateException("Was unable to duplicate provided Stroke:" + stroke); } pages.push(copy); }
/** * Used to rescaleDashArray the provided stroke. * * @param stroke the unscaled stroke, which will be modified in-place. * @param mapScale the mapScale in pixels per meter. * @param uom the unit of measure that will be used to scale. */ protected void rescaleStroke(Stroke stroke, Unit<Length> uom) { if (stroke != null) { stroke.setWidth(rescale(stroke.getWidth(), uom)); stroke.setDashArray(rescaleDashArray(stroke.dashArray(), uom)); stroke.setDashOffset(rescale(stroke.getDashOffset(), uom)); rescale(stroke.getGraphicFill(), uom); rescale(stroke.getGraphicStroke(), uom); } }
@Test public void testRescaleLocalUOM() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); original.setWidth(ff.literal("2m")); original.accept(visitor); Stroke clone = (Stroke) visitor.getCopy(); assertEquals("4m", clone.getWidth().evaluate(null, String.class)); }
@Test public void visitLocalUomPixelOverridingMeters() { UomRescaleStyleVisitor visitor = null; double size = 100; double scaleMetersToPixel = 10; StyleBuilder styleBuilder = new StyleBuilder(); LineSymbolizerImpl lineSymb = (LineSymbolizerImpl) styleBuilder.createLineSymbolizer(); lineSymb.setUnitOfMeasure(SI.METRE); lineSymb.getStroke().setWidth(ff.literal(size + "px")); visitor = new UomRescaleStyleVisitor(scaleMetersToPixel); lineSymb.accept(visitor); LineSymbolizer rescaledLineSymb = (LineSymbolizer) visitor.getCopy(); double rescaledSize = rescaledLineSymb.getStroke().getWidth().evaluate(null, Double.class); assertEquals(Math.round(size), Math.round(size)); assertNotSame(rescaledLineSymb, lineSymb); }
@Test public void visitLocalUomMeters() { UomRescaleStyleVisitor visitor = null; double size = 100; double scaleMetersToPixel = 10; double expectedRescaledSize = computeExpectedRescaleSize(size, scaleMetersToPixel, SI.METRE); StyleBuilder styleBuilder = new StyleBuilder(); LineSymbolizerImpl lineSymb = (LineSymbolizerImpl) styleBuilder.createLineSymbolizer(); lineSymb.getStroke().setWidth(ff.literal(size + "m")); visitor = new UomRescaleStyleVisitor(scaleMetersToPixel); lineSymb.accept(visitor); LineSymbolizer rescaledLineSymb = (LineSymbolizer) visitor.getCopy(); double rescaledSize = rescaledLineSymb.getStroke().getWidth().evaluate(null, Double.class); assertEquals(Math.round(expectedRescaledSize), Math.round(rescaledSize)); assertNotSame(rescaledLineSymb, lineSymb); }
@Test public void visitLocalUomOverrideFeet() { UomRescaleStyleVisitor visitor = null; double size = 100; double scaleMetersToPixel = 10; double expectedRescaledSize = computeExpectedRescaleSize(size, scaleMetersToPixel, SI.METRE); StyleBuilder styleBuilder = new StyleBuilder(); LineSymbolizerImpl lineSymb = (LineSymbolizerImpl) styleBuilder.createLineSymbolizer(); lineSymb.setUnitOfMeasure(USCustomary.FOOT); lineSymb.getStroke().setWidth(ff.literal(size + "m")); visitor = new UomRescaleStyleVisitor(scaleMetersToPixel); lineSymb.accept(visitor); LineSymbolizer rescaledLineSymb = (LineSymbolizer) visitor.getCopy(); double rescaledSize = rescaledLineSymb.getStroke().getWidth().evaluate(null, Double.class); assertEquals(Math.round(expectedRescaledSize), Math.round(rescaledSize)); assertNotSame(rescaledLineSymb, lineSymb); }
@Test public void testStroke() throws Exception { Stroke original = sb.createStroke(Color.RED, 2, new float[] {5, 10}); original.accept(visitor); Stroke clone = (Stroke) visitor.getCopy(); assertEquals(4.0d, clone.getWidth().evaluate(null, Double.class), 0d); assertEquals(10.0f, clone.getDashArray()[0], 0d); assertEquals(20.0f, clone.getDashArray()[1], 0d); }
@Test public void testRescaleLocalPixelInPixelSymbolizer() throws Exception { Stroke stroke = sb.createStroke(Color.RED, 2, new float[] {5, 10}); stroke.setWidth(ff.literal("2px")); LineSymbolizer ls = sb.createLineSymbolizer(stroke); ls.accept(visitor); LineSymbolizer clone = (LineSymbolizer) visitor.getCopy(); assertEquals("4", clone.getStroke().getWidth().evaluate(null, String.class)); }
@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 testRescaleLocalPixelInMetersSymbolizer() throws Exception { Stroke stroke = sb.createStroke(Color.RED, 2, new float[] {5, 10}); stroke.setWidth(ff.literal("2px")); LineSymbolizer ls = sb.createLineSymbolizer(stroke); ls.setUnitOfMeasure(SI.METRE); ls.accept(visitor); LineSymbolizer clone = (LineSymbolizer) visitor.getCopy(); assertEquals("4px", clone.getStroke().getWidth().evaluate(null, String.class)); }
/** * Increase stroke width. * * <p>Based on feedback we may need to change the dash array as well. * * <p> */ public void visit(org.geotools.styling.Stroke stroke) { Stroke copy = sf.getDefaultStroke(); copy.setColor(copy(stroke.getColor())); copy.setDashArray(rescaleDashArray(stroke.dashArray())); copy.setDashOffset(rescale(stroke.getDashOffset())); copy.setGraphicFill(copy(stroke.getGraphicFill())); copy.setGraphicStroke(copy(stroke.getGraphicStroke())); copy.setLineCap(copy(stroke.getLineCap())); copy.setLineJoin(copy(stroke.getLineJoin())); copy.setOpacity(copy(stroke.getOpacity())); copy.setWidth(rescale(stroke.getWidth())); pages.push(copy); }
@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 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 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)); } }
@Override protected void encode(Stroke stroke) { putColor("stroke-color", stroke.getColor()); put("stroke-width", stroke.getWidth()); put("stroke-opacity", nullIf(stroke.getOpacity(), 1d)); put("stroke-linejoin", nullIf(stroke.getLineJoin(), "miter")); putName("stroke-linecap", nullIf(stroke.getLineCap(), "butt")); put("stroke-dasharray", toStringOrNull(stroke.getDashArray())); put("stroke-dashoffset", nullIf(stroke.getDashOffset(), 0)); if (stroke.getGraphicFill() != null) { push("stroke-graphic-fill").inline(new GraphicEncoder(stroke.getGraphicFill())); } if (stroke.getGraphicStroke() != null) { push("stroke-graphic").inline(new GraphicEncoder(stroke.getGraphicStroke())); } }