svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 16454
History | View | Annotate | Download (10.3 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 |
/**
|
69 |
* Constructor method
|
70 |
*/
|
71 |
public VectorialIntervalLegend() {
|
72 |
//defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
|
73 |
} |
74 |
|
75 |
/**
|
76 |
* Constructor method
|
77 |
*
|
78 |
* @param type type of the shape.
|
79 |
*/
|
80 |
public VectorialIntervalLegend(int type) { |
81 |
setShapeType(type); |
82 |
} |
83 |
|
84 |
|
85 |
public String getSLDString(String layerName) { |
86 |
|
87 |
try{
|
88 |
|
89 |
XmlBuilder xmlBuilder = new XmlBuilder();
|
90 |
xmlBuilder.writeHeader(); |
91 |
xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0); |
92 |
xmlBuilder.openTag(SLDTags.NAMEDLAYER); |
93 |
xmlBuilder.writeTag(SLDTags.NAME,layerName); |
94 |
xmlBuilder.openTag(SLDTags.USERSTYLE); |
95 |
xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE); |
96 |
xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,getClassifyingFieldNames()[0]);
|
97 |
|
98 |
ISymbol[] symbols = this.getSymbols(); |
99 |
Object[] values = this.getValues(); |
100 |
|
101 |
FInterval interval; |
102 |
for(int i = 0; i < symbols.length; i++ ) |
103 |
{ |
104 |
interval = (FInterval)values[i]; |
105 |
//interval = (FInterval)getInterval(ValueFactory.createValue(valueDbl.doubleValue()));
|
106 |
xmlBuilder.openTag(SLDTags.RULE); |
107 |
xmlBuilder.writeTag(SLDTags.NAME, ""+interval.getMin() +" - " +interval.getMax()); |
108 |
xmlBuilder.openTag(SLDTags.FILTER); |
109 |
xmlBuilder.openTag(SLDTags.AND); |
110 |
xmlBuilder.openTag(SLDTags.PROPERTYISGREATEROREQUALTHAN); |
111 |
xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldNames[0]);
|
112 |
xmlBuilder.writeTag(SLDTags.LITERAL, ""+interval.getMin());
|
113 |
xmlBuilder.closeTag(); |
114 |
xmlBuilder.openTag(SLDTags.PROPERTYISLESSOREQUALTHAN); |
115 |
xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldNames[0]);
|
116 |
xmlBuilder.writeTag(SLDTags.LITERAL, ""+interval.getMax());
|
117 |
xmlBuilder.closeTag(); |
118 |
xmlBuilder.closeTag(); |
119 |
if (symbols[i] instanceof ISLDCompatible) |
120 |
{ |
121 |
ISLDCompatible symSLD = (ISLDCompatible) symbols[i]; |
122 |
xmlBuilder.writeRaw(symSLD.toSLD()); |
123 |
} |
124 |
else
|
125 |
throw new RuntimeException("Cannot convert Symbol " + i + " " + symbols[i].getDescription() + " to SLD"); |
126 |
xmlBuilder.closeTag(); |
127 |
xmlBuilder.closeTag(); |
128 |
} |
129 |
|
130 |
xmlBuilder.closeTag(); |
131 |
xmlBuilder.closeTag(); |
132 |
xmlBuilder.closeTag(); |
133 |
xmlBuilder.closeTag(); |
134 |
return xmlBuilder.getXML();
|
135 |
|
136 |
}catch(Exception e) |
137 |
{ |
138 |
e.printStackTrace(); |
139 |
return null; |
140 |
} |
141 |
} |
142 |
|
143 |
|
144 |
public XMLEntity getXMLEntity() {
|
145 |
XMLEntity xml = new XMLEntity();
|
146 |
xml.putProperty("className", this.getClass().getName()); |
147 |
xml.putProperty("useDefaultSymbolB", useDefaultSymbol);
|
148 |
if (getDefaultSymbol() == null) { |
149 |
xml.putProperty("useDefaultSymbol", 0); |
150 |
} else {
|
151 |
xml.putProperty("useDefaultSymbol", 1); |
152 |
xml.addChild(getDefaultSymbol().getXMLEntity()); |
153 |
} |
154 |
|
155 |
xml.putProperty("fieldName", fieldNames[0]); |
156 |
xml.putProperty("index", index);
|
157 |
|
158 |
xml.putProperty("intervalType", intervalType);
|
159 |
xml.putProperty("numKeys", keys.size());
|
160 |
|
161 |
if (keys.size() > 0) { |
162 |
xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName()); |
163 |
|
164 |
String[] sk = new String[keys.size()]; |
165 |
|
166 |
for (int i = 0; i < keys.size(); i++) { |
167 |
sk[i] = ((IInterval) keys.get(i)).toString(); |
168 |
} |
169 |
|
170 |
xml.putProperty("keys", getValues());
|
171 |
|
172 |
for (int i = 0; i < keys.size(); i++) { |
173 |
xml.addChild(getSymbols()[i].getXMLEntity()); |
174 |
} |
175 |
} |
176 |
|
177 |
xml.putProperty("startColor", StringUtilities.color2String(startColor));
|
178 |
xml.putProperty("endColor", StringUtilities.color2String(endColor));
|
179 |
|
180 |
///xml.putProperty("numSymbols", symbols.size());
|
181 |
///xml.putProperty("indexs", getIndexs());
|
182 |
///xml.putProperty("values", getValues());
|
183 |
return xml;
|
184 |
} |
185 |
|
186 |
|
187 |
public void setXMLEntity03(XMLEntity xml) { |
188 |
fieldNames = new String[] {xml.getStringProperty("fieldName")}; |
189 |
index = xml.getIntProperty("index");
|
190 |
|
191 |
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. |
192 |
intervalType = xml.getIntProperty("intervalType");
|
193 |
} |
194 |
|
195 |
int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol"); |
196 |
|
197 |
if (useDefaultSymbol == 1) { |
198 |
setDefaultSymbol( SymbologyFactory.createSymbolFromXML(xml.getChild(0), null)); |
199 |
} else {
|
200 |
setDefaultSymbol(null);
|
201 |
} |
202 |
|
203 |
int numKeys = xml.getIntProperty("numKeys"); |
204 |
|
205 |
if (numKeys > 0) { |
206 |
String className = xml.getStringProperty("tipoValueKeys"); |
207 |
String[] sk = xml.getStringArrayProperty("keys"); |
208 |
IInterval auxInterval; |
209 |
|
210 |
for (int i = 0; i < numKeys; i++) { |
211 |
auxInterval = FInterval.create(sk[i]); |
212 |
symbols.put(auxInterval, |
213 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null));
|
214 |
keys.add(auxInterval); |
215 |
System.out.println("auxInterval =" + auxInterval + "Symbol =" + |
216 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null)
|
217 |
.getDescription()); |
218 |
} |
219 |
} |
220 |
|
221 |
startColor = StringUtilities.string2Color(xml.getStringProperty( |
222 |
"startColor"));
|
223 |
endColor = StringUtilities.string2Color(xml.getStringProperty( |
224 |
"endColor"));
|
225 |
} |
226 |
|
227 |
|
228 |
public void setXMLEntity(XMLEntity xml) { |
229 |
fieldNames = new String[] {xml.getStringProperty("fieldName")}; |
230 |
index = xml.getIntProperty("index");
|
231 |
|
232 |
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. |
233 |
intervalType = xml.getIntProperty("intervalType");
|
234 |
} |
235 |
useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbolB");
|
236 |
int hasDefaultSymbol = xml.getIntProperty("useDefaultSymbol"); |
237 |
|
238 |
if (hasDefaultSymbol == 1) { |
239 |
setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0), null)); |
240 |
} else {
|
241 |
setDefaultSymbol(null);
|
242 |
} |
243 |
|
244 |
int numKeys = xml.getIntProperty("numKeys"); |
245 |
|
246 |
if (numKeys > 0) { |
247 |
String[] sk = xml.getStringArrayProperty("keys"); |
248 |
IInterval auxInterval; |
249 |
|
250 |
for (int i = 0; i < numKeys; i++) { |
251 |
auxInterval = FInterval.create(sk[i]); |
252 |
symbols.put(auxInterval, |
253 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null));
|
254 |
keys.add(auxInterval); |
255 |
System.out.println("auxInterval =" + auxInterval + "Symbol =" + |
256 |
SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null)
|
257 |
.getDescription()); |
258 |
} |
259 |
} |
260 |
|
261 |
startColor = StringUtilities.string2Color(xml.getStringProperty( |
262 |
"startColor"));
|
263 |
endColor = StringUtilities.string2Color(xml.getStringProperty( |
264 |
"endColor"));
|
265 |
} |
266 |
|
267 |
|
268 |
public ILegend cloneLegend() throws XMLException { |
269 |
return LegendFactory.createFromXML(getXMLEntity());
|
270 |
} |
271 |
|
272 |
/**
|
273 |
* Returns the final color
|
274 |
* @return Color final color.
|
275 |
* @uml.property name="endColor"
|
276 |
*/
|
277 |
public Color getEndColor() { |
278 |
return endColor;
|
279 |
} |
280 |
|
281 |
/**
|
282 |
* Inserts the final color.
|
283 |
* @param endColor final color.
|
284 |
* @uml.property name="endColor"
|
285 |
*/
|
286 |
public void setEndColor(Color endColor) { |
287 |
this.endColor = endColor;
|
288 |
} |
289 |
|
290 |
/**
|
291 |
* Returns the initial color.
|
292 |
* @return Color initial color.
|
293 |
* @uml.property name="startColor"
|
294 |
*/
|
295 |
public Color getStartColor() { |
296 |
return startColor;
|
297 |
} |
298 |
|
299 |
/**
|
300 |
* Inserts the initial color
|
301 |
* @param startColor initial color.
|
302 |
* @uml.property name="startColor"
|
303 |
*/
|
304 |
public void setStartColor(Color startColor) { |
305 |
this.startColor = startColor;
|
306 |
} |
307 |
|
308 |
public int getShapeType() { |
309 |
return shapeType;
|
310 |
} |
311 |
|
312 |
public void setShapeType(int shapeType) { |
313 |
if (this.shapeType != shapeType) { |
314 |
setDefaultSymbol(SymbologyFactory. |
315 |
createDefaultSymbolByShapeType(shapeType)); |
316 |
this.shapeType = shapeType;
|
317 |
} |
318 |
} |
319 |
|
320 |
|
321 |
public String getClassName() { |
322 |
return getClass().getName();
|
323 |
} |
324 |
|
325 |
} |