[OpenMap Users] SVG Map Output with Batik

From: Cary O'Brien <cobrien_at_cornet.com>
Date: Thu, 09 Jun 2005 09:37:28 -0400

I've been playing around with SVG for a few days, and was trying to create
some SVG maps. I thought about creating one from shapefiles, and started
thinking about what methods I would need to create an SVG file from
objects -- methods like fill, line, shape, and realized that these were
the Graphics2D methods, and if I implemented a Graphics2D that generated
SVG I could simply paint() an openmap map bean.

Well, after a little bit of googling I found out that the Batik project
has an SVGGraphics2D class that does exactly this!
This makes generating an SVG map from a map bean a 7line process. I'm
attaching a sample
executable (you need data/shape/... available). The key is the
SVGExport method. The code
has undergone an exhaustive 5-minute test, so use at your own risk.

Once you have the SVG map, you can display it with the Batik viewer
(squiggle), or edit it
with something like GLIPS (http://glipssvgeditor.sourceforge.net/). To
get at the shapes,
you need to ungroup, throw away the background, and ungroup again, but
after that you can
play contential drift and drag the US over here and Brazil over there.

Anyhow, I thought this was kind of neat.

-- cary

package svgmap;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Properties;
import javax.swing.*;
import java.io.*;

import com.bbn.openmap.MapBean;
import com.bbn.openmap.layer.shape.ShapeLayer;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;

 * This is a simple application that uses the OpenMap MapBean to show
 * a map.
 * <p>
 * This example shows:
 * <ul>
 * <li>MapBean
 * <li>ShapeLayer with political data
 * </ul>
public class SVGMap {
    public static void main(String args[]) {
        // Create a Swing frame
        final JFrame frame = new JFrame("Simple Map");
        frame.getContentPane().setLayout(new java.awt.BorderLayout());
        // Size the frame appropriately
        frame.setSize(640, 480);
        // Create a MapBean
        final MapBean mapBean = new MapBean();
        // Create a ShapeLayer to show world political boundaries.
        // Set the properties of the layer. This assumes that the
        // datafiles "dcwpo-browse.shp" and "dcwpo-browse.ssx" are in
        // a path specified in the CLASSPATH variable. These files
        // are distributed with OpenMap and reside in the toplevel
        // "share" subdirectory.
        ShapeLayer shapeLayer = new ShapeLayer();
        Properties shapeLayerProps = new Properties();
        shapeLayerProps.put("prettyName", "Political Solid");
        shapeLayerProps.put("lineColor", "000000");
        shapeLayerProps.put("fillColor", "BDDE83");
        shapeLayerProps.put("shapeFile", "data/shape/dcwpo-browse.shp");
        shapeLayerProps.put("spatialIndex", "data/shape/dcwpo-browse.ssx");
        // Add the political layer to the map
        // Add the map to the frame
        // Add a big output button.
        JButton export = new JButton("Export to SVG");
        export.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                File outfile = new File("map.xml");
                try {
                    SVGExport(mapBean, new FileOutputStream(outfile));
                    JOptionPane.showMessageDialog(frame, "Wrote " +
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(frame, "Error " +
        JPanel bp = new JPanel();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
        // Display the frame
     * Export a map bean to svg. Batik makes this really easy,
     * we just need to render it onto a SVGGraphics.
    public static void SVGExport(MapBean mapBean, OutputStream svgOut)
throws IOException {
        // Get a DOMImplementation
        DOMImplementation domImpl =
        // Create an instance of org.w3c.dom.Document
        org.w3c.dom.Document document = domImpl.createDocument(null,
"svg", null);
        // Create an instance of the SVG Generator
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        // Ask the test to render into the SVG Graphics2D implementation
        // Finally, stream out SVG to the standard output using UTF-8
        // character to byte encoding
        boolean useCSS = true; // we want to use CSS style attribute
        Writer out = new OutputStreamWriter(svgOut, "UTF-8");
        svgGenerator.stream(out, useCSS);

[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Thu Jun 09 2005 - 09:58:04 EDT

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:07 EDT