Skip to content

Commit 0ef70be

Browse files
committed
Add xml map reader
1 parent 10a4b0a commit 0ef70be

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

src/main/groovy/geoscript/render/io/JsonMapReader.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class JsonMapReader implements MapReader {
3939
type: values.get("type", "png"),
4040
backgroundColor: values.get("backgroundColor"),
4141
fixAspectRatio: values.get("fixAspectRation", true),
42-
proj: values.get("proj"),
4342
layers: Renderables.getRenderables(values.get("layers"))
4443
)
4544
if (values.get("proj")) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package geoscript.render.io
2+
3+
import geoscript.geom.Bounds
4+
import geoscript.layer.Renderables
5+
import geoscript.render.Map as GMap
6+
import groovy.xml.XmlSlurper
7+
8+
/**
9+
* Read a Map from an XML String.
10+
* <pre>
11+
* {@code
12+
* <map>
13+
* <width>400</width>
14+
* <height>400</height>
15+
* <type>png</type>
16+
* <proj>EPSG:4326</proj>
17+
* <backgroundColor>blue</backgroundColor>
18+
* <fixAspectRatio>true</fixAspectRatio>
19+
* <layers>
20+
* <layer>
21+
* <layertype>layer</layertype>
22+
* <file>states.shp</file>
23+
* </layer>
24+
* </layers>
25+
* <bounds>
26+
* <minX>-135.911779</minX>
27+
* <minY>36.993573</minY>
28+
* <maxX>-96.536779</maxX>
29+
* <maxY>51.405899</maxY>
30+
* </bounds>
31+
* </map>
32+
* }
33+
* </pre>
34+
* @author Jared Erickson
35+
*/
36+
class XmlMapReader implements MapReader {
37+
38+
@Override
39+
GMap read(String str) {
40+
XmlSlurper xmlSlurper = new XmlSlurper()
41+
def xml = xmlSlurper.parseText(str)
42+
GMap map = new GMap(
43+
width: getInt(xml.width?.text()?.toString(), 600),
44+
height: getInt(xml.height?.text()?.toString(), 400),
45+
type: xml.type?.text() ?: "png",
46+
backgroundColor: xml.backgroundColor?.text(),
47+
fixAspectRatio: getBoolean(xml.fixAspectRatio?.text(), true),
48+
layers: Renderables.getRenderables(getLayerMaps(xml.layers))
49+
)
50+
if (xml.proj?.text()) {
51+
map.proj = xml.proj.text()
52+
}
53+
if (xml.bounds?.text()) {
54+
Map bounds = [
55+
minX: xml.bounds.minX.text() as double,
56+
minY: xml.bounds.minY.text() as double,
57+
maxX: xml.bounds.maxX.text() as double,
58+
maxY: xml.bounds.maxY.text() as double
59+
]
60+
map.bounds = new Bounds(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, bounds?.proj)
61+
}
62+
map
63+
}
64+
65+
private int getInt(String s, int defaultValue) {
66+
if (s) {
67+
s.toInteger()
68+
} else {
69+
defaultValue
70+
}
71+
}
72+
73+
private boolean getBoolean(String s, boolean defaultValue) {
74+
if (s) {
75+
Boolean.parseBoolean(s)
76+
} else {
77+
defaultValue
78+
}
79+
}
80+
81+
private List<Map> getLayerMaps(def xml) {
82+
xml.children().collect { def layerDef ->
83+
Map layerMap = [:]
84+
layerDef.children().each { def layer ->
85+
layerMap[layer.name()] = layer.text()
86+
}
87+
layerMap
88+
}
89+
}
90+
91+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package geoscript.render.io
2+
3+
import geoscript.render.Map
4+
import org.junit.Test
5+
6+
import static org.junit.Assert.assertEquals
7+
8+
class XmlMapReaderTestCase {
9+
10+
@Test void readWithOptions() {
11+
MapReader mapReader = new XmlMapReader()
12+
String dir = new File("src/test/resources").absolutePath
13+
Map map = mapReader.read("""<map>
14+
<width>400</width>
15+
<height>400</height>
16+
<type>png</type>
17+
<proj>EPSG:4326</proj>
18+
<backgroundColor>blue</backgroundColor>
19+
<fixAspectRatio>true</fixAspectRatio>
20+
<layers>
21+
<layer>
22+
<layertype>layer</layertype>
23+
<file>${dir}/states.shp</file>
24+
</layer>
25+
</layers>
26+
<bounds>
27+
<minX>-135.911779</minX>
28+
<minY>36.993573</minY>
29+
<maxX>-96.536779</maxX>
30+
<maxY>51.405899</maxY>
31+
</bounds>
32+
</map>""")
33+
assertEquals(400, map.width)
34+
assertEquals(400, map.height)
35+
assertEquals("png", map.type)
36+
assertEquals("#0000ff", map.backgroundColor)
37+
assertEquals(true, map.fixAspectRatio)
38+
assertEquals(1, map.layers.size())
39+
map.render("target/map1.png")
40+
}
41+
42+
@Test void read() {
43+
MapReader mapReader = new XmlMapReader()
44+
String dir = new File("src/test/resources").absolutePath
45+
Map map = mapReader.read("""<map>
46+
<layers>
47+
<layer>
48+
<layertype>layer</layertype>
49+
<file>${dir}/states.shp</file>
50+
</layer>
51+
</layers>
52+
</map>""")
53+
assertEquals(600, map.width)
54+
assertEquals(400, map.height)
55+
assertEquals("png", map.type)
56+
assertEquals(null, map.backgroundColor)
57+
assertEquals(true, map.fixAspectRatio)
58+
assertEquals(1, map.layers.size())
59+
map.render("target/map2.png")
60+
}
61+
62+
}

0 commit comments

Comments
 (0)