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