01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.vfny.geoserver.wms.responses.map.kml;
06:
07: import org.geotools.map.MapContext;
08: import org.geotools.map.MapLayer;
09: import org.geotools.xml.transform.TransformerBase;
10: import org.geotools.xml.transform.Translator;
11: import org.vfny.geoserver.wms.WMSMapContext;
12: import org.xml.sax.ContentHandler;
13: import org.xml.sax.helpers.AttributesImpl;
14: import java.util.Map;
15:
16: /**
17: * Encodes the legend for a map layer as a kml ScreenOverlay.
18: *
19: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
20: *
21: */
22: public class KMLLegendTransformer extends KMLTransformerBase {
23: WMSMapContext mapContext;
24:
25: public KMLLegendTransformer(WMSMapContext mapContext) {
26: this .mapContext = mapContext;
27: setNamespaceDeclarationEnabled(false);
28: }
29:
30: public Translator createTranslator(ContentHandler handler) {
31: return new KMLLegendTranslator(handler);
32: }
33:
34: class KMLLegendTranslator extends KMLTranslatorSupport {
35: public KMLLegendTranslator(ContentHandler contentHandler) {
36: super (contentHandler);
37: }
38:
39: /**
40: * Encodes a KML ScreenOverlay wihch depicts the legend of a map.
41: */
42: public void encode(Object o) throws IllegalArgumentException {
43: MapLayer mapLayer = (MapLayer) o;
44:
45: if (isStandAlone()) {
46: start("kml");
47: }
48:
49: start("ScreenOverlay");
50: element("name", "Legend");
51:
52: element("overlayXY", null, KMLUtils
53: .attributes(new String[] { "x", "0", "y", "0",
54: "xunits", "pixels", "yunits", "pixels" }));
55: element("screenXY", null, KMLUtils.attributes(new String[] {
56: "x", "10", "y", "20", "xunits", "pixels", "yunits",
57: "pixels" }));
58:
59: start("Icon");
60:
61: //reference the image as a remote wms call
62: element("href", KMLUtils.getLegendGraphicUrl(mapContext,
63: mapLayer, null));
64:
65: end("Icon");
66:
67: end("ScreenOverlay");
68:
69: if (isStandAlone()) {
70: end("kml");
71: }
72: }
73: }
74: }
|