001    //$HeadURL: https://sushibar/svn/deegree/base/trunk/resources/eclipse/svn_classfile_header_template.xml $
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    
037    package org.deegree.graphics.sld;
038    
039    import java.util.Iterator;
040    import java.util.LinkedList;
041    
042    import org.deegree.framework.log.ILogger;
043    import org.deegree.framework.log.LoggerFactory;
044    import org.deegree.graphics.sld.SLDFactory.ThresholdsBelongTo;
045    import org.deegree.model.filterencoding.FilterEvaluationException;
046    
047    /**
048     * <code>Categorize</code> encapsulates data from a categorize element in a RasterSymbolizer.
049     *
050     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
051     * @author last edited by: $Author:$
052     *
053     * @version $Revision:$, $Date:$
054     */
055    public class Categorize {
056    
057        private static final ILogger LOG = LoggerFactory.getLogger( Categorize.class );
058    
059        // private String fallbackValue;
060    
061        private LinkedList<Float> thresholds;
062    
063        private LinkedList<Long> values;
064    
065        private LinkedList<Boolean> opacities;
066    
067        private ThresholdsBelongTo thresholdsBelongTo;
068    
069        /**
070         *
071         */
072        public Categorize() {
073            // this.fallbackValue = fallbackValue;
074        }
075    
076        /**
077         * @param thresholds
078         */
079        public void setThresholds( LinkedList<ParameterValueType> thresholds ) {
080            this.thresholds = new LinkedList<Float>();
081            for ( ParameterValueType pvt : thresholds ) {
082                try {
083                    this.thresholds.add( Float.valueOf( ( pvt.evaluate( null ) ) ) );
084                } catch ( NumberFormatException e ) {
085                    LOG.logError( "A number in a threshold value of a RasterSymbolizer could not be parsed.", e );
086                } catch ( FilterEvaluationException e ) {
087                    LOG.logError( "A threshold value could not be parsed in a RasterSymbolizer.", e );
088                }
089            }
090        }
091    
092        /**
093         * @param values
094         */
095        public void setValues( LinkedList<ParameterValueType> values ) {
096            this.values = new LinkedList<Long>();
097            opacities = new LinkedList<Boolean>();
098    
099            for ( ParameterValueType pvt : values ) {
100                try {
101                    String s = pvt.evaluate( null );
102                    this.values.add( Long.valueOf( s.substring( 1 ), 16 ) );
103                    opacities.add( new Boolean( s.length() > 7 ) );
104                } catch ( FilterEvaluationException e ) {
105                    LOG.logError( "A color value could not be parsed in a RasterSymbolizer.", e );
106                }
107            }
108        }
109    
110        /**
111         * @param thresholdsBelongTo
112         */
113        public void setThresholdsBelongTo( ThresholdsBelongTo thresholdsBelongTo ) {
114            this.thresholdsBelongTo = thresholdsBelongTo;
115        }
116    
117        /**
118         * @param val
119         * @param opacity
120         *            the opacity to use if none has been set for the value
121         * @return a categorized value
122         */
123        public int categorize( float val, int opacity ) {
124            Iterator<Float> ts = thresholds.iterator();
125            Iterator<Long> vs = values.iterator();
126            Iterator<Boolean> os = opacities.iterator();
127    
128            // hope this is interpreted correctly.
129            // preceding: value is classified to our value, if value is >= threshold
130            // succeeding: value is classified to our value, if value is > threshold
131            boolean preceding = thresholdsBelongTo == SLDFactory.ThresholdsBelongTo.PRECEDING;
132    
133            float threshold = ts.next().floatValue();
134    
135            while ( ( preceding ? ( threshold < val ) : ( threshold <= val ) ) && ts.hasNext() ) {
136                threshold = ts.next().floatValue();
137                vs.next();
138                os.next();
139            }
140    
141            int res = vs.next().intValue();
142            if ( os.next().booleanValue() ) {
143                return res;
144            }
145            return res | opacity;
146        }
147    }