view src/java/de/intevation/mxd/reader/PictureMarkerSymbolReader.java @ 334:2e445968851f

* src/java/de/intevation/mxd/reader/WMSLayerReader.java: Do not write WMS Verstions in the map file that mapserver can not support
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 26 Oct 2012 12:45:42 +0200
parents 2cb2f26d0d54
children a46adb3697fa
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 org.apache.log4j.Logger;

import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.IMarkerSymbol;
import com.esri.arcgis.display.PictureMarkerSymbol;
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;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.awt.Color;

import org.apache.commons.codec.binary.Base64;

/**
 * Reads picture marker symbol information.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class PictureMarkerSymbolReader
extends AbstractSymbolReader {

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

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

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

    public PictureMarkerSymbolReader(IMarkerSymbol symbol)
    throws Exception {
        logger.debug("contructor()");
        if(symbol instanceof PictureMarkerSymbol) {
            this.symbol = (PictureMarkerSymbol)symbol;
        }
        else {
            throw new Exception("Not a PictureMarkerSymbol!");
        }
    }

    /**
     * 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(
                "angle",
                String.valueOf(symbol.getAngle()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read angle.");
        }

        try {
            symbolElement.setAttribute(
                "size",
                String.valueOf(symbol.getSize()));
        }
        catch (IOException ioe) {
            logger.warn("Could not read size. Setting size to 1.");
            symbolElement.setAttribute("size", "1");
        }

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

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

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

        //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("style", "picture");
        symbolElement.setAttribute("type", "marker");
        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)