|
| 1 | +package geoscript.carto.io |
| 2 | + |
| 3 | +import geoscript.carto.CartoBuilder |
| 4 | +import geoscript.carto.CartoFactories |
| 5 | +import geoscript.carto.DateTextItem |
| 6 | +import geoscript.carto.GridItem |
| 7 | +import geoscript.carto.HorizontalAlign |
| 8 | +import geoscript.carto.ImageItem |
| 9 | +import geoscript.carto.LegendItem |
| 10 | +import geoscript.carto.LineItem |
| 11 | +import geoscript.carto.MapItem |
| 12 | +import geoscript.carto.NorthArrowItem |
| 13 | +import geoscript.carto.NorthArrowStyle |
| 14 | +import geoscript.carto.OverviewMapItem |
| 15 | +import geoscript.carto.PageSize |
| 16 | +import geoscript.carto.ParagraphItem |
| 17 | +import geoscript.carto.RectangleItem |
| 18 | +import geoscript.carto.ScaleBarItem |
| 19 | +import geoscript.carto.ScaleTextItem |
| 20 | +import geoscript.carto.TableItem |
| 21 | +import geoscript.carto.TextItem |
| 22 | +import geoscript.carto.VerticalAlign |
| 23 | +import geoscript.filter.Color |
| 24 | +import geoscript.render.io.JsonMapReader |
| 25 | +import groovy.json.JsonSlurper |
| 26 | + |
| 27 | +import java.awt.Font |
| 28 | +import java.text.DateFormat |
| 29 | +import java.text.SimpleDateFormat |
| 30 | + |
| 31 | +/** |
| 32 | + * Read a CartoBuilder from a JSON String |
| 33 | + * @author Jared Erickson |
| 34 | + */ |
| 35 | +class JsonCartoReader implements CartoReader { |
| 36 | + |
| 37 | + @Override |
| 38 | + CartoBuilder read(String str) { |
| 39 | + JsonSlurper jsonSlurper = new JsonSlurper() |
| 40 | + Map config = jsonSlurper.parseText(str) |
| 41 | + |
| 42 | + JsonMapReader mapReader = new JsonMapReader() |
| 43 | + Map mapItems = [:] |
| 44 | + CartoBuilder cartoBuilder = CartoFactories.findByName(config.type) |
| 45 | + .create(new PageSize(config.width, config.height)) |
| 46 | + config.items.each { Map item -> |
| 47 | + String itemType = item.type |
| 48 | + if (itemType.equalsIgnoreCase("rectangle")) { |
| 49 | + RectangleItem rectangleItem = new RectangleItem( |
| 50 | + item.x, item.y, item.width, item.height |
| 51 | + ) |
| 52 | + if (item.strokeColor) { |
| 53 | + rectangleItem.strokeColor(new Color(item.strokeColor).asColor()) |
| 54 | + } |
| 55 | + if (item.fillColor) { |
| 56 | + rectangleItem.fillColor(new Color(item.fillColor).asColor()) |
| 57 | + } |
| 58 | + if (item.strokeWidth) { |
| 59 | + rectangleItem.strokeWidth(item.strokeWidth as float) |
| 60 | + } |
| 61 | + cartoBuilder.rectangle(rectangleItem) |
| 62 | + } else if (itemType.equalsIgnoreCase("dateText")) { |
| 63 | + DateTextItem dateTextItem = new DateTextItem( |
| 64 | + item.x, item.y, item.width, item.height |
| 65 | + ) |
| 66 | + if (item.format) { |
| 67 | + dateTextItem.format(item.format) |
| 68 | + } |
| 69 | + if (item.date) { |
| 70 | + DateFormat dateFormat = new SimpleDateFormat(dateTextItem.format) |
| 71 | + dateTextItem.date(dateFormat.parse(item.date)) |
| 72 | + } |
| 73 | + if (item.color) { |
| 74 | + dateTextItem.color(new Color(item.color).asColor()) |
| 75 | + } |
| 76 | + if (item.font) { |
| 77 | + dateTextItem.font(getFont(item.font)) |
| 78 | + } |
| 79 | + if (item.horizontalAlign) { |
| 80 | + dateTextItem.horizontalAlign(HorizontalAlign.valueOf(item.horizontalAlign.toString().toUpperCase())) |
| 81 | + } |
| 82 | + if (item.verticalAlign) { |
| 83 | + dateTextItem.verticalAlign(VerticalAlign.valueOf(item.verticalAlign.toString().toUpperCase())) |
| 84 | + } |
| 85 | + cartoBuilder.dateText(dateTextItem) |
| 86 | + } else if (itemType.equalsIgnoreCase("image")) { |
| 87 | + ImageItem imageItem = new ImageItem(item.x, item.y, item.width, item.height) |
| 88 | + imageItem.path(new File(item.path).absoluteFile) |
| 89 | + cartoBuilder.image(imageItem) |
| 90 | + } else if (itemType.equalsIgnoreCase("line")) { |
| 91 | + LineItem lineItem = new LineItem(item.x, item.y, item.width, item.height) |
| 92 | + if (item.strokeColor) { |
| 93 | + lineItem.strokeColor(new Color(item.strokeColor).asColor()) |
| 94 | + } |
| 95 | + if (item.strokeWidth) { |
| 96 | + lineItem.strokeWidth(item.strokeWidth as float) |
| 97 | + } |
| 98 | + cartoBuilder.line(lineItem) |
| 99 | + } else if (itemType.equalsIgnoreCase("map")) { |
| 100 | + MapItem mapItem = new MapItem(item.x, item.y, item.width, item.height) |
| 101 | + mapItem.map(mapReader.read(item)) |
| 102 | + mapItems[item.name] = mapItem |
| 103 | + cartoBuilder.map(mapItem) |
| 104 | + } else if (itemType.equalsIgnoreCase("overViewMap")) { |
| 105 | + OverviewMapItem overviewMapItem = new OverviewMapItem(item.x, item.y, item.width, item.height) |
| 106 | + overviewMapItem.overviewMap(mapReader.read(item)) |
| 107 | + overviewMapItem.linkedMap(mapItems[item.linkedMap].map) |
| 108 | + if (item.zoomIntoBounds) { |
| 109 | + overviewMapItem.zoomIntoBounds(item.zoomIntoBounds) |
| 110 | + } |
| 111 | + if (item.scaleFactor) { |
| 112 | + overviewMapItem.scaleFactor(item.scaleFactor) |
| 113 | + } |
| 114 | + cartoBuilder.overViewMap(overviewMapItem) |
| 115 | + } else if (itemType.equalsIgnoreCase("northArrow")) { |
| 116 | + NorthArrowItem northArrowItem = new NorthArrowItem(item.x, item.y, item.width, item.height) |
| 117 | + if (item.style) { |
| 118 | + northArrowItem.style(NorthArrowStyle.valueOf(item.style.toString())) |
| 119 | + } |
| 120 | + if (item.fillColor1) { |
| 121 | + northArrowItem.fillColor1(new Color(item.fillColor1).asColor()) |
| 122 | + } |
| 123 | + if (item.fillColor2) { |
| 124 | + northArrowItem.fillColor2(new Color(item.fillColor2).asColor()) |
| 125 | + } |
| 126 | + if (item.strokeColor1) { |
| 127 | + northArrowItem.strokeColor1(new Color(item.strokeColor1).asColor()) |
| 128 | + } |
| 129 | + if (item.strokeColor2) { |
| 130 | + northArrowItem.strokeColor2(new Color(item.strokeColor2).asColor()) |
| 131 | + } |
| 132 | + if (item.strokeWidth) { |
| 133 | + northArrowItem.strokeWidth(item.strokeWidth as float) |
| 134 | + } |
| 135 | + if (item.drawText != null) { |
| 136 | + northArrowItem.drawText(item.drawText) |
| 137 | + } |
| 138 | + if (item.font) { |
| 139 | + northArrowItem.font(getFont(item.font)) |
| 140 | + } |
| 141 | + if (item.textColor) { |
| 142 | + northArrowItem.textColor(new Color(item.textColor).asColor()) |
| 143 | + } |
| 144 | + cartoBuilder.northArrow(northArrowItem) |
| 145 | + } else if (itemType.equalsIgnoreCase("text")) { |
| 146 | + TextItem textItem = new TextItem(item.x, item.y, item.width, item.height) |
| 147 | + textItem.text(item.text) |
| 148 | + if (item.color) { |
| 149 | + textItem.color(new Color(item.color).asColor()) |
| 150 | + } |
| 151 | + if (item.font) { |
| 152 | + textItem.font(getFont(item.font)) |
| 153 | + } |
| 154 | + if (item.horizontalAlign) { |
| 155 | + textItem.horizontalAlign(HorizontalAlign.valueOf(item.horizontalAlign.toString().toUpperCase())) |
| 156 | + } |
| 157 | + if (item.verticalAlign) { |
| 158 | + textItem.verticalAlign(VerticalAlign.valueOf(item.verticalAlign.toString().toUpperCase())) |
| 159 | + } |
| 160 | + cartoBuilder.text(textItem) |
| 161 | + } else if (itemType.equalsIgnoreCase("paragraph")) { |
| 162 | + ParagraphItem paragraphItem = new ParagraphItem(item.x, item.y, item.width, item.height) |
| 163 | + paragraphItem.text(item.text) |
| 164 | + if (item.color) { |
| 165 | + paragraphItem.color(new Color(item.color).asColor()) |
| 166 | + } |
| 167 | + if (item.font) { |
| 168 | + paragraphItem.font(getFont(item.font)) |
| 169 | + } |
| 170 | + cartoBuilder.paragraph(paragraphItem) |
| 171 | + } else if (itemType.equalsIgnoreCase("scaleText")) { |
| 172 | + ScaleTextItem scaleTextItem = new ScaleTextItem(item.x, item.y, item.width, item.height) |
| 173 | + scaleTextItem.map(mapItems[item.map].map) |
| 174 | + if (item.format) { |
| 175 | + scaleTextItem.format(item.format) |
| 176 | + } |
| 177 | + if (item.prefixText) { |
| 178 | + scaleTextItem.prefixText(item.prefixText) |
| 179 | + } |
| 180 | + if (item.color) { |
| 181 | + scaleTextItem.color(new Color(item.color).asColor()) |
| 182 | + } |
| 183 | + if (item.font) { |
| 184 | + scaleTextItem.font(getFont(item.font)) |
| 185 | + } |
| 186 | + if (item.horizontalAlign) { |
| 187 | + scaleTextItem.horizontalAlign(HorizontalAlign.valueOf(item.horizontalAlign.toString().toUpperCase())) |
| 188 | + } |
| 189 | + if (item.verticalAlign) { |
| 190 | + scaleTextItem.verticalAlign(VerticalAlign.valueOf(item.verticalAlign.toString().toUpperCase())) |
| 191 | + } |
| 192 | + cartoBuilder.scaleText(scaleTextItem) |
| 193 | + } else if (itemType.equalsIgnoreCase("scaleBar")) { |
| 194 | + ScaleBarItem scaleBarItem = new ScaleBarItem(item.x, item.y, item.width, item.height) |
| 195 | + scaleBarItem.map(mapItems[item.map].map) |
| 196 | + if (item.strokeColor) { |
| 197 | + scaleBarItem.strokeColor(new Color(item.strokeColor).asColor()) |
| 198 | + } |
| 199 | + if (item.fillColor) { |
| 200 | + scaleBarItem.fillColor(new Color(item.fillColor).asColor()) |
| 201 | + } |
| 202 | + if (item.font) { |
| 203 | + scaleBarItem.font(getFont(item.font)) |
| 204 | + } |
| 205 | + if (item.strokeWidth) { |
| 206 | + scaleBarItem.strokeWidth(item.strokeWidth as float) |
| 207 | + } |
| 208 | + if (item.border) { |
| 209 | + scaleBarItem.border(item.border as int) |
| 210 | + } |
| 211 | + if (item.units) { |
| 212 | + scaleBarItem.units(ScaleBarItem.Units.valueOf(item.units.toString().toUpperCase())) |
| 213 | + } |
| 214 | + cartoBuilder.scaleBar(scaleBarItem) |
| 215 | + } else if (itemType.equalsIgnoreCase("grid")) { |
| 216 | + GridItem gridItem = new GridItem(item.x, item.y, item.width, item.height) |
| 217 | + if (item.size) { |
| 218 | + gridItem.size(item.size as int) |
| 219 | + } |
| 220 | + if (item.strokeWidth) { |
| 221 | + gridItem.strokeWidth(item.strokeWidth as float) |
| 222 | + } |
| 223 | + if (item.strokeColor) { |
| 224 | + gridItem.strokeColor(new Color(item.strokeColor).asColor()) |
| 225 | + } |
| 226 | + cartoBuilder.grid(gridItem) |
| 227 | + } else if (itemType.equalsIgnoreCase("table")) { |
| 228 | + TableItem tableItem = new TableItem(item.x, item.y, item.width, item.height) |
| 229 | + tableItem.columns(item.columns.collect { it }) |
| 230 | + item.rows.each { Map row -> |
| 231 | + tableItem.row(row) |
| 232 | + } |
| 233 | + cartoBuilder.table(tableItem) |
| 234 | + } else if (itemType.equalsIgnoreCase("legend")) { |
| 235 | + LegendItem legendItem = new LegendItem(item.x, item.y, item.width, item.height) |
| 236 | + legendItem.addMap(mapItems[item.map].map) |
| 237 | + if (item.backgroundColor) { |
| 238 | + legendItem.backgroundColor(new Color(item.backgroundColor).asColor()) |
| 239 | + } |
| 240 | + if (item.title) { |
| 241 | + legendItem.title(item.title) |
| 242 | + } |
| 243 | + if (item.titleFont) { |
| 244 | + legendItem.titleFont(getFont(item.titleFont)) |
| 245 | + } |
| 246 | + if (item.titleColor) { |
| 247 | + legendItem.titleColor(new Color(item.titleColor).asColor()) |
| 248 | + } |
| 249 | + if (item.textFont) { |
| 250 | + legendItem.textFont(getFont(item.textFont)) |
| 251 | + } |
| 252 | + if (item.textColor) { |
| 253 | + legendItem.textColor(new Color(item.textColor).asColor()) |
| 254 | + } |
| 255 | + if (item.numberFormat) { |
| 256 | + legendItem.numberFormat(item.numberFormat) |
| 257 | + } |
| 258 | + if (item.legendEntryWidth) { |
| 259 | + legendItem.legendEntryWidth(item.legendEntryWidth as int) |
| 260 | + } |
| 261 | + if (item.legendEntryHeight) { |
| 262 | + legendItem.legendEntryHeight(item.legendEntryHeight as int) |
| 263 | + } |
| 264 | + if (item.gapBetweenEntries) { |
| 265 | + legendItem.gapBetweenEntries(item.gapBetweenEntries as int) |
| 266 | + } |
| 267 | + cartoBuilder.legend(legendItem) |
| 268 | + } |
| 269 | + } |
| 270 | + cartoBuilder |
| 271 | + } |
| 272 | + |
| 273 | + private Font getFont(Map fontItem) { |
| 274 | + String name = fontItem.name |
| 275 | + int style = Font.PLAIN |
| 276 | + if (fontItem.style.equalsIgnoreCase("bold")) { |
| 277 | + style = Font.BOLD |
| 278 | + } else if (fontItem.style.equalsIgnoreCase("italic")) { |
| 279 | + style = Font.ITALIC |
| 280 | + } |
| 281 | + int size = fontItem.size.toString().toInteger() |
| 282 | + new Font(name, style, size) |
| 283 | + } |
| 284 | + |
| 285 | +} |
0 commit comments