001 //$HeadURL: http://svn.wald.intevation.org/svn/deegree/base/trunk/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: 30389 $ $Date: 2011-04-07 10:21:00 +0200 (Thu, 07 Apr 2011) $
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 double[] dis = ( (PointSymbolizer) symbolizer ).getGraphic().getDisplacement( feature );
143 if ( geometry instanceof Point ) {
144 drawPoint( g2D, (Point) geometry, projection, image, dis );
145 } else {
146 MultiPoint mp = (MultiPoint) geometry;
147
148 for ( int i = 0; i < mp.getSize(); i++ ) {
149 drawPoint( g2D, mp.getPointAt( i ), projection, image, dis );
150 }
151 }
152 } catch ( FilterEvaluationException e ) {
153 LOG.logError( "Exception caught evaluating an Expression!", e );
154 }
155 }
156 }
157
158 /**
159 * renders one point to the submitted graphic context considering the also submitted projection
160 *
161 * @param g
162 * @param point
163 * @param projection
164 * @param image
165 * @param dis displacement
166 */
167 private void drawPoint( Graphics2D g, Point point, GeoTransform projection, Image image, double[] dis ) {
168 Envelope destSize = projection.getDestRect();
169 Position source = point.getPosition();
170 int x = (int) Math.round( projection.getDestX( source.getX() ) + 0.5 + dis[0] );
171 int y = (int) Math.round( projection.getDestY( source.getY() ) + 0.5 + dis[1] );
172
173 int x_ = x - ( image.getWidth( null ) >> 1 );
174 int y_ = y - ( image.getHeight( null ) >> 1 );
175
176 int dx = Math.min( image.getWidth( null ), (int) destSize.getWidth() - x_ );
177 int dy = Math.min( image.getHeight( null ), (int) destSize.getHeight() - y_ );
178 int tx = Math.min( (int) destSize.getWidth(), x_ + image.getWidth( null ) );
179 int ty = Math.min( (int) destSize.getHeight(), y_ + image.getHeight( null ) );
180
181 g.drawImage( image, x_, y_, tx, ty, 0, 0, dx, dy, null );
182 }
183 }