/** * Retrieves the Stroke from a PointSymbolizer. * * @param sym the symbolizer * @return the Stroke or null if not found. */ public static Stroke stroke(PointSymbolizer sym) { Mark mark = mark(sym); return (mark == null) ? null : mark.getStroke(); }
/** * Retrieves the color from the first Mark in a PointSymbolizer object. * * <p>If you are using something fun like symbols you will need to do your own thing. * * @param symbolizer Point symbolizer information. * @return Color of the point's mark, or null if unavailable. */ public static Color color(PointSymbolizer symbolizer) { if (symbolizer == null) { return null; } Graphic graphic = symbolizer.getGraphic(); if (graphic == null) { return null; } for (GraphicalSymbol gs : graphic.graphicalSymbols()) { if (gs != null && gs instanceof Mark) { Mark mark = (Mark) gs; Stroke stroke = mark.getStroke(); if (stroke != null) { Color colour = color(stroke); if (colour != null) { return colour; } } } } return null; }
public void visit(Mark mark) { Mark copy = null; copy = sf.createMark(); copy.setFill(copy(mark.getFill())); copy.setStroke(copy(mark.getStroke())); copy.setWellKnownName(copy(mark.getWellKnownName())); copy.setExternalMark(copy(mark.getExternalMark())); if (STRICT && !copy.equals(mark)) { throw new IllegalStateException("Was unable to duplicate provided Mark:" + mark); } pages.push(copy); }
public void visit(Mark mark) { start("Mark"); if (mark.getWellKnownName() != null && (!"square".equals(mark.getWellKnownName().evaluate(null)) || isExportDefaultValues())) { encodeValue("WellKnownName", null, mark.getWellKnownName(), null); } if (mark.getFill() != null) { mark.getFill().accept(this); } if (mark.getStroke() != null) { mark.getStroke().accept(this); } end("Mark"); }
private Geometry getGeometryBounds( Icon icon, Mark mark, Shape shape, double markSize, Object feature) { Geometry bounds; if (icon != null) { bounds = new GeometryBuilder().box(0, 0, icon.getIconWidth(), icon.getIconHeight()); } else { // the shape can be very complicated, go for the MBR. Wanted to use ShapeReader, but it // blindly assumes the shape is a polygon, while it may not be. Building a multipoint // instead AffineTransform at = AffineTransform.getScaleInstance(markSize, -markSize); Shape ts = at.createTransformedShape(shape); Geometry shapeGeometry = JTS.toGeometry(ts); bounds = new MinimumDiameter(shapeGeometry).getMinimumRectangle(); } // grow by the stroke size, if this is a mark if (icon == null && mark != null) { Stroke stroke = factory.getStroke(mark.getStroke(), feature); if (stroke instanceof BasicStroke) { float width = ((BasicStroke) stroke).getLineWidth() / 2 + 1; if (width > 0) { Geometry buffered = bounds.buffer(width); bounds = new MinimumDiameter(buffered).getMinimumRectangle(); } } } return bounds; }
/** * Retrieves the point border opacity from a PointSymbolizer. * * <p>If you are using something fun like rules you will need to do your own thing. * * @param symbolizer Point symbolizer information. * @return double of the point's border opacity, or NaN if unavailable. */ public static double pointBorderOpacity(PointSymbolizer symbolizer) { if (symbolizer == null) { return Double.NaN; } Graphic graphic = symbolizer.getGraphic(); if (graphic == null) { return Double.NaN; } for (GraphicalSymbol gs : graphic.graphicalSymbols()) { if (gs != null && gs instanceof Mark) { Mark mark = (Mark) gs; Stroke stroke = mark.getStroke(); if (stroke != null) { Expression exp = stroke.getOpacity(); return Filters.asDouble(exp); } } } return Double.NaN; }
/** @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Mark) */ public void visit(Mark mark) { if (mark.getFill() != null) { mark.getFill().accept(this); } if (mark.getStroke() != null) { mark.getStroke().accept(this); } if (mark.getWellKnownName() != null) { if (mark.getWellKnownName() instanceof Literal) { visitCqlExpression(mark.getWellKnownName().evaluate(null, String.class)); } else { mark.getWellKnownName().accept(this, null); } } }
SymbolsEncoder encode(Mark mark) { putName("shape", mark.getWellKnownName()); inline(new StrokeEncoder(mark.getStroke())); inline(new FillEncoder(mark.getFill())); // encode("stroke", new StrokeEncoder(mark.getStroke())); // encode("fill", mark.getFill()); // url: // inline: return this; }
@SuppressWarnings("deprecation") static MarkImpl cast(GraphicalSymbol item) { if (item == null) { return null; } else if (item instanceof MarkImpl) { return (MarkImpl) item; } else if (item instanceof Mark) { Mark mark = (Mark) item; MarkImpl copy = new MarkImpl(); copy.setStroke(mark.getStroke()); copy.setWellKnownName(mark.getWellKnownName()); copy.setExternalMark(mark.getExternalMark()); return copy; } return null; } }
MarkStyle2D createMarkStyle(Mark mark, Object feature, Symbolizer symbolizer, double size) { Shape shape = getShape(mark, feature); if (shape == null) { throw new IllegalArgumentException( "The specified mark " + mark.getWellKnownName() + " was not found!"); } Composite composite = getComposite(symbolizer.getOptions()); MarkStyle2D ms2d = new MarkStyle2D(); ms2d.setShape(shape); ms2d.setFill(getPaint(mark.getFill(), feature, symbolizer)); ms2d.setFillComposite( composite != null ? composite : getComposite(mark.getFill(), feature)); ms2d.setStroke(getStroke(mark.getStroke(), feature)); ms2d.setContour(getStrokePaint(mark.getStroke(), feature)); ms2d.setContourComposite( composite != null ? composite : getStrokeComposite(mark.getStroke(), feature)); // in case of Mark we don't have a natural size, so we default // to 16 if (size <= 0) { size = DEFAULT_MARK_SIZE; } ms2d.setSize(size); return ms2d; }
/** * Sets the Colour for the given point symbolizer * * @param symbolizer the point symbolizer * @param colour the new colour */ public static void setPointColour(PointSymbolizer symbolizer, Color colour) { if (symbolizer == null || colour == null) { return; } Graphic graphic = symbolizer.getGraphic(); if (graphic == null) { graphic = sf.createDefaultGraphic(); } for (GraphicalSymbol gs : graphic.graphicalSymbols()) { if (gs != null && gs instanceof Mark) { Mark mark = (Mark) gs; Stroke stroke = mark.getStroke(); if (stroke == null) { stroke = sf.createStroke(ff.literal(Color.BLACK), Stroke.DEFAULT.getWidth()); mark.setStroke(stroke); } Fill fill = mark.getFill(); if (fill != null) { fill.setColor(ff.literal(colour)); } } } }
@Test public void testRescalePolygonMargin() throws Exception { // create a graphic that needs rescaling StyleBuilder sb = new StyleBuilder(); // a graphic fill Fill fill = sb.createFill(); fill.setColor(null); fill.setGraphicFill( sb.createGraphic(null, sb.createMark("square", null, sb.createStroke(2)), null)); // a polygon and line symbolizer using them PolygonSymbolizer polygonSymbolizer = sb.createPolygonSymbolizer(sb.createStroke(), fill); polygonSymbolizer.getOptions().put(PolygonSymbolizer.GRAPHIC_MARGIN_KEY, "1 2 3 4"); // rescale it polygonSymbolizer.accept(visitor); PolygonSymbolizer rps = (PolygonSymbolizer) visitor.getCopy(); Mark rm = (Mark) rps.getFill().getGraphicFill().graphicalSymbols().get(0); assertEquals(4.0, rm.getStroke().getWidth().evaluate(null, Double.class), 0d); assertEquals("2 4 6 8", rps.getOptions().get(PolygonSymbolizer.GRAPHIC_MARGIN_KEY)); } }
private void rescale(Graphic graphic, Unit<Length> unit) { if (graphic != null) { graphic.setSize(rescale(graphic.getSize(), unit)); graphic.setGap(rescale(graphic.getGap(), unit)); Displacement disp = graphic.getDisplacement(); if (disp != null) { disp.setDisplacementX(rescale(disp.getDisplacementX(), unit)); disp.setDisplacementY(rescale(disp.getDisplacementY(), unit)); graphic.setDisplacement(disp); } if (graphic.graphicalSymbols() != null) { for (GraphicalSymbol gs : graphic.graphicalSymbols()) { if (gs instanceof Mark) { Mark mark = (Mark) gs; rescaleStroke(mark.getStroke(), unit); rescaleFill(mark.getFill(), unit); } } } } }
@Test public void testRescaleGraphicPointSymbolizer() { // create a graphic that needs rescaling StyleBuilder sb = new StyleBuilder(); // create a circle Mark circle = sb.createMark("circle", null, sb.createStroke(500)); Graphic g = sb.createGraphic(null, circle, null); // a point symbolizer with the specified circle PointSymbolizer ps = sb.createPointSymbolizer(g); // first see it in feet ps.setUnitOfMeasure(USCustomary.FOOT); // rescale it UomRescaleStyleVisitor visitor = new UomRescaleStyleVisitor(10); ps.accept(visitor); PointSymbolizer rps = (PointSymbolizer) visitor.getCopy(); Mark rm = (Mark) rps.getGraphic().graphicalSymbols().get(0); assertEquals(1524.0, rm.getStroke().getWidth().evaluate(null, Double.class), 0d); // now let's see the same in meters ps.setUnitOfMeasure(SI.METRE); // rescale it visitor = new UomRescaleStyleVisitor(10); ps.accept(visitor); rps = (PointSymbolizer) visitor.getCopy(); rm = (Mark) rps.getGraphic().graphicalSymbols().get(0); assertEquals(5000.0, rm.getStroke().getWidth().evaluate(null, Double.class), 0d); }
PolygonSymbolizer rps = (PolygonSymbolizer) visitor.getCopy(); Mark rm = (Mark) rps.getStroke().getGraphicStroke().graphicalSymbols().get(0); assertEquals(10.0, rm.getStroke().getWidth().evaluate(null, Double.class), 0d); rm = (Mark) rps.getFill().getGraphicFill().graphicalSymbols().get(0); assertEquals(20.0, rm.getStroke().getWidth().evaluate(null, Double.class), 0d); LineSymbolizer lps = (LineSymbolizer) visitor.getCopy(); rm = (Mark) lps.getStroke().getGraphicStroke().graphicalSymbols().get(0); assertEquals(10.0, rm.getStroke().getWidth().evaluate(null, Double.class), 0d);
@Test public void testMarkStroke() throws Exception { StyleBuilder sb = new StyleBuilder(); Mark mark = sb.createMark("square"); mark.getStroke().setWidth(sb.getFilterFactory().literal(10)); Graphic graphic = sb.createGraphic(null, mark, null); graphic.setSize(NilExpression.NIL); PointSymbolizer ps = sb.createPointSymbolizer(graphic); Style style = sb.createStyle(ps); MetaBufferEstimator estimator = new MetaBufferEstimator(); style.accept(estimator); assertTrue(estimator.isEstimateAccurate()); assertEquals(26, estimator.getBuffer()); }