Skip to content

Commit 10a4b0a

Browse files
committed
Read a Map from a json configuration string
1 parent 5ab1f43 commit 10a4b0a

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.json.JsonSlurper
7+
8+
/**
9+
* Read a Map from a JSON String.
10+
* <pre>
11+
* {
12+
* "width": 400,
13+
* "height": 400,
14+
* "type": "png",
15+
* "backgroundColor": "blue",
16+
* "proj": "EPSG:4326",
17+
* "bounds": {
18+
* "minX": -135.911779,
19+
* "minY": 36.993573,
20+
* "maxX": -96.536779,
21+
* "maxY": 51.405899
22+
* },
23+
* "layers": [
24+
* {"layertype": "layer", "file": "states.shp"}
25+
* ]
26+
* }
27+
* </pre>
28+
* @author Jared Erickson
29+
*/
30+
class JsonMapReader implements MapReader {
31+
32+
@Override
33+
GMap read(String str) {
34+
JsonSlurper jsonSlurper = new JsonSlurper()
35+
Map values = jsonSlurper.parseText(str)
36+
GMap map = new GMap(
37+
width: values.get("width", 600),
38+
height: values.get("height", 400),
39+
type: values.get("type", "png"),
40+
backgroundColor: values.get("backgroundColor"),
41+
fixAspectRatio: values.get("fixAspectRation", true),
42+
proj: values.get("proj"),
43+
layers: Renderables.getRenderables(values.get("layers"))
44+
)
45+
if (values.get("proj")) {
46+
map.proj = values.get("proj")
47+
}
48+
if (values.get("bounds")) {
49+
Map bounds = values.get("bounds")
50+
map.bounds = new Bounds(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, bounds?.proj)
51+
}
52+
map
53+
}
54+
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package geoscript.render.io
2+
3+
import geoscript.render.Map as GMap
4+
5+
/**
6+
* Read a Map from a configuration string.
7+
* @author Jared Erickson
8+
*/
9+
interface MapReader {
10+
11+
/**
12+
* Read a Map from a configuration string.
13+
* @param str The string
14+
* @return A Map
15+
*/
16+
GMap read(String str)
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
<html>
3+
<head>
4+
<title>package geoscript.render.io.*</title>
5+
</head>
6+
<body>
7+
<p>GeoScript's Render I/O API</p>
8+
</body>
9+
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package geoscript.render.io
2+
3+
import geoscript.render.Map
4+
import org.junit.Test
5+
import static org.junit.Assert.*
6+
7+
class JsonMapReaderTestCase {
8+
9+
@Test void readWithOptions() {
10+
MapReader mapReader = new JsonMapReader()
11+
String dir = new File("src/test/resources").absolutePath
12+
Map map = mapReader.read("""{
13+
"width": 400,
14+
"height": 400,
15+
"type": "png",
16+
"backgroundColor": "blue",
17+
"proj": "EPSG:4326",
18+
"bounds": {
19+
"minX": -135.911779,
20+
"minY": 36.993573,
21+
"maxX": -96.536779,
22+
"maxY": 51.405899
23+
},
24+
"layers": [
25+
{"layertype": "layer", "file": "${dir}/states.shp"}
26+
]
27+
}""")
28+
assertEquals(400, map.width)
29+
assertEquals(400, map.height)
30+
assertEquals("png", map.type)
31+
assertEquals("#0000ff", map.backgroundColor)
32+
assertEquals(1, map.layers.size())
33+
}
34+
35+
@Test void read() {
36+
MapReader mapReader = new JsonMapReader()
37+
String dir = new File("src/test/resources").absolutePath
38+
Map map = mapReader.read("""{
39+
"layers": [
40+
{"layertype": "layer", "file": "${dir}/states.shp"}
41+
]
42+
}""")
43+
assertEquals(600, map.width)
44+
assertEquals(400, map.height)
45+
assertEquals("png", map.type)
46+
assertEquals(1, map.layers.size())
47+
}
48+
}

0 commit comments

Comments
 (0)