    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/graphics/displayelements/PointDisplayElement.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005     Department of Geography, University of Bonn
    006     and
    007     lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035     ----------------------------------------------------------------------------*/
    036    package org.deegree.graphics.displayelements;
    037
    038    import java.awt.Color;
    039    import java.awt.Graphics;
    040    import java.awt.Graphics2D;
    041    import java.awt.Image;
    042    import java.awt.image.BufferedImage;
    043
    044    import org.deegree.framework.log.ILogger;
    045    import org.deegree.framework.log.LoggerFactory;
    046    import org.deegree.graphics.sld.PointSymbolizer;
    047    import org.deegree.graphics.sld.Symbolizer;
    048    import org.deegree.graphics.transformation.GeoTransform;
    049    import org.deegree.model.feature.Feature;
    050    import org.deegree.model.filterencoding.FilterEvaluationException;
    051    import org.deegree.model.spatialschema.Envelope;
    052    import org.deegree.model.spatialschema.MultiPoint;
    053    import org.deegree.model.spatialschema.Point;
    054    import org.deegree.model.spatialschema.Position;
    055
    056    /**
    057     * DisplayElement that encapsulates a point geometry (<tt>GM_Point</tt>) and a <tt>PointSymbolizer</tt>.
    058     * <p>
    059     *
    060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    061     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
    062     * @version $Revision: 20198 $ $Date: 2009-10-20 09:18:00 +0200 (Di, 20. Okt 2009) $
    063     */
    064    class PointDisplayElement extends GeometryDisplayElement {
    065
    066        private static ILogger LOG = LoggerFactory.getLogger( PointDisplayElement.class );
    067
    068        /** Use serialVersionUID for interoperability. */
    069        private final static long serialVersionUID = -2979559276151855757L;
    070
    071        private transient static Image defaultImg = new BufferedImage( 7, 7, BufferedImage.TYPE_INT_ARGB );
    072
    073        static {
    074            Graphics g = defaultImg.getGraphics();
    075            g.setColor( Color.LIGHT_GRAY );
    076            g.fillRect( 0, 0, 9, 9 );
    077            g.dispose();
    078        }
    079
    080        /**
    081         * Creates a new PointDisplayElement_Impl object.
    082         *
    083         * @param feature
    084         * @param geometry
    085         */
    086        public PointDisplayElement( Feature feature, Point geometry ) {
    087            super( feature, geometry, null );
    088
    089            Symbolizer defaultSymbolizer = new PointSymbolizer();
    090            this.setSymbolizer( defaultSymbolizer );
    091        }
    092
    093        /**
    094         * Creates a new PointDisplayElement_Impl object.
    095         *
    096         * @param feature
    097         * @param geometry
    098         * @param symbolizer
    099         */
    100        public PointDisplayElement( Feature feature, Point geometry, PointSymbolizer symbolizer ) {
    101            super( feature, geometry, symbolizer );
    102        }
    103
    104        /**
    105         * Creates a new PointDisplayElement object.
    106         *
    107         * @param feature
    108         * @param geometry
    109         */
    110        public PointDisplayElement( Feature feature, MultiPoint geometry ) {
    111            super( feature, geometry, null );
    112
    113            Symbolizer defaultSymbolizer = new PointSymbolizer();
    114            this.setSymbolizer( defaultSymbolizer );
    115        }
    116
    117        /**
    118         * Creates a new PointDisplayElement object.
    119         *
    120         * @param feature
    121         * @param geometry
    122         * @param symbolizer
    123         */
    124        public PointDisplayElement( Feature feature, MultiPoint geometry, PointSymbolizer symbolizer ) {
    125            super( feature, geometry, symbolizer );
    126        }
    127
    128        /**
    129         * renders the DisplayElement to the submitted graphic context
    130         */
    131        public void paint( Graphics g, GeoTransform projection, double scale ) {
    132            synchronized ( symbolizer ) {
    133                ( (ScaledFeature) feature ).setScale( scale );
    134                try {
    135                    Image image = defaultImg;
    136
    137                    if ( ( (PointSymbolizer) symbolizer ).getGraphic() != null ) {
    138                        image = ( (PointSymbolizer) symbolizer ).getGraphic().getAsImage( feature );
    139                    }
    140                    Graphics2D g2D = (Graphics2D) g;
    141
    142                    if ( geometry instanceof Point ) {
    143                        drawPoint( g2D, (Point) geometry, projection, image );
    144                    } else {
    145                        MultiPoint mp = (MultiPoint) geometry;
    146
    147                        for ( int i = 0; i < mp.getSize(); i++ ) {
    148                            drawPoint( g2D, mp.getPointAt( i ), projection, image );
    149                        }
    150                    }
    151                } catch ( FilterEvaluationException e ) {
    152                    LOG.logError( "Exception caught evaluating an Expression!", e );
    153                }
    154            }
    155        }
    156
    157        /**
    158         * renders one point to the submitted graphic context considering the also submitted projection
    159         */
    160        private void drawPoint( Graphics2D g, Point point, GeoTransform projection, Image image ) {
    161            Envelope destSize = projection.getDestRect();
    162            Position source = point.getPosition();
    163            int x = (int) ( projection.getDestX( source.getX() ) + 0.5 );
    164            int y = (int) ( projection.getDestY( source.getY() ) + 0.5 );
    165
    166            int x_ = x - ( image.getWidth( null ) >> 1 );
    167            int y_ = y - ( image.getHeight( null ) >> 1 );
    168
    169            int dx = Math.min( image.getWidth( null ), (int) destSize.getWidth() - x_ );
    170            int dy = Math.min( image.getHeight( null ), (int) destSize.getHeight() - y_ );
    171            int tx = Math.min( (int) destSize.getWidth(), x_ + image.getWidth( null ) );
    172            int ty = Math.min( (int) destSize.getHeight(), y_ + image.getHeight( null ) );
    173
    174            g.drawImage( image, x_, y_, tx, ty, 0, 0, dx, dy, null );
    175        }
    176    }