@Test public void testConstructorOK() { try { new UomRescaleStyleVisitor(1); } catch (Exception e2) { java.util.logging.Logger.getGlobal().log(java.util.logging.Level.INFO, "", e2); fail(e2.getClass().getSimpleName() + " should not be thrown."); } }
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); } } } } }
/** * 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); } }
@Override public void visit(LineSymbolizer line) { super.visit(line); LineSymbolizer copy = (LineSymbolizer) pages.peek(); Unit<Length> uom = copy.getUnitOfMeasure(); Stroke copyStroke = copy.getStroke(); rescaleStroke(copyStroke, uom); copy.setPerpendicularOffset(rescale(copy.getPerpendicularOffset(), uom)); copy.setUnitOfMeasure(Units.PIXEL); }
@Test public void testVisitLineSymbolizer_NullStroke() { try { UomRescaleStyleVisitor visitor = null; StyleBuilder styleBuilder = new StyleBuilder(); Stroke stroke = null; LineSymbolizerImpl lineSymb = (LineSymbolizerImpl) styleBuilder.createLineSymbolizer(stroke); lineSymb.setUnitOfMeasure(SI.METRE); visitor = new UomRescaleStyleVisitor(10); lineSymb.accept(visitor); LineSymbolizer rescaledLineSymb = (LineSymbolizer) visitor.getCopy(); assertNull(rescaledLineSymb.getStroke()); assertNotSame(rescaledLineSymb, lineSymb); } catch (Exception e2) { java.util.logging.Logger.getGlobal().log(java.util.logging.Level.INFO, "", e2); fail(e2.getClass().getSimpleName() + " should not be thrown."); } }
/** * Tests size calculation of dynamically sized feature, using real-world units combined with * DPI-based resizing. */ @Test public void testCombinedResizingDpiUom() { // given: Point with dynamic size in real-world units int sizeNum = 10; double scaleDpi = 2; double scaleUom = 3; PointSymbolizer symbolizer = sb.createPointSymbolizer(); Graphic graphic = sb.createGraphic(); graphic.setSize(ff.function("strConcat", ff.property("size"), ff.literal("m"))); symbolizer.setGraphic(graphic); // when: DPI based resizing is applied RescaleStyleVisitor dpiVisitor = new GraphicsAwareDpiRescaleStyleVisitor(scaleDpi); symbolizer.accept(dpiVisitor); symbolizer = (PointSymbolizer) dpiVisitor.getCopy(); // and: UOM resizing is applied UomRescaleStyleVisitor uomVisitor = new UomRescaleStyleVisitor(scaleUom); uomVisitor.visit(symbolizer); symbolizer = (PointSymbolizer) uomVisitor.getCopy(); SimpleFeature feature = createFeatureSized(sizeNum); // then: only the UOM resizing must have an effect Double size = symbolizer.getGraphic().getSize().evaluate(feature, Double.class); assertEquals(sizeNum * scaleUom, size.doubleValue(), 0d); }
font.setSize(rescale(font.getSize(), uom)); Displacement disp = pointPlacement.getDisplacement(); if (disp != null) { disp.setDisplacementX(rescale(disp.getDisplacementX(), uom)); disp.setDisplacementY(rescale(disp.getDisplacementY(), uom)); pointPlacement.setDisplacement(disp); linePlacement.setGap(rescale(linePlacement.getGap(), uom)); linePlacement.setInitialGap(rescale(linePlacement.getInitialGap(), uom)); linePlacement.setPerpendicularOffset( rescale(linePlacement.getPerpendicularOffset(), uom)); copy.getHalo().setRadius(rescale(copy.getHalo().getRadius(), uom)); TextSymbolizer2 copy2 = (TextSymbolizer2) copy; rescale(copy2.getGraphic(), uom); scaleIntOption(options, TextSymbolizer.MAX_DISPLACEMENT_KEY, uom); scaleIntOption(options, TextSymbolizer.SPACE_AROUND_KEY, uom); scaleIntOption(options, TextSymbolizer.MIN_GROUP_DISTANCE_KEY, uom); scaleIntOption(options, TextSymbolizer.LABEL_REPEAT_KEY, uom); scaleIntOption(options, TextSymbolizer.AUTO_WRAP_KEY, uom); scaleIntArrayOption(options, TextSymbolizer.GRAPHIC_MARGIN_KEY, uom);
private void rescaleFill(Fill copyFill, Unit<Length> unit) { // rescale the graphic fill, if any if (copyFill != null) { rescale(copyFill.getGraphicFill(), unit); } }
/** * Used to rescale the provided unscaled value. * * @param unscaled * the unscaled value. * @param mapScale * the mapScale in pixels per meter. * @param uom * the unit of measure that will be used to scale. * @return the expression multiplied by the provided scale. */ protected Expression rescale(Expression unscaled, double mapScale, Unit<Length> uom) { if (unscaled == null || unscaled.equals(Expression.NIL)) return unscaled; if (unscaled instanceof Literal && unscaled.evaluate(null, Double.class) != null) { // if given Expression is a literal, we can return a literal double rescaled = rescale(unscaled.evaluate(null, Double.class), mapScale, uom); return ff.literal(rescaled); } else { // otherwise, we return a Multiply expression with the rescaling multiplier double rescaleMultiplier = computeRescaleMultiplier(mapScale, uom); return ff.multiply(unscaled, ff.literal(rescaleMultiplier)); } }
/** * Used to rescale the provided unscaled value. * * @param unscaled * the unscaled value. * @param mapScale * the mapScale in pixels per meter. * @param uom * the unit of measure that will be used to scale. * @return a scaled value. */ protected double rescale(double unscaled, double mapScale, Unit<Length> uom) { // computes the basic rescaled value return unscaled * computeRescaleMultiplier(mapScale, uom); }
@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); }
UomRescaleStyleVisitor rescaleVisitor = new UomRescaleStyleVisitor(pixelsPerMeters); for (int j = 0; j < ruleList.size(); j++) { rescaleVisitor.visit(ruleList.get(j)); ruleList.set(j, (Rule) rescaleVisitor.getCopy()); rescaleVisitor.visit(elseRuleList.get(j)); elseRuleList.set(j, (Rule) rescaleVisitor.getCopy());
font.setSize(rescale(font.getSize(), mapScale, uom)); copy.setFonts(fonts); Displacement disp = pointPlacement.getDisplacement(); if (disp != null) { disp.setDisplacementX(rescale(disp.getDisplacementX(), mapScale, uom)); disp.setDisplacementY(rescale(disp.getDisplacementY(), mapScale, uom)); pointPlacement.setDisplacement(disp); linePlacement.setGap(rescale(linePlacement.getGap(), mapScale, uom)); linePlacement.setInitialGap(rescale(linePlacement.getInitialGap(), mapScale, uom)); linePlacement.setPerpendicularOffset(rescale(linePlacement.getPerpendicularOffset(), mapScale, uom)); copy.getHalo().setRadius(rescale(copy.getHalo().getRadius(), mapScale, uom)); TextSymbolizer2 copy2 = (TextSymbolizer2) copy; rescale(copy2.getGraphic(), mapScale, uom); scaleIntOption(options, TextSymbolizer.MAX_DISPLACEMENT_KEY, uom); scaleIntOption(options, TextSymbolizer.SPACE_AROUND_KEY, uom); scaleIntOption(options, TextSymbolizer.MIN_GROUP_DISTANCE_KEY, uom); scaleIntOption(options, TextSymbolizer.LABEL_REPEAT_KEY, uom); scaleIntOption(options, TextSymbolizer.AUTO_WRAP_KEY, uom); scaleIntArrayOption(options, TextSymbolizer.GRAPHIC_MARGIN_KEY, uom);
/** Rescale a list of expressions, can handle null. */ protected List<Expression> rescaleDashArray(List<Expression> expressions, Unit<Length> uom) { if (expressions == null || expressions.isEmpty()) { return expressions; } List<Expression> rescaled = new ArrayList<>(expressions.size()); final Expression scale = rescale(ff.literal(1), uom); for (Expression expression : expressions) { Expression rescale = ff.function("listMultiply", scale, expression); if (expression instanceof Literal) { rescaled.add(ff.literal(rescale.evaluate(null))); } else { rescaled.add(rescale); } } return rescaled; }
private void scaleIntArrayOption( Map<String, String> options, String optionName, Unit<Length> uom) { if (options.containsKey(optionName)) { String strValue = options.get(optionName); String[] splitted = strValue.split("\\s+"); StringBuilder sb = new StringBuilder(); for (String value : splitted) { String rescaled = rescale(value, uom); sb.append(toInt(rescaled)).append(" "); } sb.setLength(sb.length() - 1); options.put(optionName, sb.toString()); } }
@Test public void testVisitPolygonSymbolizer_NullStroke() { try { UomRescaleStyleVisitor visitor = null; StyleBuilder styleBuilder = new StyleBuilder(); Fill fill = styleBuilder.createFill(Color.RED); PolygonSymbolizerImpl polySymb = (PolygonSymbolizerImpl) styleBuilder.createPolygonSymbolizer(null, fill); visitor = new UomRescaleStyleVisitor(10); polySymb.accept(visitor); PolygonSymbolizer rescaledPolySymb = (PolygonSymbolizer) visitor.getCopy(); assertEquals(polySymb.getFill(), rescaledPolySymb.getFill()); assertNull(rescaledPolySymb.getStroke()); assertNotSame(rescaledPolySymb, polySymb); } catch (Exception e2) { java.util.logging.Logger.getGlobal().log(java.util.logging.Level.INFO, "", e2); fail(e2.getClass().getSimpleName() + " should not be thrown."); } }
RendererUtilities.calculatePixelsPerMeterRatio( request.getScale(), request.getLegendOptions()); UomRescaleStyleVisitor rescaleVisitor = new UomRescaleStyleVisitor(pixelsPerMeters); rescaleVisitor.visit(gt2Style); gt2Style = (Style) rescaleVisitor.getCopy();
@Test public void testConstructorFail() { try { new UomRescaleStyleVisitor(-1); } catch (IllegalArgumentException e) { return; } fail("Should throw IllegalArgumentException."); }
private void rescale(Graphic graphic, double mapScale, Unit<Length> unit) { if(graphic != null) { graphic.setSize(rescale(graphic.getSize(), mapScale, unit)); graphic.setGap(rescale(graphic.getGap(), mapScale, unit)); Displacement disp = graphic.getDisplacement(); if (disp != null) { disp.setDisplacementX(rescale(disp.getDisplacementX(), mapScale, unit)); disp.setDisplacementY(rescale(disp.getDisplacementY(), mapScale, unit)); graphic.setDisplacement(disp); } if (graphic.graphicalSymbols() != null) { for (GraphicalSymbol gs : graphic.graphicalSymbols()) { if(gs instanceof Mark) { Mark mark = (Mark) gs; rescaleStroke(mark.getStroke(), mapScale, unit); rescaleFill(mark.getFill(), mapScale, unit); } } } } }