Permalink
Browse files

[GEOT-4449] UOMRescaleStyleVisitor fails to rescale graphic-margin in…

… array mode
  • Loading branch information...
1 parent 7ea4466 commit 3b6a6d71b25f40779657ecdbfd3f616b8284c9bb @aaime aaime committed Apr 14, 2013
@@ -20,6 +20,9 @@
import java.util.Map;
+import javax.measure.quantity.Length;
+import javax.measure.unit.Unit;
+
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.styling.Displacement;
import org.geotools.styling.ExternalGraphic;
@@ -241,6 +244,7 @@ public void visit(TextSymbolizer text) {
rescaleOption(options, MIN_GROUP_DISTANCE_KEY, DEFAULT_MIN_GROUP_DISTANCE);
rescaleOption(options, LABEL_REPEAT_KEY, DEFAULT_LABEL_REPEAT);
rescaleOption(options, AUTO_WRAP_KEY, DEFAULT_AUTO_WRAP);
+ rescaleArrayOption(options, GRAPHIC_MARGIN_KEY, 0);
}
/**
@@ -278,5 +282,30 @@ protected void rescaleOption(Map<String, String> options, String key, int defaul
}
};
+
+ /**
+ * Rescales the specified vendor option
+ * @param options
+ * @param key
+ * @param defaultAutoWrap
+ * @param value
+ */
+ protected void rescaleArrayOption(Map<String, String> options, String key, int defaultValue) {
+ double scaleFactor = (double) scale.evaluate(null, Double.class);
+ if(options.get(key) != null) {
+ String strValue = options.get(key);
+ String[] splitted = strValue.split("\\s+");
+ StringBuilder sb = new StringBuilder();
+ for (String value : splitted) {
+ double rescaled = (int) Math.round(Double.parseDouble(value) * scaleFactor);
+ sb.append((int) rescaled).append(" ");
+ }
+ sb.setLength(sb.length() - 1);
+ options.put(key, sb.toString());
+ } else if(defaultValue != 0) {
+ options.put(key, String.valueOf((int) Math.round(defaultValue * scaleFactor)));
+ }
+ };
+
}
@@ -190,8 +190,13 @@ public void visit(PointSymbolizer ps) {
super.visit(ps);
PointSymbolizer copy = (PointSymbolizer) pages.peek();
+ Unit<Length> uom = copy.getUnitOfMeasure();
+ if(uom == null || uom.equals(NonSI.PIXEL)) {
+ return;
+ }
+
Graphic copyGraphic = copy.getGraphic();
- rescale(copyGraphic, mapScale, copy.getUnitOfMeasure());
+ rescale(copyGraphic, mapScale, uom);
copy.setUnitOfMeasure(NonSI.PIXEL);
}
@@ -223,9 +228,13 @@ private void rescale(Graphic graphic, double mapScale, Unit<Length> unit) {
public void visit(LineSymbolizer line) {
super.visit(line);
LineSymbolizer copy = (LineSymbolizer) pages.peek();
+ Unit<Length> uom = copy.getUnitOfMeasure();
+ if(uom == null || uom.equals(NonSI.PIXEL)) {
+ return;
+ }
Stroke copyStroke = copy.getStroke();
- rescaleStroke(copyStroke, mapScale, copy.getUnitOfMeasure());
+ rescaleStroke(copyStroke, mapScale, uom);
copy.setUnitOfMeasure(NonSI.PIXEL);
}
@@ -234,8 +243,12 @@ public void visit(PolygonSymbolizer poly) {
super.visit(poly);
PolygonSymbolizer copy = (PolygonSymbolizer) pages.peek();
- rescaleStroke(copy.getStroke(), mapScale, copy.getUnitOfMeasure());
- rescaleFill(copy.getFill(), mapScale, copy.getUnitOfMeasure());
+ Unit<Length> uom = copy.getUnitOfMeasure();
+ if(uom == null || uom.equals(NonSI.PIXEL)) {
+ return;
+ }
+ rescaleStroke(copy.getStroke(), mapScale, uom);
+ rescaleFill(copy.getFill(), mapScale, uom);
copy.setUnitOfMeasure(NonSI.PIXEL);
}
@@ -253,6 +266,9 @@ public void visit(TextSymbolizer text) {
TextSymbolizer copy = (TextSymbolizer) pages.peek();
Unit<Length> uom = copy.getUnitOfMeasure();
+ if(uom == null || uom.equals(NonSI.PIXEL)) {
+ return;
+ }
// rescales fonts
Font[] fonts = copy.getFonts();
@@ -298,7 +314,8 @@ public void visit(TextSymbolizer text) {
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.GRAPHIC_MARGIN_KEY, uom);
+ scaleIntOption(options, TextSymbolizer.AUTO_WRAP_KEY, uom);
+ scaleIntArrayOption(options, TextSymbolizer.GRAPHIC_MARGIN_KEY, uom);
copy.setUnitOfMeasure(NonSI.PIXEL);
}
@@ -312,4 +329,19 @@ private void scaleIntOption(Map<String, String> options,
options.put(optionName, Integer.toString((int) v));
}
}
+
+ 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) {
+ double rescaled = rescale(Double.parseDouble(value), mapScale, uom);
+ sb.append((int) rescaled).append(" ");
+ }
+ sb.setLength(sb.length() - 1);
+ options.put(optionName, sb.toString());
+ }
+ }
}
@@ -18,8 +18,6 @@
import java.awt.Color;
-import javax.measure.unit.SI;
-
import junit.framework.TestCase;
import org.geotools.factory.CommonFactoryFinder;
@@ -140,10 +138,21 @@ public void testStroke() throws Exception {
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));
+ }
+
+ 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));
}
public void testRescaleGraphicFillStrokes() {
@@ -17,6 +17,7 @@
package org.geotools.styling.visitor;
import java.awt.Color;
+import java.util.Map;
import javax.measure.quantity.Length;
import javax.measure.unit.NonSI;
@@ -223,6 +224,8 @@ protected void visitTextSymbolizerTest(double scaleMetersToPixel, Unit<Length> u
double expectedRescaledDisplacementXSize = computeExpectedRescaleSize(displacementX, scaleMetersToPixel, uom);
double expectedRescaledDisplacementYSize = computeExpectedRescaleSize(displacementY, scaleMetersToPixel, uom);
int expectedMaxDisplacement = (int) computeExpectedRescaleSize(maxDisplacement, scaleMetersToPixel, uom);
+ int expectedGraphicMargin1 = (int) computeExpectedRescaleSize(maxDisplacement, scaleMetersToPixel, uom);
+ int expectedGraphicMargin2 = (int) computeExpectedRescaleSize(maxDisplacement * 2, scaleMetersToPixel, uom);
StyleBuilder styleBuilder = new StyleBuilder();
@@ -237,6 +240,7 @@ protected void visitTextSymbolizerTest(double scaleMetersToPixel, Unit<Length> u
// check we can rescale properly also vendor options
textSymb.addToOptions("maxDisplacement", String.valueOf(maxDisplacement));
+ textSymb.addToOptions(TextSymbolizer.GRAPHIC_MARGIN_KEY, maxDisplacement + " " + maxDisplacement * 2);
visitor = new UomRescaleStyleVisitor(scaleMetersToPixel);
@@ -253,8 +257,15 @@ protected void visitTextSymbolizerTest(double scaleMetersToPixel, Unit<Length> u
Assert.assertEquals(Math.round(expectedRescaledDisplacementYSize), Math.round(rescaledDisplacementYSize));
Assert.assertNotSame(rescaledTextSymb, textSymb);
- int rescaledMaxDisplacement = Converters.convert(rescaledTextSymb.getOptions().get("maxDisplacement"), Integer.class).intValue();
+ Map<String, String> options = rescaledTextSymb.getOptions();
+ int rescaledMaxDisplacement = Converters.convert(options.get("maxDisplacement"), Integer.class).intValue();
assertEquals(rescaledMaxDisplacement, expectedMaxDisplacement);
+
+ String[] splitted = options.get(TextSymbolizer.GRAPHIC_MARGIN_KEY).split("\\s+");
+ int rescaledGraphicMargin1 = Converters.convert(splitted[0], Integer.class).intValue();
+ int rescaledGraphicMargin2 = Converters.convert(splitted[1], Integer.class).intValue();
+ assertEquals(expectedGraphicMargin1, rescaledGraphicMargin1);
+ assertEquals(expectedGraphicMargin2, rescaledGraphicMargin2);
}
catch (Exception e2)
{

0 comments on commit 3b6a6d7

Please sign in to comment.