|
| 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 | +} |
0 commit comments