001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/displayelements/ScaledFeature.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.util.HashMap;
039    import java.util.Map;
040    
041    import org.deegree.datatypes.QualifiedName;
042    import org.deegree.datatypes.Types;
043    import org.deegree.io.datastore.PropertyPathResolvingException;
044    import org.deegree.model.feature.Feature;
045    import org.deegree.model.feature.FeatureFactory;
046    import org.deegree.model.feature.FeatureProperty;
047    import org.deegree.model.feature.schema.FeatureType;
048    import org.deegree.model.feature.schema.PropertyType;
049    import org.deegree.model.spatialschema.Envelope;
050    import org.deegree.model.spatialschema.Geometry;
051    import org.deegree.model.spatialschema.GeometryException;
052    import org.deegree.ogcbase.PropertyPath;
053    
054    /**
055     * This class is a wrapper for a Feature and a Feature itself.
056     * <p>
057     * It adds a special behavior/property to a feature that is required by deegree DisplayElements. This special behavior
058     * is an additional property named "$SCALE". In opposite to conventional properties this one can change its value during
059     * lifetime of a feature without changing the underlying feature itself. <br>
060     * The class is use to offer users the opportunity to use the scale of a map within expressions embedded in SLD
061     * rules/symbolizers, i.e. this enables a user to define that a symbol shall appear in 10m size independ of a map's
062     * scale.
063     *
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @author last edited by: $Author: apoth $
066     *
067     * @version $Revision: 27102 $, $Date: 2010-09-30 14:16:00 +0200 (Do, 30 Sep 2010) $
068     */
069    public class ScaledFeature implements Feature {
070    
071        private Feature feature;
072    
073        private FeatureType ft;
074    
075        private FeatureProperty[] props;
076    
077        private Map<String, String> attributeMap = new HashMap<String, String>();
078    
079        /**
080         *
081         * @param feature
082         *            feature wrap
083         * @param scale
084         *            maps scale (should be -1 if not known)
085         */
086        public ScaledFeature( Feature feature, double scale ) {
087            this.feature = feature;
088            PropertyType[] ftp = feature.getFeatureType().getProperties();
089            PropertyType[] ftp2 = new PropertyType[ftp.length + 1];
090            for ( int i = 0; i < ftp.length; i++ ) {
091                ftp2[i] = ftp[i];
092            }
093            QualifiedName qn = new QualifiedName( feature.getName().getPrefix(), "$SCALE", feature.getName().getNamespace() );
094            ftp2[ftp2.length - 1] = FeatureFactory.createSimplePropertyType( qn, Types.DOUBLE, false );
095            FeatureProperty[] o = feature.getProperties();
096            props = new FeatureProperty[o.length + 1];
097            for ( int i = 0; i < o.length; i++ ) {
098                props[i] = o[i];
099            }
100            props[props.length - 1] = FeatureFactory.createFeatureProperty( qn, new Double( scale ) );
101            ft = FeatureFactory.createFeatureType( feature.getFeatureType().getName(), false, ftp2 );
102        }
103    
104        /**
105         * @return features owner
106         */
107        public FeatureProperty getOwner() {
108            return feature.getOwner();
109        }
110    
111        /**
112         * @return feature description
113         */
114        public String getDescription() {
115            return feature.getDescription();
116        }
117    
118        /**
119         * @return features name
120         */
121        public QualifiedName getName() {
122            return feature.getName();
123        }
124    
125        /**
126         * @see Feature#getDefaultGeometryPropertyValue()
127         */
128        public Geometry getDefaultGeometryPropertyValue() {
129            return feature.getDefaultGeometryPropertyValue();
130        }
131    
132        /**
133         * @return features envelope
134         */
135        public Envelope getBoundedBy()
136                                throws GeometryException {
137            return feature.getBoundedBy();
138        }
139    
140        /**
141         * @see Feature#getFeatureType() the returned feature type contains all properties of the wrapped feature plus a
142         *      property named '$SCALE'
143         */
144        public FeatureType getFeatureType() {
145            return ft;
146        }
147    
148        /**
149         * @see Feature#getGeometryPropertyValues()
150         */
151        public Geometry[] getGeometryPropertyValues() {
152            return feature.getGeometryPropertyValues();
153        }
154    
155        /**
156         * @see Feature#getId()
157         */
158        public String getId() {
159            return feature.getId();
160        }
161    
162        /**
163         * @see Feature#getProperties() the returned array contains all properties of the wrapped feature plus a property
164         *      named '$SCALE'
165         */
166        public FeatureProperty[] getProperties() {
167            return props;
168        }
169    
170        /**
171         * The property '$SCALE' has the highest valid index
172         */
173        public FeatureProperty[] getProperties( int index ) {
174            return new FeatureProperty[] { props[index] };
175        }
176    
177        /**
178         * use '$SCALE' to access the scale property value
179         */
180        public FeatureProperty getDefaultProperty( QualifiedName name ) {
181            QualifiedName qn = new QualifiedName( "$SCALE" );
182            if ( name.equals( qn ) ) {
183                return props[props.length - 1];
184            }
185            return feature.getDefaultProperty( name );
186        }
187    
188        /**
189         * @param name
190         * @return property array
191         */
192        public FeatureProperty[] getProperties( QualifiedName name ) {
193            if ( name.getLocalName().equalsIgnoreCase( "$SCALE" ) ) {
194                return new FeatureProperty[] { props[props.length - 1] };
195            }
196            return feature.getProperties( name );
197        }
198    
199        /**
200         * @param path
201         * @return property
202         */
203        public FeatureProperty getDefaultProperty( PropertyPath path )
204                                throws PropertyPathResolvingException {
205            if ( path.getStep( 0 ).getPropertyName().getLocalName().equalsIgnoreCase( "$SCALE" ) ) {
206                return props[props.length - 1];
207            }
208            return feature.getDefaultProperty( path );
209        }
210    
211        public void setProperty( FeatureProperty property, int index ) {
212            feature.setProperty( property, index );
213        }
214    
215        /**
216         * sets the features scale. Expected is the scale denominator as defined by OGC SLD specification
217         *
218         * @param scale
219         */
220        public void setScale( double scale ) {
221            // must be multiplied with pixel size to get scale as length
222            // of a pixels diagonal measured in meter
223            props[props.length - 1].setValue( scale * 0.00028 );
224        }
225    
226        /**
227         * returns the features scale
228         *
229         * @return the features scale
230         */
231        public double getScale() {
232            return ( (Double) props[props.length - 1].getValue() ).doubleValue();
233        }
234    
235        /**
236         * @param property
237         */
238        public void addProperty( FeatureProperty property ) {
239            this.feature.addProperty( property );
240        }
241    
242        /**
243         * @param propertyName
244         */
245        public void removeProperty( QualifiedName propertyName ) {
246            this.feature.removeProperty( propertyName );
247        }
248    
249        /**
250         * @param oldProperty
251         * @param newProperty
252         */
253        public void replaceProperty( FeatureProperty oldProperty, FeatureProperty newProperty ) {
254            this.feature.replaceProperty( oldProperty, newProperty );
255        }
256    
257        /**
258         * @param fid
259         */
260        public void setId( String fid ) {
261            feature.setId( fid );
262        }
263    
264        /**
265         * Returns the attribute value of the attribute with the specified name.
266         *
267         * @param name
268         *            name of the attribute
269         * @return the attribute value
270         */
271        public String getAttribute( String name ) {
272            return this.attributeMap.get( name );
273        }
274    
275        /**
276         * Returns all attributes of the feature.
277         *
278         * @return all attributes, keys are names, values are attribute values
279         */
280        public Map<String, String> getAttributes() {
281            return this.attributeMap;
282        }
283    
284        /**
285         * Sets the value of the attribute with the given name.
286         *
287         * @param name
288         *            name of the attribute
289         * @param value
290         *            value to set
291         */
292        public void setAttribute( String name, String value ) {
293            this.attributeMap.put( name, value );
294        }
295    
296        /**
297         * Sets the feature type of this feature.
298         *
299         * @param ft
300         *            feature type to set
301         */
302        public void setFeatureType( FeatureType ft ) {
303            feature.setFeatureType( ft );
304        }
305    
306        /*
307         * (non-Javadoc)
308         *
309         * @see org.deegree.model.feature.Feature#setEnvelopesUpdated()
310         */
311        public void setEnvelopesUpdated() {
312            feature.setEnvelopesUpdated();
313        }
314    
315        /*
316         * (non-Javadoc)
317         *
318         * @see org.deegree.model.feature.Feature#cloneDeep()
319         */
320        public Feature cloneDeep()
321                                throws CloneNotSupportedException {
322            Feature tmp = feature.cloneDeep();
323            return new ScaledFeature( tmp, getScale() );
324        }
325    
326        @Override
327        public Object clone()
328                                throws CloneNotSupportedException {
329            Feature tmp = (Feature) feature.clone();
330            return new ScaledFeature( tmp, getScale() );
331        }
332    
333    }