Skip to content

Commit

Permalink
[GEOS-8156] Allow specifying number formatting for scale ratio decora…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
aaime committed May 28, 2017
1 parent 038abf8 commit c1de40c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
5 changes: 5 additions & 0 deletions doc/en/user/source/services/wms/decoration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ document.
- the background color for the text. supports RGB or RGBA colors specified as hex values.
* - ``fgcolor``
- the color for the text and border. follows the color specification from bgcolor.
* - ``format``
- the number format pattern, specified using Java own `DecimalFormat <https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html>`_ syntax
* - ``formatLanguage``
- the language used to drive number formatting (applies only if also ``format`` is used), using a valid Java `Locale <https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html>`_


The **scaleline** decoration (``type="scaleline"``) overlays a graphic showing the scale of the map in world units.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,29 @@
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

import org.geoserver.wms.WMSMapContent;


public class ScaleRatioDecoration implements MapDecoration {

String format = null;
String formatLanguage = null;

public void loadOptions(Map<String, String> options) {
String format = options.get("format");
if (format != null) {
this.format = format;
}
String formatLanguage = options.get("formatLanguage");
if (format != null) {
this.formatLanguage = formatLanguage;
}
}

public Dimension findOptimalSize(Graphics2D g2d, WMSMapContent mapContent){
Expand All @@ -33,7 +48,18 @@ public double getScale(WMSMapContent mapContent) {
}

public String getScaleText(WMSMapContent mapContent) {
return String.format("1 : %0$1.0f", getScale(mapContent));
final double scale = getScale(mapContent);
if(format == null) {
return String.format("1 : %0$1.0f", scale);
} else {
DecimalFormatSymbols decimalFormatSymbols;
if(formatLanguage != null) {
decimalFormatSymbols = DecimalFormatSymbols.getInstance(new Locale(formatLanguage));
} else {
decimalFormatSymbols = DecimalFormatSymbols.getInstance();
}
return "1 : " + new DecimalFormat(format, decimalFormatSymbols).format(scale);
}
}

public void paint(Graphics2D g2d, Rectangle paintArea, WMSMapContent mapContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
*/
package org.geoserver.wms.decoration;

import org.junit.Assert;
import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

Expand All @@ -20,12 +23,32 @@ public void testRatio() throws Exception {
ScaleRatioDecoration d = new ScaleRatioDecoration();

// 90 is DPI default value
Assert.assertEquals(3975, d.getScale(createMapContent(-1)), 1);
Assert.assertEquals(3975, d.getScale(createMapContent(25.4 / 0.28)), 1);
assertEquals(3975, d.getScale(createMapContent(-1)), 1);
assertEquals(3975, d.getScale(createMapContent(25.4 / 0.28)), 1);

assertEquals(13147, d.getScale(createMapContent(300)), 1);
assertEquals(26295, d.getScale(createMapContent(600)), 1);
assertEquals(78887, d.getScale(createMapContent(1800)), 1);
}

Assert.assertEquals(13147, d.getScale(createMapContent(300)), 1);
Assert.assertEquals(26295, d.getScale(createMapContent(600)), 1);
Assert.assertEquals(78887, d.getScale(createMapContent(1800)), 1);
@Test
public void testDefaultFormat() throws Exception {
ScaleRatioDecoration d = new ScaleRatioDecoration();
assertEquals("1 : 13148", d.getScaleText(createMapContent(300)));
}

@Test
public void testCustomFormat() throws Exception {
ScaleRatioDecoration d = new ScaleRatioDecoration();
Map<String, String> options = new HashMap<>();
options.put("format", "#,###");
options.put("formatLanguage", "en");
d.loadOptions(options);
assertEquals("1 : 13,148", d.getScaleText(createMapContent(300)));

options.put("formatLanguage", "it");
d.loadOptions(options);
assertEquals("1 : 13.148", d.getScaleText(createMapContent(300)));
}

}

0 comments on commit c1de40c

Please sign in to comment.