view src/java/de/intevation/mxd/reader/PictureLineSymbolReader.java @ 351:eb0d89a97396 release-1.2

Updated Website to version 1.2, added download links
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 16 Nov 2012 18:28:26 +0100
parents a46adb3697fa
children
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
 *
 * This file is part of MXD2map.
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LICENCE.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 *
 * MXD2map has been developed on behalf of the
 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
 * by Intevation GmbH.
 *
 * Authors:
 * Raimund Renkert <raimund.renkert@intevation.de>
 * Bjoern Schilberg <bjoern.schilberg@intevation.de>
 * Stephan Holl <stephan.holl@intevation.de>
 */

package de.intevation.mxd.reader;

import java.awt.Color;
import java.awt.Image;

import org.apache.log4j.Logger;

import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.ILineSymbol;
import com.esri.arcgis.display.PictureLineSymbol;
import com.esri.arcgis.support.ms.stdole.Picture;
import com.esri.arcgis.display.RgbColor;

import org.w3c.dom.Element;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;

import org.apache.commons.codec.binary.Base64;
/**
 * Reads picture line symbol information.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class PictureLineSymbolReader
extends AbstractSymbolReader {

    /**
     * The logger.
     */
    private static final Logger logger =
        Logger.getLogger(SimpleLineSymbolReader.class);

    /**
     * Private member.
     */
    private PictureLineSymbol symbol;

    public PictureLineSymbolReader(ISymbol symbol)
    throws Exception {
        logger.debug("constructor()");
        if(symbol instanceof PictureLineSymbol) {
            this.symbol = (PictureLineSymbol)symbol;
        }
        else {
            throw new Exception("Not a PictureLineSymbol!");
        }
    }

    public PictureLineSymbolReader(ILineSymbol symbol)
    throws Exception {
        logger.debug("constructor()");
        if(symbol instanceof PictureLineSymbol) {
            this.symbol = (PictureLineSymbol)symbol;
        }
        else {
            throw new Exception("Not a PictureLineSymbol!");
        }
    }

    /**
     * Reads the symbol attributes.
     *
     * @return The XML node.
     */
    public Element read() {
        //TODO Read the picture from mxd and write it as base64 string to the
        //     XML Element.
        logger.debug("read()");
        Element symbolElement = util.addSymbol(parent);

        try {
            symbolElement.setAttribute("name", symbol.getNameString());
        }
        catch(IOException ioe) {
            logger.warn("Could not read name. Setting name to \"default\"");
            symbolElement.setAttribute("name", "default");
        }

        try {
            symbolElement.setAttribute(
                "offset",
                String.valueOf(symbol.getOffset()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read offset.");
        }

        try {
            symbolElement.setAttribute(
                "x_scale",
                String.valueOf(symbol.getXScale()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read x-scale.");
        }

        try {
            symbolElement.setAttribute(
                "y_scale",
                String.valueOf(symbol.getYScale()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read y-scale.");
        }

        try {
            symbolElement.setAttribute(
                "width",
                String.valueOf(symbol.getWidth()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read width.");
        }
        
        //Read the picture and convert to base64.
        try {
            Picture p = symbol.getPicture();
            Image i = p.toImage();
            if(i instanceof BufferedImage) {
                BufferedImage bi = (BufferedImage)i;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                //Get byte array from image.
                ImageIO.write(bi, "BMP", baos);
                Base64 encoder = new Base64();
                //encode in a base64 string
                String pict = encoder.encodeBase64String(baos.toByteArray());
                symbolElement.setAttribute("picture", pict);

                //Get transparent color.
                RgbColor c = new RgbColor();
                c.setRGB(symbol.getBitmapTransparencyColor().getRGB());
                Color transp = new Color (
                        c.getRed(),
                        c.getGreen(),
                        c.getBlue());
                symbolElement.setAttribute(
                    "transparent_color",
                    String.valueOf(transp.getRGB()));
            }
            else {
                logger.warn("Could not read image symbol.");
                return null;
            }
        }
        catch(IOException ioe) {
            logger.warn("Could not read picture.");
        }
        symbolElement.setAttribute("type", "line");
        symbolElement.setAttribute("style", "picture");

        return symbolElement;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)