svn-gvsig-desktop / trunk / extensions / extWMS / src / com / iver / cit / gvsig / fmap / layers / DefaultDimension.java @ 9075
History | View | Annotate | Download (8.21 KB)
1 | 3746 | jaume | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | |||
42 | /* CVS MESSAGES:
|
||
43 | *
|
||
44 | * $Id$
|
||
45 | * $Log$
|
||
46 | 4222 | jaume | * Revision 1.5 2006-02-28 15:25:14 jaume
|
47 | 3805 | jaume | * *** empty log message ***
|
48 | *
|
||
49 | 4222 | jaume | * Revision 1.3.2.4 2006/02/10 13:22:35 jaume
|
50 | * now analyzes dimensions on demand
|
||
51 | *
|
||
52 | * Revision 1.3.2.3 2006/01/31 16:25:24 jaume
|
||
53 | * correcciones de bugs
|
||
54 | *
|
||
55 | * Revision 1.4 2006/01/26 16:07:14 jaume
|
||
56 | * *** empty log message ***
|
||
57 | *
|
||
58 | 3805 | jaume | * Revision 1.3.2.1 2006/01/26 12:59:32 jaume
|
59 | * 0.5
|
||
60 | *
|
||
61 | * Revision 1.3 2006/01/25 09:08:53 jaume
|
||
62 | 3756 | jaume | * test save and reload project
|
63 | *
|
||
64 | * Revision 1.2 2006/01/24 14:36:33 jaume
|
||
65 | 3746 | jaume | * This is the new version
|
66 | *
|
||
67 | * Revision 1.1.2.1 2006/01/20 15:22:46 jaume
|
||
68 | * *** empty log message ***
|
||
69 | *
|
||
70 | *
|
||
71 | */
|
||
72 | package com.iver.cit.gvsig.fmap.layers; |
||
73 | /**
|
||
74 | * This class instances a regular WMS dimension. It handles single, multiple and
|
||
75 | * interval values and uses them as they were a point, a list or a regularly
|
||
76 | * split line, respectivelly.<br>
|
||
77 | * <p>
|
||
78 | * As far as it implements IFMapWMSDimension it uses the same interface and
|
||
79 | * documentation.
|
||
80 | * </p>
|
||
81 | * @author jaume dominguez faus (jaume.dominguez@iver.es)
|
||
82 | *
|
||
83 | */
|
||
84 | public class DefaultDimension implements IFMapWMSDimension { |
||
85 | static private final String digit = "[0-9]"; |
||
86 | static private final String nonZeroDigit = "[1-9]"; |
||
87 | static private final String letter = "[_$%a-zA-Z]"; |
||
88 | static private final String word = letter+"("+letter+"|"+digit+")+"; |
||
89 | static private final String floatingPointNumber = "("+digit+"+(\\."+digit+"+)?)"; |
||
90 | static private final String integerNumber = nonZeroDigit+digit+"+"; |
||
91 | static private final String dimensionItem = "("+floatingPointNumber+"|"+word+")"; |
||
92 | /**
|
||
93 | * regular expression for matching dimensions.
|
||
94 | */
|
||
95 | static private final String regexpDefaultDimensionExpression = |
||
96 | "("+floatingPointNumber+"/"+floatingPointNumber+"/"+floatingPointNumber+"|"+ |
||
97 | dimensionItem+"(,"+dimensionItem+")*)"; |
||
98 | |||
99 | private String name; |
||
100 | private String unit; |
||
101 | private String unitSymbol; |
||
102 | private String expression; |
||
103 | private String period; |
||
104 | private Object minValue; |
||
105 | private Object maxValue; |
||
106 | private int type; |
||
107 | 4222 | jaume | private boolean compiled = false; |
108 | 3746 | jaume | /**
|
109 | * Creates a new instance of DefaultDimension.
|
||
110 | * @param _name
|
||
111 | * @param _units
|
||
112 | * @param _unitSymbol
|
||
113 | * @param _dimensionExpression
|
||
114 | */
|
||
115 | public DefaultDimension(String _name, String _units, String _unitSymbol, String _dimensionExpression) { |
||
116 | this.name = _name;
|
||
117 | this.unit = _units;
|
||
118 | this.unitSymbol = _unitSymbol;
|
||
119 | setExpression(_dimensionExpression); |
||
120 | } |
||
121 | |||
122 | 3756 | jaume | /*
|
123 | * (non-Javadoc)
|
||
124 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getName()
|
||
125 | */
|
||
126 | public String getName() { |
||
127 | 3746 | jaume | return name;
|
128 | } |
||
129 | |||
130 | 3756 | jaume | /*
|
131 | * (non-Javadoc)
|
||
132 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getUnit()
|
||
133 | */
|
||
134 | 3746 | jaume | public String getUnit() { |
135 | return unit;
|
||
136 | } |
||
137 | |||
138 | 3756 | jaume | /*
|
139 | * (non-Javadoc)
|
||
140 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getUnitSymbol()
|
||
141 | */
|
||
142 | 3746 | jaume | public String getUnitSymbol() { |
143 | return unitSymbol;
|
||
144 | } |
||
145 | |||
146 | 3756 | jaume | /*
|
147 | * (non-Javadoc)
|
||
148 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getLowLimit()
|
||
149 | */
|
||
150 | 3746 | jaume | public String getLowLimit() { |
151 | return (String) minValue; |
||
152 | } |
||
153 | |||
154 | 3756 | jaume | /*
|
155 | * (non-Javadoc)
|
||
156 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getHighLimit()
|
||
157 | */
|
||
158 | 3746 | jaume | public String getHighLimit() { |
159 | return (String) maxValue; |
||
160 | } |
||
161 | |||
162 | 3756 | jaume | /*
|
163 | * (non-Javadoc)
|
||
164 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getResolution()
|
||
165 | */
|
||
166 | 3746 | jaume | public String getResolution() { |
167 | if (type == INTERVAL) {
|
||
168 | String[] s = expression.split("/"); |
||
169 | return (s.length == 1) ? s[3] : null; |
||
170 | } else return null; |
||
171 | } |
||
172 | |||
173 | 3756 | jaume | /*
|
174 | * (non-Javadoc)
|
||
175 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#isValidValue(java.lang.String)
|
||
176 | */
|
||
177 | 3746 | jaume | public boolean isValidValue(String value) { |
178 | return value.matches(word) || value.matches(floatingPointNumber);
|
||
179 | } |
||
180 | |||
181 | 3756 | jaume | /*
|
182 | * (non-Javadoc)
|
||
183 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#valueOf(java.lang.String)
|
||
184 | */
|
||
185 | 3746 | jaume | public Object valueOf(String value) throws IllegalArgumentException { |
186 | 4222 | jaume | if (compiled) {
|
187 | if (value.matches(word)) {
|
||
188 | return (String) value; |
||
189 | } else if (value.matches(integerNumber)){ |
||
190 | return new Integer(value); |
||
191 | } |
||
192 | else if (value.matches(floatingPointNumber)) { |
||
193 | return new Float(value); |
||
194 | } |
||
195 | 3746 | jaume | } |
196 | return null; |
||
197 | } |
||
198 | |||
199 | 3756 | jaume | /*
|
200 | * (non-Javadoc)
|
||
201 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#valueAt(int)
|
||
202 | */
|
||
203 | 3746 | jaume | public String valueAt(int pos) throws ArrayIndexOutOfBoundsException { |
204 | 4222 | jaume | if (compiled) {
|
205 | if (pos<0 || pos>valueCount()) |
||
206 | throw new ArrayIndexOutOfBoundsException(pos+"(must be >= 0 and <="+valueCount()+")"); |
||
207 | |||
208 | if (type == SINGLE_VALUE)
|
||
209 | return expression;
|
||
210 | |||
211 | if (type == MULTIPLE_VALUE)
|
||
212 | return expression.split(",")[pos]; |
||
213 | |||
214 | if (type == INTERVAL) {
|
||
215 | double minPos = Double.parseDouble((String) minValue); |
||
216 | double maxPos = Double.parseDouble((String) maxValue); |
||
217 | double step = Double.parseDouble(period); |
||
218 | double newPos = minPos + (step*pos);
|
||
219 | if (newPos < minPos)
|
||
220 | return minPos+""; |
||
221 | |||
222 | if (newPos > maxPos)
|
||
223 | return maxPos+""; |
||
224 | return newPos+""; |
||
225 | } |
||
226 | } |
||
227 | 3746 | jaume | return null; |
228 | } |
||
229 | |||
230 | 3756 | jaume | /*
|
231 | * (non-Javadoc)
|
||
232 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#valueCount()
|
||
233 | */
|
||
234 | 3746 | jaume | public int valueCount() { |
235 | 4222 | jaume | if (compiled) {
|
236 | if (type == MULTIPLE_VALUE) {
|
||
237 | return expression.split(",").length; |
||
238 | } else if (type == INTERVAL) { |
||
239 | int count;
|
||
240 | double min = Double.parseDouble((String) minValue); |
||
241 | double max = Double.parseDouble((String) maxValue); |
||
242 | double step = Double.parseDouble(period); |
||
243 | double distance = max - min;
|
||
244 | count = (int) (distance/step);
|
||
245 | return count;
|
||
246 | } else {
|
||
247 | return 1; |
||
248 | } |
||
249 | 3746 | jaume | } |
250 | 4222 | jaume | return -1; |
251 | 3746 | jaume | } |
252 | |||
253 | 3756 | jaume | /*
|
254 | * (non-Javadoc)
|
||
255 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getExpression()
|
||
256 | */
|
||
257 | 3746 | jaume | public String getExpression() { |
258 | return expression;
|
||
259 | } |
||
260 | |||
261 | 3756 | jaume | /*
|
262 | * (non-Javadoc)
|
||
263 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#setExpression(java.lang.String)
|
||
264 | */
|
||
265 | 4222 | jaume | public void setExpression(String expr) { |
266 | 3746 | jaume | expression = expr.toUpperCase(); |
267 | |||
268 | 4222 | jaume | } |
269 | 3746 | jaume | |
270 | 4222 | jaume | /*
|
271 | * (non-Javadoc)
|
||
272 | * @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getType()
|
||
273 | */
|
||
274 | public int getType() { |
||
275 | return type;
|
||
276 | } |
||
277 | |||
278 | public void compile() throws IllegalArgumentException { |
||
279 | if (expression.matches(regexpDefaultDimensionExpression)){
|
||
280 | |||
281 | 3746 | jaume | } else {
|
282 | //System.err.println("Invalid dimension expression: "+expr+" (for "+this.getName()+")");
|
||
283 | throw new IllegalArgumentException(); |
||
284 | } |
||
285 | 4222 | jaume | |
286 | |||
287 | 3746 | jaume | String separator;
|
288 | if (expression.indexOf("/")!=-1) { |
||
289 | separator = "/";
|
||
290 | type = INTERVAL; |
||
291 | } else if (expression.indexOf(",")!=-1) { |
||
292 | separator = ",";
|
||
293 | type = MULTIPLE_VALUE; |
||
294 | } else {
|
||
295 | separator = ",";
|
||
296 | type = SINGLE_VALUE; |
||
297 | } |
||
298 | 4222 | jaume | compiled = true;
|
299 | 3746 | jaume | String[] s = expression.split(separator); |
300 | minValue = valueOf(s[0]);
|
||
301 | if (type == INTERVAL) {
|
||
302 | maxValue = (s.length>1) ? valueOf(s[1]) : valueOf(s[0]); |
||
303 | period = (s.length>2) ? s[2] : null; |
||
304 | } else if (type == MULTIPLE_VALUE) { |
||
305 | maxValue = valueOf(s[s.length-1]);
|
||
306 | } else {
|
||
307 | maxValue = valueOf(s[0]);
|
||
308 | } |
||
309 | 4222 | jaume | |
310 | 3746 | jaume | } |
311 | |||
312 | } |