Skip to content

Commit 915ff08

Browse files
committed
Add halo, point and line placement for labels in the SimpleStyleReader.
1 parent 0c1932e commit 915ff08

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/groovy/geoscript/style/io/SimpleStyleReader.groovy

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package geoscript.style.io
22

3+
import geoscript.filter.Color
34
import geoscript.style.Composite
45
import geoscript.style.Fill
56
import geoscript.style.Font
@@ -102,7 +103,7 @@ class SimpleStyleReader implements Reader {
102103
)
103104
parts.add(icon)
104105
}
105-
if (['label-size','label-style','label-weight','label-family'].any{ options.containsKey(it) }) {
106+
if (['label', 'label-size','label-style','label-weight','label-family', 'label-color'].any{ options.containsKey(it) }) {
106107
Font font = new Font(
107108
size: options.get('label-size',12),
108109
style: options.get('label-style', 'normal'),
@@ -111,8 +112,37 @@ class SimpleStyleReader implements Reader {
111112
)
112113
Label label = new Label(
113114
property: options.get('label'),
114-
font: font
115+
font: font,
116+
fill: new Fill(new Color(options.get("label-color", "black")))
115117
)
118+
if (['label-halo-color','label-halo-radius'].any { options.containsKey(it)}) {
119+
label.halo(new Fill(options.get("label-halo-color", "white")), options.get("label-halo-radius", 5))
120+
}
121+
String placement = options.get("label-placement", "point")
122+
if (placement.equalsIgnoreCase("point")) {
123+
// label-point-anchor, label-point-displace, label-point-rotate
124+
Map params = [
125+
anchor: options.get("label-point-anchor","0.5,0.5")?.split(","),
126+
displace: options.get("label-point-displace","0,0")?.split(","),
127+
rotate: options.get("label-point-rotate",0) as double
128+
]
129+
label.point(params)
130+
} else if (placement.equalsIgnoreCase("line")) {
131+
// label-line-offset, label-line-gap, label-line-igap, label-line-align, label-line-follow,
132+
// label-line-group, label-line-displacement, label-line-repeat
133+
Map params = [
134+
offset: options.get("label-line-offset", 0) as double,
135+
gap: options.get("label-line-gap"),
136+
igap: options.get("label-line-igap"),
137+
align: options.get("label-line-align", false) as boolean,
138+
follow: options.get("label-line-follow", false) as boolean,
139+
group: options.get("label-line-group", false) as boolean,
140+
displacement: options.get("label-line-displacement"),
141+
repeat: options.get("label-line-repeat")
142+
]
143+
label.linear(params)
144+
}
145+
116146
parts.add(label)
117147
}
118148
new Composite(parts)

src/test/groovy/geoscript/style/io/SimpleStyleReaderTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ class SimpleStyleReaderTest {
5353
assertEquals("image/jpeg", icon.format)
5454
}
5555

56+
@Test void pointWithLabelWithHalo() {
57+
SimpleStyleReader styleReader = new SimpleStyleReader()
58+
Style style = styleReader.read("shape-type=triangle label=NAME label-size=12 label-halo-color=white label-halo-radius=7")
59+
assertEquals style.toString(), "Composite (Shape(color = #7e7e7e, size = 6, type = triangle), Label(property = NAME))"
60+
}
61+
62+
@Test void pointWithLabelWithPointPlacement() {
63+
SimpleStyleReader styleReader = new SimpleStyleReader()
64+
Style style = styleReader.read("shape-type=triangle label=NAME label-placement=point")
65+
assertEquals style.toString(), "Composite (Shape(color = #7e7e7e, size = 6, type = triangle), Label(property = NAME))"
66+
}
67+
68+
@Test void pointWithLabelWithComplexPointPlacement() {
69+
SimpleStyleReader styleReader = new SimpleStyleReader()
70+
Style style = styleReader.read("shape-type=triangle label=NAME label-placement=point label-point-anchor=5,6 label-point-displace=10,10 label-point-rotate=45")
71+
assertEquals style.toString(), "Composite (Shape(color = #7e7e7e, size = 6, type = triangle), Label(property = NAME))"
72+
}
73+
74+
@Test void pointWithLabelWithLinePlacement() {
75+
SimpleStyleReader styleReader = new SimpleStyleReader()
76+
Style style = styleReader.read("shape-type=triangle label=NAME label-placement=line")
77+
assertEquals style.toString(), "Composite (Shape(color = #7e7e7e, size = 6, type = triangle), Label(property = NAME))"
78+
}
79+
80+
@Test void pointWithLabelWithComplexLinePlacement() {
81+
SimpleStyleReader styleReader = new SimpleStyleReader()
82+
Style style = styleReader.read("shape-type=triangle label=NAME label-placement=line label-line-offset=4 label-line-gap=2 label-line-igap=3 label-line-align=true label-line-follow=true label-line-group=true label-line-displacement=12 label-line-repeat=true")
83+
assertEquals style.toString(), "Composite (Shape(color = #7e7e7e, size = 6, type = triangle), Label(property = NAME))"
84+
}
85+
5686
@Test void readFromMap() {
5787
SimpleStyleReader styleReader = new SimpleStyleReader()
5888
Style style = styleReader.read([fill: 'wheat', 'stroke-width': 1.2])

0 commit comments

Comments
 (0)