001 //$HeadURL: https://sushibar/svn/deegree/base/trunk/resources/eclipse/svn_classfile_header_template.xml $
002 /*---------------- FILE HEADER ------------------------------------------
003 This file is part of deegree.
004 Copyright (C) 2001-2008 by:
005 Department of Geography, University of Bonn
006 http://www.giub.uni-bonn.de/deegree/
007 lat/lon GmbH
008 http://www.lat-lon.de
009
010 This library is free software; you can redistribute it and/or
011 modify it under the terms of the GNU Lesser General Public
012 License as published by the Free Software Foundation; either
013 version 2.1 of the License, or (at your option) any later version.
014 This library is distributed in the hope that it will be useful,
015 but WITHOUT ANY WARRANTY; without even the implied warranty of
016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 Lesser General Public License for more details.
018 You should have received a copy of the GNU Lesser General Public
019 License along with this library; if not, write to the Free Software
020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021 Contact:
022
023 Andreas Poth
024 lat/lon GmbH
025 Aennchenstr. 19
026 53177 Bonn
027 Germany
028 E-Mail: poth@lat-lon.de
029
030 Prof. Dr. Klaus Greve
031 Department of Geography
032 University of Bonn
033 Meckenheimer Allee 166
034 53115 Bonn
035 Germany
036 E-Mail: greve@giub.uni-bonn.de
037 ---------------------------------------------------------------------------*/
038
039 package org.deegree.graphics.sld;
040
041 import java.util.Iterator;
042 import java.util.LinkedList;
043
044 import org.deegree.framework.log.ILogger;
045 import org.deegree.framework.log.LoggerFactory;
046 import org.deegree.graphics.sld.SLDFactory.ThresholdsBelongTo;
047 import org.deegree.model.filterencoding.FilterEvaluationException;
048
049 /**
050 * <code>Categorize</code> encapsulates data from a categorize element in a RasterSymbolizer.
051 *
052 * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
053 * @author last edited by: $Author:$
054 *
055 * @version $Revision:$, $Date:$
056 */
057 public class Categorize {
058
059 private static final ILogger LOG = LoggerFactory.getLogger( Categorize.class );
060
061 // private String fallbackValue;
062
063 private LinkedList<Float> thresholds;
064
065 private LinkedList<Long> values;
066
067 private LinkedList<Boolean> opacities;
068
069 private ThresholdsBelongTo thresholdsBelongTo;
070
071 /**
072 *
073 */
074 public Categorize() {
075 // this.fallbackValue = fallbackValue;
076 }
077
078 /**
079 * @param thresholds
080 */
081 public void setThresholds( LinkedList<ParameterValueType> thresholds ) {
082 this.thresholds = new LinkedList<Float>();
083 for ( ParameterValueType pvt : thresholds ) {
084 try {
085 this.thresholds.add( Float.valueOf( ( pvt.evaluate( null ) ) ) );
086 } catch ( NumberFormatException e ) {
087 LOG.logError( "A number in a threshold value of a RasterSymbolizer could not be parsed.", e );
088 } catch ( FilterEvaluationException e ) {
089 LOG.logError( "A threshold value could not be parsed in a RasterSymbolizer.", e );
090 }
091 }
092 }
093
094 /**
095 * @param values
096 */
097 public void setValues( LinkedList<ParameterValueType> values ) {
098 this.values = new LinkedList<Long>();
099 opacities = new LinkedList<Boolean>();
100
101 for ( ParameterValueType pvt : values ) {
102 try {
103 String s = pvt.evaluate( null );
104 this.values.add( Long.valueOf( s.substring( 1 ), 16 ) );
105 opacities.add( new Boolean( s.length() > 7 ) );
106 } catch ( FilterEvaluationException e ) {
107 LOG.logError( "A color value could not be parsed in a RasterSymbolizer.", e );
108 }
109 }
110 }
111
112 /**
113 * @param thresholdsBelongTo
114 */
115 public void setThresholdsBelongTo( ThresholdsBelongTo thresholdsBelongTo ) {
116 this.thresholdsBelongTo = thresholdsBelongTo;
117 }
118
119 /**
120 * @param val
121 * @param opacity
122 * the opacity to use if none has been set for the value
123 * @return a categorized value
124 */
125 public int categorize( float val, int opacity ) {
126 Iterator<Float> ts = thresholds.iterator();
127 Iterator<Long> vs = values.iterator();
128 Iterator<Boolean> os = opacities.iterator();
129
130 // hope this is interpreted correctly.
131 // preceding: value is classified to our value, if value is >= threshold
132 // succeeding: value is classified to our value, if value is > threshold
133 boolean preceding = thresholdsBelongTo == SLDFactory.ThresholdsBelongTo.PRECEDING;
134
135 float threshold = ts.next().floatValue();
136
137 while ( ( preceding ? ( threshold < val ) : ( threshold <= val ) ) && ts.hasNext() ) {
138 threshold = ts.next().floatValue();
139 vs.next();
140 os.next();
141 }
142
143 int res = vs.next().intValue();
144 if ( os.next().booleanValue() ) {
145 return res;
146 }
147 return res | opacity;
148 }
149 }