svn-gvsig-desktop / branches / v05 / extensions / extWMS / src / com / iver / cit / gvsig / fmap / layers / DefaultDimension.java @ 4054
History | View | Annotate | Download (8.13 KB)
1 |
/* 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: DefaultDimension.java 4054 2006-02-10 13:22:35Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.3.2.4 2006-02-10 13:22:35 jaume
|
47 |
* now analyzes dimensions on demand
|
48 |
*
|
49 |
* Revision 1.3.2.3 2006/01/31 16:25:24 jaume
|
50 |
* correcciones de bugs
|
51 |
*
|
52 |
* Revision 1.4 2006/01/26 16:07:14 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.3.2.1 2006/01/26 12:59:32 jaume
|
56 |
* 0.5
|
57 |
*
|
58 |
* Revision 1.3 2006/01/25 09:08:53 jaume
|
59 |
* test save and reload project
|
60 |
*
|
61 |
* Revision 1.2 2006/01/24 14:36:33 jaume
|
62 |
* This is the new version
|
63 |
*
|
64 |
* Revision 1.1.2.1 2006/01/20 15:22:46 jaume
|
65 |
* *** empty log message ***
|
66 |
*
|
67 |
*
|
68 |
*/
|
69 |
package com.iver.cit.gvsig.fmap.layers; |
70 |
/**
|
71 |
* This class instances a regular WMS dimension. It handles single, multiple and
|
72 |
* interval values and uses them as they were a point, a list or a regularly
|
73 |
* split line, respectivelly.<br>
|
74 |
* <p>
|
75 |
* As far as it implements IFMapWMSDimension it uses the same interface and
|
76 |
* documentation.
|
77 |
* </p>
|
78 |
* @author jaume dominguez faus (jaume.dominguez@iver.es)
|
79 |
*
|
80 |
*/
|
81 |
public class DefaultDimension implements IFMapWMSDimension { |
82 |
static private final String digit = "[0-9]"; |
83 |
static private final String nonZeroDigit = "[1-9]"; |
84 |
static private final String letter = "[_$%a-zA-Z]"; |
85 |
static private final String word = letter+"("+letter+"|"+digit+")+"; |
86 |
static private final String floatingPointNumber = "("+digit+"+(\\."+digit+"+)?)"; |
87 |
static private final String integerNumber = nonZeroDigit+digit+"+"; |
88 |
static private final String dimensionItem = "("+floatingPointNumber+"|"+word+")"; |
89 |
/**
|
90 |
* regular expression for matching dimensions.
|
91 |
*/
|
92 |
static private final String regexpDefaultDimensionExpression = |
93 |
"("+floatingPointNumber+"/"+floatingPointNumber+"/"+floatingPointNumber+"|"+ |
94 |
dimensionItem+"(,"+dimensionItem+")*)"; |
95 |
|
96 |
private String name; |
97 |
private String unit; |
98 |
private String unitSymbol; |
99 |
private String expression; |
100 |
private String period; |
101 |
private Object minValue; |
102 |
private Object maxValue; |
103 |
private int type; |
104 |
private boolean compiled = false; |
105 |
/**
|
106 |
* Creates a new instance of DefaultDimension.
|
107 |
* @param _name
|
108 |
* @param _units
|
109 |
* @param _unitSymbol
|
110 |
* @param _dimensionExpression
|
111 |
*/
|
112 |
public DefaultDimension(String _name, String _units, String _unitSymbol, String _dimensionExpression) { |
113 |
this.name = _name;
|
114 |
this.unit = _units;
|
115 |
this.unitSymbol = _unitSymbol;
|
116 |
setExpression(_dimensionExpression); |
117 |
} |
118 |
|
119 |
/*
|
120 |
* (non-Javadoc)
|
121 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getName()
|
122 |
*/
|
123 |
public String getName() { |
124 |
return name;
|
125 |
} |
126 |
|
127 |
/*
|
128 |
* (non-Javadoc)
|
129 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getUnit()
|
130 |
*/
|
131 |
public String getUnit() { |
132 |
return unit;
|
133 |
} |
134 |
|
135 |
/*
|
136 |
* (non-Javadoc)
|
137 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getUnitSymbol()
|
138 |
*/
|
139 |
public String getUnitSymbol() { |
140 |
return unitSymbol;
|
141 |
} |
142 |
|
143 |
/*
|
144 |
* (non-Javadoc)
|
145 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getLowLimit()
|
146 |
*/
|
147 |
public String getLowLimit() { |
148 |
return (String) minValue; |
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getHighLimit()
|
154 |
*/
|
155 |
public String getHighLimit() { |
156 |
return (String) maxValue; |
157 |
} |
158 |
|
159 |
/*
|
160 |
* (non-Javadoc)
|
161 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getResolution()
|
162 |
*/
|
163 |
public String getResolution() { |
164 |
if (type == INTERVAL) {
|
165 |
String[] s = expression.split("/"); |
166 |
return (s.length == 1) ? s[3] : null; |
167 |
} else return null; |
168 |
} |
169 |
|
170 |
/*
|
171 |
* (non-Javadoc)
|
172 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#isValidValue(java.lang.String)
|
173 |
*/
|
174 |
public boolean isValidValue(String value) { |
175 |
return value.matches(word) || value.matches(floatingPointNumber);
|
176 |
} |
177 |
|
178 |
/*
|
179 |
* (non-Javadoc)
|
180 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#valueOf(java.lang.String)
|
181 |
*/
|
182 |
public Object valueOf(String value) throws IllegalArgumentException { |
183 |
if (compiled) {
|
184 |
if (value.matches(word)) {
|
185 |
return (String) value; |
186 |
} else if (value.matches(integerNumber)){ |
187 |
return new Integer(value); |
188 |
} |
189 |
else if (value.matches(floatingPointNumber)) { |
190 |
return new Float(value); |
191 |
} |
192 |
} |
193 |
return null; |
194 |
} |
195 |
|
196 |
/*
|
197 |
* (non-Javadoc)
|
198 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#valueAt(int)
|
199 |
*/
|
200 |
public String valueAt(int pos) throws ArrayIndexOutOfBoundsException { |
201 |
if (compiled) {
|
202 |
if (pos<0 || pos>valueCount()) |
203 |
throw new ArrayIndexOutOfBoundsException(pos+"(must be >= 0 and <="+valueCount()+")"); |
204 |
|
205 |
if (type == SINGLE_VALUE)
|
206 |
return expression;
|
207 |
|
208 |
if (type == MULTIPLE_VALUE)
|
209 |
return expression.split(",")[pos]; |
210 |
|
211 |
if (type == INTERVAL) {
|
212 |
double minPos = Double.parseDouble((String) minValue); |
213 |
double maxPos = Double.parseDouble((String) maxValue); |
214 |
double step = Double.parseDouble(period); |
215 |
double newPos = minPos + (step*pos);
|
216 |
if (newPos < minPos)
|
217 |
return minPos+""; |
218 |
|
219 |
if (newPos > maxPos)
|
220 |
return maxPos+""; |
221 |
return newPos+""; |
222 |
} |
223 |
} |
224 |
return null; |
225 |
} |
226 |
|
227 |
/*
|
228 |
* (non-Javadoc)
|
229 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#valueCount()
|
230 |
*/
|
231 |
public int valueCount() { |
232 |
if (compiled) {
|
233 |
if (type == MULTIPLE_VALUE) {
|
234 |
return expression.split(",").length; |
235 |
} else if (type == INTERVAL) { |
236 |
int count;
|
237 |
double min = Double.parseDouble((String) minValue); |
238 |
double max = Double.parseDouble((String) maxValue); |
239 |
double step = Double.parseDouble(period); |
240 |
double distance = max - min;
|
241 |
count = (int) (distance/step);
|
242 |
return count;
|
243 |
} else {
|
244 |
return 1; |
245 |
} |
246 |
} |
247 |
return -1; |
248 |
} |
249 |
|
250 |
/*
|
251 |
* (non-Javadoc)
|
252 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getExpression()
|
253 |
*/
|
254 |
public String getExpression() { |
255 |
return expression;
|
256 |
} |
257 |
|
258 |
/*
|
259 |
* (non-Javadoc)
|
260 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#setExpression(java.lang.String)
|
261 |
*/
|
262 |
public void setExpression(String expr) { |
263 |
expression = expr.toUpperCase(); |
264 |
|
265 |
} |
266 |
|
267 |
/*
|
268 |
* (non-Javadoc)
|
269 |
* @see com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension#getType()
|
270 |
*/
|
271 |
public int getType() { |
272 |
return type;
|
273 |
} |
274 |
|
275 |
public void compile() throws IllegalArgumentException { |
276 |
if (expression.matches(regexpDefaultDimensionExpression)){
|
277 |
|
278 |
} else {
|
279 |
//System.err.println("Invalid dimension expression: "+expr+" (for "+this.getName()+")");
|
280 |
throw new IllegalArgumentException(); |
281 |
} |
282 |
|
283 |
|
284 |
String separator;
|
285 |
if (expression.indexOf("/")!=-1) { |
286 |
separator = "/";
|
287 |
type = INTERVAL; |
288 |
} else if (expression.indexOf(",")!=-1) { |
289 |
separator = ",";
|
290 |
type = MULTIPLE_VALUE; |
291 |
} else {
|
292 |
separator = ",";
|
293 |
type = SINGLE_VALUE; |
294 |
} |
295 |
compiled = true;
|
296 |
String[] s = expression.split(separator); |
297 |
minValue = valueOf(s[0]);
|
298 |
if (type == INTERVAL) {
|
299 |
maxValue = (s.length>1) ? valueOf(s[1]) : valueOf(s[0]); |
300 |
period = (s.length>2) ? s[2] : null; |
301 |
} else if (type == MULTIPLE_VALUE) { |
302 |
maxValue = valueOf(s[s.length-1]);
|
303 |
} else {
|
304 |
maxValue = valueOf(s[0]);
|
305 |
} |
306 |
|
307 |
} |
308 |
|
309 |
} |