root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 14426
History | View | Annotate | Download (10.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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 |
package com.iver.cit.gvsig.fmap.rendering; |
42 |
|
43 |
import java.awt.Color; |
44 |
|
45 |
import com.iver.cit.gvsig.fmap.core.ISLDCompatible; |
46 |
import com.iver.cit.gvsig.fmap.core.SLDTags; |
47 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
48 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
49 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
50 |
import com.iver.utiles.StringUtilities; |
51 |
import com.iver.utiles.XMLEntity; |
52 |
|
53 |
|
54 |
/**
|
55 |
* <p>VectorialIntervalLegend (which should be better called GraduatedColorLegend) is
|
56 |
* a legend that allows to classify ranges of values using a color gradient based
|
57 |
* on a value.<b>
|
58 |
* </p>
|
59 |
* <p>
|
60 |
* The color gradient will be calculated according the starting color, the
|
61 |
* ending color and the amount of intervals.
|
62 |
* </p>
|
63 |
* @author Vicente Caballero Navarro
|
64 |
*/
|
65 |
public class VectorialIntervalLegend extends AbstractIntervalLegend { |
66 |
private Color startColor = Color.red; |
67 |
private Color endColor = Color.blue; |
68 |
private ZSort zSort;
|
69 |
/**
|
70 |
* Constructor method
|
71 |
*/
|
72 |
public VectorialIntervalLegend() {
|
73 |
//defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* Constructor method
|
78 |
*
|
79 |
* @param type type of the shape.
|
80 |
*/
|
81 |
public VectorialIntervalLegend(int type) { |
82 |
setShapeType(type); |
83 |
} |
84 |
|
85 |
|
86 |
public String getSLDString(String layerName) { |
87 |
|
88 |
try{
|
89 |
|
90 |
XmlBuilder xmlBuilder = new XmlBuilder();
|
91 |
xmlBuilder.writeHeader(); |
92 |
xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0); |
93 |
xmlBuilder.openTag(SLDTags.NAMEDLAYER); |
94 |
xmlBuilder.writeTag(SLDTags.NAME,layerName); |
95 |
xmlBuilder.openTag(SLDTags.USERSTYLE); |
96 |
xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE); |
97 |
xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,fieldNames[0]);
|
98 |
|
99 |
ISymbol[] symbols = this.getSymbols(); |
100 |
Object[] values = this.getValues(); |
101 |
|
102 |
FInterval interval; |
103 |
for(int i = 0; i < symbols.length; i++ ) |
104 |
{ |
105 |
interval = (FInterval)values[i]; |
106 |
//interval = (FInterval)getInterval(ValueFactory.createValue(valueDbl.doubleValue()));
|
107 |
xmlBuilder.openTag(SLDTags.RULE); |
108 |
xmlBuilder.writeTag(SLDTags.NAME, ""+interval.getMin() +" - " +interval.getMax()); |
109 |
xmlBuilder.openTag(SLDTags.FILTER); |
110 |
xmlBuilder.openTag(SLDTags.AND); |
111 |
xmlBuilder.openTag(SLDTags.PROPERTYISGREATEROREQUALTHAN); |
112 |
xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldNames[0]);
|
113 |
xmlBuilder.writeTag(SLDTags.LITERAL, ""+interval.getMin());
|
114 |
xmlBuilder.closeTag(); |
115 |
xmlBuilder.openTag(SLDTags.PROPERTYISLESSOREQUALTHAN); |
116 |
xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldNames[0]);
|
117 |
xmlBuilder.writeTag(SLDTags.LITERAL, ""+interval.getMax());
|
118 |
xmlBuilder.closeTag(); |
119 |
xmlBuilder.closeTag(); |
120 |
if (symbols[i] instanceof ISLDCompatible) |
121 |
{ |
122 |
ISLDCompatible symSLD = (ISLDCompatible) symbols[i]; |
123 |
xmlBuilder.writeRaw(symSLD.toSLD()); |
124 |
} |
125 |
else
|
126 |
throw new RuntimeException("Cannot convert Symbol " + i + " " + symbols[i].getDescription() + " to SLD"); |
127 |
xmlBuilder.closeTag(); |
128 |
xmlBuilder.closeTag(); |
129 |
} |
130 |
|
131 |
xmlBuilder.closeTag(); |
132 |
xmlBuilder.closeTag(); |
133 |
xmlBuilder.closeTag(); |
134 |
xmlBuilder.closeTag(); |
135 |
return xmlBuilder.getXML();
|
136 |
|
137 |
}catch(Exception e) |
138 |
{ |
139 |
e.printStackTrace(); |
140 |
return null; |
141 |
} |
142 |
} |
143 |
|
144 |
|
145 |
public XMLEntity getXMLEntity() {
|
146 |
XMLEntity xml = new XMLEntity();
|
147 |
xml.putProperty("className", this.getClass().getName()); |
148 |
xml.putProperty("useDefaultSymbolB", useDefaultSymbol);
|
149 |
if (getDefaultSymbol() == null) { |
150 |
xml.putProperty("useDefaultSymbol", 0); |
151 |
} else {
|
152 |
xml.putProperty("useDefaultSymbol", 1); |
153 |
xml.addChild(getDefaultSymbol().getXMLEntity()); |
154 |
} |
155 |
|
156 |
xml.putProperty("fieldName", fieldNames[0]); |
157 |
xml.putProperty("index", index);
|
158 |
|
159 |
xml.putProperty("intervalType", intervalType);
|
160 |
xml.putProperty("numKeys", keys.size());
|
161 |
|
162 |
if (keys.size() > 0) { |
163 |
xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName()); |
164 |
|
165 |
String[] sk = new String[keys.size()]; |
166 |
|
167 |
for (int i = 0; i < keys.size(); i++) { |
168 |
sk[i] = ((IInterval) keys.get(i)).toString(); |
169 |
} |
170 |
|
171 |
xml.putProperty("keys", getValues());
|
172 |
|
173 |
for (int i = 0; i < keys.size(); i++) { |
174 |
xml.addChild(getSymbols()[i].getXMLEntity()); |
175 |
} |
176 |
} |
177 |
|
178 |
xml.putProperty("startColor", StringUtilities.color2String(startColor));
|
179 |
xml.putProperty("endColor", StringUtilities.color2String(endColor));
|
180 |
|
181 |
///xml.putProperty("numSymbols", symbols.size());
|
182 |
///xml.putProperty("indexs", getIndexs());
|
183 |
///xml.putProperty("values", getValues());
|
184 |
return xml;
|
185 |
} |
186 |
|
187 |
|
188 |
public void setXMLEntity03(XMLEntity xml) { |
189 |
fieldNames = new String[] {xml.getStringProperty("fieldName")}; |
190 |
index = xml.getIntProperty("index");
|
191 |
|
192 |
if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n. |
193 |
intervalType = xml.getIntProperty("intervalType");
|
194 |
} |
195 |
|
196 |
int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol"); |
197 |
|
198 |
if (useDefaultSymbol == 1) { |
199 |
setDefaultSymbol( SymbologyFactory.createSymbolFromXML(xml.getChild(0), null)); |
200 |
} else {
|
201 |
setDefaultSymbol(null);
|
202 |
} |
203 |
|
204 |
int numKeys = xml.getIntProperty("numKeys"); |
205 |
|
206 |
if (numKeys > 0) { |
207 |
String className = xml.getStringProperty("tipoValueKeys"); |
208 |
String[] sk = xml.getStringArrayProperty("keys"); |
209 |
IInterval auxInterval; |
210 |
|
211 |
for (int i = 0; i < numKeys; i++) { |
212 |
auxInterval = FInterval.create(sk[i]); |
213 |
symbols.put(auxInterval, |
214 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null));
|
215 |
keys.add(auxInterval); |
216 |
System.out.println("auxInterval =" + auxInterval + "Symbol =" + |
217 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null)
|
218 |
.getDescription()); |
219 |
} |
220 |
} |
221 |
|
222 |
startColor = StringUtilities.string2Color(xml.getStringProperty( |
223 |
"startColor"));
|
224 |
endColor = StringUtilities.string2Color(xml.getStringProperty( |
225 |
"endColor"));
|
226 |
} |
227 |
|
228 |
|
229 |
public void setXMLEntity(XMLEntity xml) { |
230 |
fieldNames = new String[] {xml.getStringProperty("fieldName")}; |
231 |
index = xml.getIntProperty("index");
|
232 |
|
233 |
if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n. |
234 |
intervalType = xml.getIntProperty("intervalType");
|
235 |
} |
236 |
useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbolB");
|
237 |
int hasDefaultSymbol = xml.getIntProperty("useDefaultSymbol"); |
238 |
|
239 |
if (hasDefaultSymbol == 1) { |
240 |
setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0), null)); |
241 |
} else {
|
242 |
setDefaultSymbol(null);
|
243 |
} |
244 |
|
245 |
int numKeys = xml.getIntProperty("numKeys"); |
246 |
|
247 |
if (numKeys > 0) { |
248 |
String[] sk = xml.getStringArrayProperty("keys"); |
249 |
IInterval auxInterval; |
250 |
|
251 |
for (int i = 0; i < numKeys; i++) { |
252 |
auxInterval = FInterval.create(sk[i]); |
253 |
symbols.put(auxInterval, |
254 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null));
|
255 |
keys.add(auxInterval); |
256 |
System.out.println("auxInterval =" + auxInterval + "Symbol =" + |
257 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null)
|
258 |
.getDescription()); |
259 |
} |
260 |
} |
261 |
|
262 |
startColor = StringUtilities.string2Color(xml.getStringProperty( |
263 |
"startColor"));
|
264 |
endColor = StringUtilities.string2Color(xml.getStringProperty( |
265 |
"endColor"));
|
266 |
} |
267 |
|
268 |
|
269 |
public ILegend cloneLegend() throws XMLException { |
270 |
return LegendFactory.createFromXML(getXMLEntity());
|
271 |
} |
272 |
|
273 |
/**
|
274 |
* Returns the final color
|
275 |
* @return Color final color.
|
276 |
* @uml.property name="endColor"
|
277 |
*/
|
278 |
public Color getEndColor() { |
279 |
return endColor;
|
280 |
} |
281 |
|
282 |
/**
|
283 |
* Inserts the final color.
|
284 |
* @param endColor final color.
|
285 |
* @uml.property name="endColor"
|
286 |
*/
|
287 |
public void setEndColor(Color endColor) { |
288 |
this.endColor = endColor;
|
289 |
} |
290 |
|
291 |
/**
|
292 |
* Returns the initial color.
|
293 |
* @return Color initial color.
|
294 |
* @uml.property name="startColor"
|
295 |
*/
|
296 |
public Color getStartColor() { |
297 |
return startColor;
|
298 |
} |
299 |
|
300 |
/**
|
301 |
* Inserts the initial color
|
302 |
* @param startColor initial color.
|
303 |
* @uml.property name="startColor"
|
304 |
*/
|
305 |
public void setStartColor(Color startColor) { |
306 |
this.startColor = startColor;
|
307 |
} |
308 |
|
309 |
public int getShapeType() { |
310 |
return shapeType;
|
311 |
} |
312 |
|
313 |
public void setShapeType(int shapeType) { |
314 |
if (this.shapeType != shapeType) { |
315 |
setDefaultSymbol(SymbologyFactory. |
316 |
createDefaultSymbolByShapeType(shapeType)); |
317 |
this.shapeType = shapeType;
|
318 |
} |
319 |
} |
320 |
|
321 |
public ZSort getZSort() {
|
322 |
return zSort;
|
323 |
} |
324 |
|
325 |
public void setZSort(ZSort zSort) { |
326 |
// TODO Implement it
|
327 |
throw new Error("Not yet implemented!"); |
328 |
|
329 |
} |
330 |
|
331 |
public String getClassName() { |
332 |
return getClass().getName();
|
333 |
} |
334 |
|
335 |
} |