001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.svg;
018:
019: import java.awt.Dimension;
020: import java.awt.Rectangle;
021: import java.awt.geom.AffineTransform;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.OutputStreamWriter;
025: import java.util.logging.Logger;
026:
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.parsers.FactoryConfigurationError;
030: import javax.xml.parsers.ParserConfigurationException;
031:
032: import org.apache.batik.svggen.SVGGeneratorContext;
033: import org.apache.batik.svggen.SVGGraphics2D;
034: import org.geotools.map.MapContext;
035: import org.geotools.renderer.lite.StreamingRenderer;
036: import org.w3c.dom.Document;
037:
038: import com.vividsolutions.jts.geom.Envelope;
039:
040: /**
041: * This is a simple support class which allows you to generate an SVG file from a map.
042: *
043: * To use, setup a Map object with the layers you want to render, create an envelope for the
044: * region to be drawn and pass in an OutputStream (probably attached to a new file) for the
045: * resulting SVG information to be stored in.
046: *
047: * Optionaly you can change the default size of the SVG cavas (in effect increasing the resolution)
048: * by calling setCavasSize before calling go.
049: *
050: * @author James Macgill, PennState
051: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/svgsupport/src/main/java/org/geotools/svg/GenerateSVG.java $
052: * @version $Id: GenerateSVG.java 27862 2007-11-12 19:51:19Z desruisseaux $
053: */
054: public class GenerateSVG {
055: private static Logger LOGGER = org.geotools.util.logging.Logging
056: .getLogger("org.geotools.svgsupport");
057: private Dimension canvasSize = new Dimension(300, 300);
058:
059: /**
060: * Creates a new instance of GenerateSVG.
061: */
062: public GenerateSVG() {
063: }
064:
065: /** Generate an SVG document from the supplied information.
066: * Note, call setCavasSize first if you want to change the default output size.
067: * @param map Contains the layers (features + styles) to be rendered
068: * @param env The portion of the map to generate an SVG from
069: * @param out Stream to write the resulting SVG out to (probable should be a new file)
070: * @throws IOException Should anything go wrong whilst writing to 'out'
071: * @throws ParserConfigurationException If critical XML tools are missing from the classpath
072: */
073: public void go(MapContext map, Envelope env, OutputStream out)
074: throws IOException, ParserConfigurationException {
075: SVGGeneratorContext ctx = setupContext();
076: ctx
077: .setComment("Generated by GeoTools2 with Batik SVG Generator");
078:
079: SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);
080:
081: g2d.setSVGCanvasSize(getCanvasSize());
082:
083: renderMap(map, env, g2d);
084: LOGGER.finest("writing to file");
085: OutputStreamWriter osw = null;
086: try {
087: osw = new OutputStreamWriter(out, "UTF-8");
088: g2d.stream(osw);
089: } finally {
090: if (osw != null)
091: osw.close();
092: }
093:
094: }
095:
096: /**
097: * DOCUMENT ME!
098: *
099: * @param map
100: * @param env
101: * @param g2d
102: */
103: private void renderMap(final MapContext map, final Envelope env,
104: final SVGGraphics2D g2d) throws IOException {
105: StreamingRenderer renderer = new StreamingRenderer();
106: renderer.setContext(map);
107:
108: Rectangle outputArea = new Rectangle(g2d.getSVGCanvasSize());
109: Envelope dataArea = map.getLayerBounds();
110:
111: LOGGER.finest("rendering map");
112: renderer.paint(g2d, outputArea, dataArea);
113: }
114:
115: /**
116: * DOCUMENT ME!
117: *
118: * @return
119: *
120: * @throws FactoryConfigurationError
121: * @throws ParserConfigurationException
122: */
123: private SVGGeneratorContext setupContext()
124: throws FactoryConfigurationError,
125: ParserConfigurationException {
126: Document document = null;
127:
128: DocumentBuilderFactory dbf = DocumentBuilderFactory
129: .newInstance();
130: DocumentBuilder db = dbf.newDocumentBuilder();
131:
132: // Create an instance of org.w3c.dom.Document
133: document = db.getDOMImplementation().createDocument(null,
134: "svg", null);
135:
136: // Set up the context
137: SVGGeneratorContext ctx = SVGGeneratorContext
138: .createDefault(document);
139:
140: return ctx;
141: }
142:
143: public Dimension getCanvasSize() {
144: return this .canvasSize;
145: }
146:
147: public void setCanvasSize(final Dimension size) {
148: this.canvasSize = size;
149: }
150: }
|